本文介绍了如何做好GDI +与实际测量的形状(英寸)旋转的形状命中测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
由于我有一个包含许多形状的画布,可以说矩形现在。
Given I have a canvas that contains many shapes, lets say rectangles for now.
每个形状都有一个位置(英寸),尺寸(英寸)和旋转角度(度)。
Each shape has a location (inches), size(inches) and rotation angle(degrees).
当鼠标点击事件画布里面发生在像素位置(X,Y)。
When a mouse click event happen inside the canvas for a location (x,y) in pixels.
我要检查,如果点击鼠标的位置是在/特定形状中,考虑到旋转角度和测量单位转换。
I want to check if the clicked mouse position is inside/within a specific shape, considering the rotation angle and measurement unit conversion.
你能帮忙吗?
推荐答案
我找到了答案(我要人测量转换为像素,以确保它会正确计算):
I found the answer (I have to convert al measurements to pixels to make sure it will calculate correctly):
public static bool HitTest(Rectangle bounds, float angle, Point location)
{
if (angle == 0) return bounds.Contains(location);
using (Matrix matrix = new Matrix())
{
matrix.RotateAt(angle, Center(bounds));
using (GraphicsPath path = new GraphicsPath())
{
path.AddRectangle(bounds);
path.Transform(matrix);
return path.IsVisible(location.X, location.Y);
}
}
}
这篇关于如何做好GDI +与实际测量的形状(英寸)旋转的形状命中测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!