【问题标题】:C# Matrix gives weird mouse coordinatesC# Matrix 给出了奇怪的鼠标坐标
【发布时间】:2012-05-12 18:26:12
【问题描述】:

我正在编写一个绘制多边形的自定义控件。 我使用矩阵计算来缩放和剪切多边形,使它们适合控件。

我需要知道鼠标是否在其中一个多边形内被点击,所以我正在使用光线投射。

这一切似乎都可以单独工作,但是我现在遇到了一个问题,即检索相对于我正在使用的显示矩阵的鼠标坐标。

我使用以下代码:

// 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];

现在这适用于所有其他坐标集,我的意思是一对鼠标坐标似乎给出了正确的值,就好像它已经通过矩阵一样,但是它旁边的那个给出了一个值,就好像它有没有通过矩阵,下面是向下移动控件时收到的鼠标值的输出。

{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}

如果它有助于用于绘制多边形的矩阵是

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;

提前致谢

【问题讨论】:

    标签: c# matrix coordinates coordinate-transformation


    【解决方案1】:

    每次调用它时,您都在反转矩阵。 Matrix 是一个类,这意味着通过对mx 执行Invert(),您也在对currentMatrixTransform 执行它。

    您可以使用Clone() 复制矩阵,然后反转克隆, 或者您可以在转换点pa 后再次执行Invert()

    第二个反转示例:

    // 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);
    
    // inverts it back
    max.Invert();
    
    return pa[0];
    

    一个克隆示例:

    // takes the graphics matrix used to draw the polygons
    Matrix mx = currentMatrixTransform.Clone();
    
    // 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];
    

    【讨论】:

      猜你喜欢
      • 2011-05-13
      • 1970-01-01
      • 1970-01-01
      • 2020-10-07
      • 2020-05-15
      • 2021-07-02
      • 1970-01-01
      • 1970-01-01
      • 2013-04-03
      相关资源
      最近更新 更多