0

単純な長方形がPathGeometryあり、ポイントが の内側にあるかどうかをテストしたいPathGeometry。明白な方法は呼び出すことですFillContainsが、期待どおりに機能しません。パラメータを持つオーバーロード関数もありますが、を高い値にtolerance調整するとが返される場合がありますが、指定された許容誤差により、他のジオメトリの呼び出しでも が返される場合があります。toleranceFillContainstrueFillContainstrue

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)
4

1 に答える 1