无法使用旋转,平移和矩阵求逆返回正确的坐标

兰伯特

新成员
已加入
2018年12月24日
留言内容
1
地点
荷兰阿尔梅勒
编程经验
10+
您好,我想做的是将图像绕其中心旋转并返回鼠标指向的点的坐标。下一步将是放大图像中的任何点。旋转图像以使对象与Y轴对齐。
我正在使用GraphicsPath实现此目的。当我将光标移动到旋转图像的边缘之一时,坐标会发生偏移。我发现角度和偏移量之间没有关系。
这是一个错误,还是我做错了一个假设。 Matrix IsInvertible属性为true;

代码:
        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));
        }

任何帮助将不胜感激。
谢谢
兰伯·纳彭
 
最佳 底部