I am writing a custom control which draws polygons.
I use matrix calculations to scale and shear the polygons so that they fit the control.
I need to know if the mouse has been clicked inside one of the polygons, so I am using ray casting.
This all seems to work fine individually, however I am now encountering an issue with retrieving the mouse coordinates relative to the display matrix im using.
I use the following code:
// takes the graphics matrix used to draw the polygons
Matrix mx = currentMatrixTransform;
// inverts it
mx.Invert();
// mouse position
Point[] pa = new Point[] { new Point(e.X, e.Y) };
// uses it to transform the current mouse position
mx.TransformPoints(pa);
return pa[0];
now this works for every other set of coordinates, by that I mean that one pair of mouse coordinates appears to give the correct values as if it has been through the matrix, but the one beside it gives a value as if it has not been through the matrix, below is an output of the mouse values received when moving down the control.
{X=51,Y=75}
{X=167,Y=251}
{X=52,Y=77}
{X=166,Y=254}
{X=52,Y=78}
{X=166,Y=258}
{X=52,Y=79}
{X=166,Y=261}
{X=52,Y=80}
{X=165,Y=265}
{X=52,Y=81}
{X=165,Y=268}
if it helps the matrix used to draw the polygons is
Matrix trans = new Matrix();
trans.Scale(scaleWidth, scaleHeight);
trans.Shear(italicFactor, 0.0F, MatrixOrder.Append);
trans.Translate(offsetX, offsetY, MatrixOrder.Append);
e.Graphics.Transform = trans;
currentMatrixTransform = e.Graphics.Transform;
Thanks in advance
You are inverting your matrix each time you call it.
Matrix is a class, meaning that by performing
Invert()onmx, you are also performing it oncurrentMatrixTransform.You can either copy the matrix using
Clone()and then invert the clone,or you can perform
Invert()once again after you’ve transformed the pointpa.A second invert example:
A clone example: