Search code examples
iosalgorithmgraphics3dcore-animation

2D transforms in ellipse drawing algorithm


I'm trying to draw a 3D ellipse with a 2.5D graphics engine (Core Animation layers) which allow me to only compose my ellipse with line segments that must be moved into place using rotations and translations. I'm having trouble with the order of operations and can't get it to draw properly. Any graphics gurus or game programmers out there who can help me?

Here's an image describing my current approach:

enter image description here

For each segment in the ellipse polygon, I'm first creating a line segment with the correct length, then translating it to the point P1, then rotating the point by the PI/2+theta, but this is clearly not working.

It's been 10 years since I took a graphics class in university, can someone please jog my memory as to what I'm doing wrong?


Solution

  • I accidentally found the solution while cleaning up my code to post here!

    The correct procedure is:

    CATransform3D transform = CATransform3DIdentity;
    transform = CATransform3DTranslate(transform, -size.width/2.0, 0, 0);
    transform = CATransform3DTranslate(transform, point.x, 0, point.y);
    transform = CATransform3DRotate(transform, M_PI_2+angle, 0, 1, 0);
    transform = CATransform3DTranslate(transform, -size.width/2.0, 0, 0);