Changing co-ordinate system

2019-08-19 05:42发布

问题:

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?

回答1:

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!