11

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
); 
Steve
  • 6,332
  • 11
  • 41
  • 53
  • 1
    Since I found this question while having this problem: If you are used to vector graphics software such as Illustrator, the meaning of control points is slightly different from what is used in Core Graphics. If you have a path [(a1,p1,b1),(a2,p2,b2),...] where pi are the points and ai are the control points visually connected to them, the equivalent path in Core Graphics would start with [(b1,p2,a2),...] (after a moveToPoint for p1). I hope this is helpful to someone. – mrueg Oct 27 '13 at 15:40

1 Answers1

7

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

Wayne Hartman
  • 18,369
  • 7
  • 84
  • 116
Denis Hennessy
  • 7,243
  • 4
  • 25
  • 31
  • 2
    Denis is write about them being control points, but wrong about their usage. cp1x,cp1y gives the vector OUT of the CURRENT point, and cp2x,xp2y gives the vector INTO the point x,y. Also, using the last and next points would probably give you straight lines. Sorry, but I can't tell you how you should actually calculate the points, but I can suggest trying CGPathAddArcToPoint instead. – ughoavgfhw Dec 13 '10 at 23:27