Unable to return correct Coordinates using Rotation, Translate and Matrix invert

LambertK

New member
Joined
Dec 24, 2018
Messages
1
Location
Almere, Netherlands
Programming Experience
10+
Hello, what I am trying to do is rotate an image round its center and return the coordinate of the point pointed to by the mouse. The next step would be to Zoom in on any point in the image. The image is rotated to align an object to the Y-Axis.
I am using GraphicsPath to achieve this. When I move the cursor to one of the edges of the rotated image the coordinates have an offset. I have found no relation between angle and offset.
Is this a bug, or am I making a wrong assumption. The Matrix IsInvertible property is true;

The code:
        public PointF CalcPointA(float orX, float orY, int nwW, int nwH, float sin, float cos, PointF p, int OrigWidth = 0, int OrigHeight = 0)
        {
            System.Drawing.Drawing2D.Matrix m1 = new System.Drawing.Drawing2D.Matrix();
            GraphicsPath gpt = new GraphicsPath();
            m1.RotateAt(angle, new PointF(Origim.Width / 2, Origim.Height / 2));
            gpt.AddRectangle(new RectangleF(0, 0, Origim.Width, Origim.Height));       // The size of the original image
            gpt.Transform(m1);
            PointF[] pts = gpt.PathPoints;              //Get the PathPoints to calculate the Translation to Align with Axes
            float x11 = ShiftX(pts);                     // calculate X translation to shift the rotated image to the Y-Axis
            float y11 = ShiftY(pts);                     // Calculate Y translation to shift the rotated image to the X-Axis
            m1.Translate(x11, y11);
            gpt.Transform(m1);
            pts = gpt.PathPoints;                       // Here I can check the Image is touching X and Y Axes
            gpt.AddRectangle(new RectangleF(p.X * Origim1.Width / Width, p.Y * Origim1.Height / Height, 10, 10));          // Point p is the mouse coordinate, translated to Image coordinates, since you can't add a PointF to the graphicsPath, I add a small rectangle
            // Origim1 is the rotated Image as a Bitmap, I have verified the size of the resulting rcetangle matches Origim1.Size
            m1.Invert();                                   // invert, Rotate back to 0 degrees angle
            gpt.Transform(m1);
            pts = gpt.PathPoints;                       //Original image is back with original Size angle 0 PathPoints(0,0,Origim.Width,Origim.Height)
            return new PointF((pts[4].X), (pts[4].Y));
        }

Any help would be greatly appreciated.
Thanks
Lambert Knapen
 
Back
Top Bottom