Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do the parameters to CGPathAddCurveToPoint mean?

I want to build a CGPathRef programatically based on the coordinates of the Sun at different points of the day. Calculating the points is not a problem, but I want to make a CGPathRef that is smooth and thought CGPathAddCurveToPoint would be appropriate.

I understand the path, transform, x and y parameters, but I'm not sure about the others. Per the Apple documentation they are the control points, and I'm guessing they are like you'd see in a vector drawing program where you can adjust the way the curve passes through the point.

My question is how to choose points that relate to my coordinates without knowing what those coordinates are ahead of time? I'm thinking maybe just subtract a set amount from each of the first control points and add the same amount to the second control points, but that sounds over simplified to me. Is there a standard method for generating control points that "make sense" for a smooth curve?

void CGPathAddCurveToPoint (
   CGMutablePathRef path,
   const CGAffineTransform *m,
   CGFloat cp1x,
   CGFloat cp1y,
   CGFloat cp2x,
   CGFloat cp2y,
   CGFloat x,
   CGFloat y
); 
like image 842
Steve Avatar asked Dec 13 '10 17:12

Steve


1 Answers

The extra points are the bezier control points for the curve out of the source (current) point and the curve into the target point (see http://en.wikipedia.org/wiki/Bézier_curve for a general explanation). The line currentX,currentY - cp1x,cp1y is the vector 'out' of the current point and cp2x,cp2y - x,y is the vector 'in' to the final point.

A reasonable way to produce a smooth curve from p1 to p2 (assuming 4 points p0,p1,p2,p3) is (pseudocode):

v = (strength of curve from 0.0 to 1.0)
cp1x = p1.x+v*(p1.x-p0.x)
cp1y = p1.y+v*(p1.y-p0.y)
cp2x = p2.x-v*(p3.x-p2.x)
cp2y = p2.y-v*(p3.y-p2.y)

For the starting point, set cp1x,cp1y to the starting x,y and for the ending point set cp2x,cp2y to the ending x,y.

NOTE: I've updated the answer to incorporate the comments from ughoavgfhw

like image 179
Denis Hennessy Avatar answered Nov 07 '22 02:11

Denis Hennessy