views:

66

answers:

1

alt text

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?

+3  A: 

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!

Beta
I assume you mean normalize (pB-pA), not normalize x and y.
BlueRaja - Danny Pflughoeft
@Beta, But normalizing x and y is indirect trig, isn't it?
Vulcan Eager
@Vulcan: No; if `v=(pB-pA)`, just divide `v.x` and `v.y` by the length of v (`v.length = sqrt(v.x^2 + v.y^2)`)
BlueRaja - Danny Pflughoeft
@BlueRaja - Danny Pflughoeft: yes, I spoke carelessly, I should have said something like "normalize (pB-pA)" or "normalize `[x y]`".
Beta
Thanks. Answer accepted. However, I am replacing the trig functions with a square root operation (calculating the length) which is equally expensive. Is it possible to avoid the square root?
Vulcan Eager
@Vulcan Eager: No. If (pB-pA) is, say, [1 1], then when you transform a simple point like [0 1] the result will involve square roots. You really can't get around it.
Beta