2012-05-12 32 views
1

我正在寫一個繪製多邊形的自定義控件。 我使用矩陣計算來縮放和剪切多邊形,以使它們適合控件。C#矩陣給出了奇怪的鼠標座標

我需要知道鼠標是否已被點擊其中一個多邊形內,所以我正在使用光線投射。

這一切似乎單獨工作,但我現在遇到與檢索鼠標座標相對於顯示矩陣即時通訊使用問題。

我使用下面的代碼:

// 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 = ,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; 

在此先感謝

回答

2

則在每次調用時反相你的矩陣。 Matrix是一個類,這意味着通過執行Invert()mx,您也可以在currentMatrixTransform上執行它。

您可以使用Clone()複製矩陣,然後反轉克隆, 也可以再進行一次Invert()你已經改變了點pa後。

第二反相例如:

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

啊,感謝您的 – Reznoir