Curves of all types… it's not what you're thinking

Forums Programming Programming – Everything else Curves of all types… it's not what you're thinking

This topic contains 86 replies, has 44 voices, and was last updated by  Scott 1 year, 5 months ago.

Viewing 25 posts - 1 through 25 (of 87 total)
Author Posts
Author Posts
March 5, 2010 at 10:20 pm #220226


A while back I wrote a post helping Decept with an idea:

Well I’ve cleaned up that code and I’ve implemented a bezier and lagrange curve using a more extendable form of deCasteljau and Aitken algorithm in order to produce any order curve than just cubic and quadratic.

If you look into CCLines.m you’ll find the implementations ccpAitkenLagrange and ccpDeCasteljauBezier.

But generally when we need curves we really only need cubic and quadratic forms of either bezier or lagrange curves.

Sometimes it’s hard to really picture how the two curves behave so I’ve whipped up a quick demo code to try out so you can get to know them a bit better.

Curves Demo

March 6, 2010 at 12:01 am #275573


Interesting. Downloading it now. Also, lol at the subject line.

March 6, 2010 at 12:11 am #275574


Excellent demo, I especially like the cubic lagrange curve because the points of the curve move through the control points. THAT is VERY useful if you want a smooth curve to go through predefined intermediate points (e.g. making a homing missile avoid obstacles).

Thanks Lam.

March 6, 2010 at 4:34 am #275575


Yeah I agree with mhussa, the lagrange curve is invaluable. Nice work on a great contribution.

March 6, 2010 at 4:58 am #275576


Excellent work. Wish I had that ccpCubicFromQuadBezier function a few days ago, I just wrote my own. Thanks for sharing!

March 6, 2010 at 3:07 pm #275577


Thanks a million for sharing this!

It works perfectly, although I moved the ccpDeCasteljauBezier into the header file and used it directly with an arbitary number of points like: ccpDeCasteljauBezier(newPoints, newCount, originalPoints, originalCount);


March 6, 2010 at 7:49 pm #275578


this looks great. thanks so much!

If the starting point and end point of the lines were the same point, does anyone know how to test if a point falls inside the closed area of the curve?

That’d make it easy to do collision testing on smooth closed paths like in soosiz:


Regardless – Thanks!

March 6, 2010 at 8:37 pm #275579


Thanks everybody for all your comments!


I think a solution for your problem stems from the Jordan Curve theorem.

You have two points A and B. A is your point that you need to check if it’s inside a closed curve. B is an arbitrary point that you know is outside of your closed curve (this is easy if you choose a point far enough).

You then loop through your set of points that make up the curve and do an intersection test with the AB segment.

Count up the intersections and then we have the solution:

if the intersections are:

  • odd, then your point is inside the curve
  • even, then the point is outside the curve


The next biggest problem is to speed up the intersection loop test by storing your set of curve points in some efficient data structure so that you don’t have to loop through every segment of the curve to find the intersection.


Oh yeah, whoops, totally move that function to the header so you can access it.

March 6, 2010 at 9:10 pm #275580


Great demos. I like lagrange curves, put the points where you want the curve to curve too. FTW.

Beziers have always annoyed me, at least when coding them. Trial and error.

April 17, 2010 at 2:11 am #275581



I just found this post and it is amazing! The Langrange curves solve almost all of my problem except for one thing.

I want to use this to generate smooth curve points based on the user’s dragging their finger on the screen. Is there a good way to determine what the control points should be based on the touch events data rather than guesstimating? this is for both the cubic and quad versions.

One other thing, this links in the post you refer to at the top ( don’t seems to be there anymore.

Finally the Curves Demo Rocks!!!

thanks so much


April 20, 2010 at 2:11 am #275582


This is really fantastic stuff. Love the demo.

Now, how to figure out how to move something along a Lagrange curve at some given speed X.

August 24, 2010 at 8:58 pm #275583


Lam, you are a life saver!

October 6, 2010 at 3:56 am #275584


Someone emailed me a reply a few days ago about the lagrange curve, which gave me an excuse to dust off the old curves demo code.

I’ve now introduced a way to create smooth lines with any choice of width thickness since I remembered someone asking about that sometime ago.

Here’s the latest update of the curves demo:

Oh and sorry for not replying so soon, but if anyone has a question just PM me or email me and I’ll try to help as much as I can.

October 6, 2010 at 4:59 am #275585


Are you using this on an iphone-4G, one thing you might want to try to combine, is using the render texture test – to draw an – anti-aliased line using a brush.png texture.

Since normally people will want the nice curved line showing up anti-aliased.

November 11, 2010 at 2:13 am #275586


Thanks a lot for this demo…. I’m a new bee in cocos2d world. And your demo really help me out..

Can you please guide me how can I move the curve to its default position when ever the touches ended method is called?

January 16, 2011 at 9:53 am #275587


On a Retina display I get the curve at x/2 and y/2.

Any ideas how to make the curve show up nicely on both a normal and a Retina display?

January 29, 2011 at 12:33 am #275588



I think it probably has something to do with me not properly supporting retina display =/

I’ll read up on that and see what happens.

January 29, 2011 at 8:17 pm #275589


Sorry about the extremely late reply. *scratches head

I’ve now placed a new demo on dropbox:

Unfortunately I’ve run out-a space so dropbox won’t sync that file! You don’t have to but if you want to help me out just click on the link below and try out dropbox (it’s great!). I can get a teeny bit more space and the file will sync.

Anyway! That’s not the cool thing thing I wanted you guys to try out.

The curve demo is now retina supported!

But that’s not the only thing… I’ve also started getting bored looking at simple lines and introduced a new feature.

Varying widths!

You can now create your own functions or use a few that I created to make interesting lines that vary the width across a curve.

A curve with a width of 10 …. boring…

a width of 20 … ho hum…

How about a curve that tapers off at the ends!

Or a fancy ribbon!

Well try it out and maybe it’ll spark some game ideas. (Oh and I’m always up for suggestions and improvements for any of my demos)

January 29, 2011 at 8:23 pm #275590


it wont unzip. it just keeps going between being a .zip and a .cpgz :/

January 29, 2011 at 8:30 pm #275591


ugh…. strange

I thought even though i don’t have any space left dropbox would at least let others download. hmm

EDIT* ok i got it working!

January 29, 2011 at 8:56 pm #275592


i was able to download it, but it wouldn’t properly unzip i meant

January 29, 2011 at 9:03 pm #275593


Hi !

I just looked at your demo, it’s really cool !

I have a question : Would it be difficult to make a method to querry the line along its length ?

(Maybe I’m not phrasing this right).

I would like to use a lagrange curve as a path for a position animation.

Let’s say we define a one dimensional axis along the curve itself (that would represent time),

we’d set intermediate positions (for keyframes for example),

and if we could get (x,y) for dt, then it would be quite easy to mix this with

actions, probably by making a new action that takes a series of points and a duration.

see what I mean ? :)

January 29, 2011 at 9:48 pm #275594



Yeah that problem was an issue with me not having enough space on dropbox. I just deleted a few old examples. You should try re-downloading again.


Yeah i think I get what you’re saying. It is very possible to query a position of a point on the line based on it’s length. It’s actually very easy to implement CCActions using lagrange in exactly the same way as CCBezierTo kind of handles finding a point of the line based on time.

If I have time tonight maybe I’ll add another demo screen showing an example of a ‘CCActionLagrangeTo/CCActionLagrangeBy’ action.

January 29, 2011 at 10:41 pm #275595


@Lam : awesome !

January 30, 2011 at 3:59 am #275596


Ok I’ve quickly added an FRActionLagrangeTo into the demo as well. You can drag the points around and when you click on the cocos2d icon, it will move along the line.

The link is still the same but I’ll just paste it again.

Viewing 25 posts - 1 through 25 (of 87 total)

You must be logged in to reply to this topic.