PDA

View Full Version : Calculating the curve between two or more points - Help (for 2.49)



Sunjay03
21-May-10, 21:13
Given two or more points (plus any other variables), can someone give me the Python code/just the formula for calculating the curve between them? Just a general bezier curve, it doesn't have to be super complicated.

I don't know a lot about curves, but I am defiantly willing to learn.

If you need a starting point:


# a list of points
points = [(0,0),(1,1),(2,0)]

# the rest is up to you


This would be used in some sort of "click to add points to this graph" kind of interface.

Another interesting thing would be a formula to get any point on that curve.
-Sunjay03

[EDIT] This is for Blender 2.49

RickyBlender
21-May-10, 21:36
the script shown is for 2,4 or do you need this in 2,5 ?

now the script calculate all the points required by the algo

so what do you eman by other points here ?

Thanks

Sunjay03
22-May-10, 10:49
the script shown is for 2,4 or do you need this in 2,5 ?

now the script calculate all the points required by the algo

so what do you eman by other points here ?

Thanks

All I need, is the algorithm for a curve, which should tell me where the new points of the curve are between the first two points of my list of points.

So something like this:



# a list of points
points = [(0,1),(1,1),(0,2)]
# the first point
current_point = points[0]
# all the new points that will be generated soon. in a list
newpoints = []

for point in points[1:]:
newpoints.append(current_point)

# The curve algorithm adds lots of points to new points
# based on current_point and point

current_point = point


This is for Blender 2.49 and therefore, Python 2.6
-Sunjay03

RickyBlender
22-May-10, 11:04
find the algo and write it in python
should be very easy to do
in the other script it was for a sinus i think so the equation is very simple in this case

Y = A sin(angle ) and tnis give you the 2 points
you could also scale it or rotate afterward or add some equations to do that!

dpends on the equation i cannot guess what you need as algo here
you have to find it and then do it in python !



the script shown in example use TK
i mean i never used this before so cannot help you with this one
but if you find the doc let us know
in any case it would be better to go with 2,5 i guess


hope it helps

Abidos
22-May-10, 11:16
Hi there!

I think, what you need is an implementation of certain interpolation method in Python. I have translated some interpolation algorithms to Turbo Pascal (Delphi) some 2-3 years ago but they were regarding 2D curves, i.e. interpolating the point data to obtaining a curve that best matches the points. For doing that one should have the equation of the function of the required curve in advance in parametric form, then using certain algorithm (for example, the less sum of squares method) - to arrive at the best set of parameters for THAT equation given a precision threshold which should be also previously known. A good reading to start with the matter is given here (http://local.wasp.uwa.edu.au/%7Epbourke/miscellaneous/interpolation/). To me, the solution would look like using the general 3D cubic or 3D hermite interpolation for best results in Blender as the points may be really 3D :spin: Also, reflecting the realities in Blender, one should know at what precision to generate a point to represent the solution curve visually. And finally, I think the more are the points, the more precise interpolation you can get but after a longer calculation time! One can rely on such interpolation INSIDE the point cloud, while OUTSIDE it - the curve just goes endlessly as per the last interpolation restrictions, sooo one should be careful with that! :)

Regards,

Sunjay03
22-May-10, 12:24
Hi there!
A good reading to start with the matter is given here (http://local.wasp.uwa.edu.au/%7Epbourke/miscellaneous/interpolation/).
Regards,

Thanks Abidos! Unfortunately, I didn't have a lot of luck with any of their algorithms....:(

Even the simple cosine ones didn't seem to work.
Here is the blend I am working with.
http://www.pasteall.org/blend/2758

Press "p" in the 3D view to play the Game Engine and see a completely linear curve...?

I used the cosine interpolate algorithm.

Hope you can help.
-Sunjay03

testscreenings
22-May-10, 12:42
Sunjay03 you may be in luck.
I didn't look at what you are doing, but i believe this is what you want:
http://www.pasteall.org/13296/python

just put 0 in for nth and you get your point at t on your beziercurve described
by the verts you put in.

Hope that helps.

edit:
sorry forgot the binomial coefficent:
http://www.pasteall.org/13298/python

Sunjay03
22-May-10, 12:59
Sunjay03 you may be in luck.
I didn't look at what you are doing, but i believe this is what you want:
http://www.pasteall.org/13296/python

just put 0 in for nth and you get your point at t on your beziercurve described
by the verts you put in.

Hope that helps.

edit:
sorry forgot the binomial coefficent:
http://www.pasteall.org/13298/python

Thanks for your reply!
Could you go into a bit more detail of how I could use it?

Here is a complete version of the code:
http://www.pasteall.org/13299/python

Maybe just describe what verts and nth are.

Thanks!
-Sunjay03

testscreenings
22-May-10, 13:21
verts is a list of coordinates like [(1,4,3), (4,2,6),...]
nth is the number of the derivative one wants.
If you put 0 in there you get the bezier curve defined by the verts.
t is the location on the curve (between 0 and1) you want to get

then you get a return value where you are on the curve (a new vert)

Sunjay03
22-May-10, 13:43
verts is a list of coordinates like [(1,4,3), (4,2,6),...]
nth is the number of the derivative one wants.
If you put 0 in there you get the bezier curve defined by the verts.
t is the location on the curve (between 0 and1) you want to get

then you get a return value where you are on the curve (a new vert)

I see. I am more in the drawing stage of the curves right now, and from what I understand, your function gives me the point on the graph closest to the verts?

Could you maybe edit my blend and re-upload it? You seem to know a lot more about what you are talking about than I do. ;)
http://www.pasteall.org/blend/2758

-Sunjay03

Abidos
22-May-10, 13:55
Well, I am not an expert in BGE and I dont know why but for some reason I have problems with Rasterizer module (I refer to posting #6 above:


Compiled with Python version 2.6.2.
Checking for installed Python... got it!
Traceback (most recent call last):
File "Text", line 37, in <module>
ImportError: No module named RasterizerFortunately, I was able to get a copy of your calculated data, then I put them into EXCEL and I think the graph shows that they are correctly calculated:

http://s2.sigmirror.com/files/77670_lrcgj/2010-05-22_215414.jpg

Therefore, you'd need to check the drawing function performance... I think ;)

Regards,

testscreenings
22-May-10, 14:14
i tried a little bit.
but had no succes, i have no expirence with 2.49.
so i had problems with the loading of modules. behaved differently
sorry

Sunjay03
22-May-10, 14:44
@Abidos: Are you in 2.49? Also, make sure you aren't just hitting Alt+P, but using p to start the game engine instead.

There you will see my failures... ;)

-Sunjay03

Abidos
22-May-10, 14:50
Yes, I've got it. i dont know why it is shown linear in the game engine though

Sunjay03
22-May-10, 14:59
Yes, I've got it. i dont know why it is shown linear in the game engine though

Perhaps the curve is so subtle that it doesn't show up...?

-Sunjay03

EDIT: Never mind... I really don't know... :(

Abidos
22-May-10, 15:04
My ONE-and-ONLY idea at the moment is that you check (somehow) if in the game only the 3 initial points are shown, so that they are prime lines rather than cosine curves....

Sunjay03
22-May-10, 15:49
My ONE-and-ONLY idea at the moment is that you check (somehow) if in the game only the 3 initial points are shown, so that they are prime lines rather than cosine curves....

prime lines? You mean if I were to just change them to a single line between each point? That does work...I would just like curves.