単純な長方形がPathGeometry
あり、ポイントが の内側にあるかどうかをテストしたいPathGeometry
。明白な方法は呼び出すことですFillContains
が、期待どおりに機能しません。パラメータを持つオーバーロード関数もありますが、を高い値にtolerance
調整するとが返される場合がありますが、指定された許容誤差により、他のジオメトリの呼び出しでも が返される場合があります。tolerance
FillContains
true
FillContains
true
FillContains
したがって、この特定の長方形を修正するために、この拡張メソッドを作成しましたPathGemoetry
。
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
トランスフォームが適用されているため、ポイントが内側に収まらないことに気付きました。
Transform
これを使用してヒットテストをバイパスすることで修正しました:
PathGeometry.Parse(region.Figures.ToString()).FillContains(pt)