Wpf PathGeometry FillContains未按预期工作
我有一个简单的矩形Wpf PathGeometry FillContains未按预期工作,wpf,hittest,pathgeometry,Wpf,Hittest,Pathgeometry,我有一个简单的矩形PathGeometry,想测试点是否在PathGeometry内。最明显的方法是调用FillContains,但它不能按预期工作。还有一个重载函数,它具有一个公差参数,尽管通过将公差调整为高值FillContains可能会返回true,但由于给定的公差,在其他几何图形上调用FillContains也可能返回true 因此,我编写了这个扩展方法,以便为这个特定的矩形路径创建一个正确的FillContains: public static bool Contains(th
PathGeometry
,想测试点是否在PathGeometry
内。最明显的方法是调用FillContains
,但它不能按预期工作。还有一个重载函数,它具有一个公差
参数,尽管通过将公差
调整为高值FillContains
可能会返回true
,但由于给定的公差,在其他几何图形上调用FillContains
也可能返回true
因此,我编写了这个扩展方法,以便为这个特定的矩形路径创建一个正确的FillContains
:
public static bool Contains(this PathGeometry geo, Point pt)
{
var match = System.Text.RegularExpressions.Regex.Match(geo.Figures.ToString(), @"M(\d*.\d*),(\d*.\d*)L(\d*.\d*),(\d*.\d*) (\d*.\d*),(\d*.\d*) (\d*.\d*),(\d*.\d*)z");
float ulx = float.Parse(match.Groups[1].Value);
float uly = float.Parse(match.Groups[2].Value);
float urx = float.Parse(match.Groups[3].Value);
float ury = float.Parse(match.Groups[4].Value);
float lrx = float.Parse(match.Groups[5].Value);
float lry = float.Parse(match.Groups[6].Value);
float llx = float.Parse(match.Groups[7].Value);
float lly = float.Parse(match.Groups[8].Value);
Rect rect = new Rect(ulx, uly, urx - ulx, lly - uly);
return rect.Contains(pt);
}
以及样本的结果:
// Point: {188.981887817383,507.910125732422}
// Region: M188.759994506836,501.910003662109L216.580001831055,501.910003662109 216.580001831055,511.910003662109 188.759994506836,511.910003662109z
// returns false
var test1 = region.FillContains(pt);
// returns true
var test2 = region.Contains(pt);
既然我有很多这样的PathGemoetry
对象,那么有没有更好的方法来实现更快的命中测试,或者在使用FillContains
导致意外结果时,我遗漏了什么
编辑
刚刚注意到我的PathGeometry
应用了一个变换,导致点不适合内部
在命中测试中,我使用此选项绕过了转换
,从而修复了此问题:
PathGeometry.Parse(region.Figures.ToString()).FillContains(pt)
你试过了吗
VisualTreeHelper.HitTest(Visual reference, Point point)
FillContains可以为我提供测试数据。如何从区域数据实际创建PathGeometry?在XAML中,它将生成一个StreamGeometry,而不是PathGeometry。@Clemens,愚蠢的错误,有一个
变换
应用于该几何体,请看我的编辑器,也许效率更高:region.FillContains(region.Transform.Transform(pt))
。