Search code examples
mathgeometryc++-cligdi+gdi

Changing co-ordinate system


alt text
(source: roughnotebook at sites.google.com)

I need to switch from the XY co-ordinate system shown above to the X'Y' co-ordinate system using System::Drawing::Drawing2D (i.e. GDI+). This is what I have in mind:

float rotation =                    // +90 below is because AB is the new vertical...
    Math::Atan2(pB.Y - pA.Y, pB.X - pA.X) * 180.0 / Math::PI + 90.0f;

Matrix m;
m.Translate(pA.X, pA.Y);
m.Rotate(rotation);
m.Invert();

array<PointF> points = gcnew array<PointF>{ pC };
m.TransformPoints(points);

Is there a way to do this while minimizing rounding errors? Can I avoid the Atan2 (or other inverse trigonometric function) call here?


Solution

  • I'm not familiar with gdi+, but in principle you can do this without inverse trig or operator inversion. (I say "operator inversion" instead of "matrix inversion" because this Matrix doesn't look like a matrix to me.)

    First, you should be able to avoid the matrix inversion by changing the way you define the operator. Here's a blind stab at it:

    Matrix m;
    m.Rotate(-rotation);
    m.Translate(-pA.X, -pA.Y);
    

    Now for the rotation itself, the usual way to do it is with a matrix that looks like this:

    cos(theta)  -sin(theta)
    sin(theta)   cos(theta)
    

    and you're calculating theta using atan(y/x). But if what you want are the sin and cos, you can just normalize x and y and use them directly:

    x  -y
    y   x
    

    No atan needed. In fact, no trig at all!