1

我有几个随机大小和应用 RenderTransform 角度的画布对象。在画布里面我有几何对象(线)。

我正在使用宽度和高度绑定来填充父画布对象的路径。

在此处输入图像描述

<Path x:Name="gem2_Copy" Stretch="Fill" Stroke="Black" Opacity="0.345" RenderTransformOrigin="0.5,0.5" Width="{Binding ActualWidth, ElementName=canvas}" Height="{Binding ActualHeight, ElementName=canvas}">
            <Path.Data>
                 <PathGeometry>
                    <PathFigure IsClosed="True" IsFilled="True">
                        <LineSegment  Point="0,0">

                        </LineSegment>  
                        <LineSegment  Point="5,5">

                        </LineSegment>  
                        <LineSegment  Point="0,5">

                        </LineSegment>      
                    </PathFigure>
                </PathGeometry>     
            </Path.Data>

            </Path>

如何检查 PathFigure 是否与其他路径几何相交?我有算法来检查线是否相交但我无法应用它,因为路径点线点总是:0,0 5,5 0,5

我需要全球坐标。如何将路径点转换为全局坐标并考虑到渲染变换角度发生变化以及路径宽度和高度发生变化?

foreach (var figure in geom.Figures) 
{
     foreach (LineSegment segment in figure.Segments)
     { 
        // I have here 0,0;5,5;0,5
     }
}
4

1 回答 1

1

已经找到如何做主要部分:

    // Get scale transformation (find scale factor)
    var w = (gem2_Copy.ActualWidth / gem2_Copy.Data.Bounds.Width);
    var h = (gem2_Copy.ActualHeight / gem2_Copy.Data.Bounds.Height);
    var transform = new ScaleTransform(w, h);

    foreach (var figure in geom.Figures)
    {
      foreach (LineSegment segment in figure.Segments)
      {
        // Use scale transformation to change geometry points position.
        var scaled=transform.Transform(segment.Point);

        // Translate point to screen coordinates (including rotation)
        var onScreen = canvas.TranslatePoint(scaled, root);

        // Use line intersection formula
      }
    }

可以找到交集的代码:

public static Vector? Intersects(Point a1, Point a2, Point b1, Point b2)
    {
        Vector b = a2 - a1;
        Vector d = b2 - b1;
        var bDotDPerp = b.X * d.Y - b.Y * d.X;

        if (bDotDPerp == 0)
            return null;

        Vector c = b1 - a1;
        var t = (c.X * d.Y - c.Y * d.X) / bDotDPerp;
        if (t < 0 || t > 1)
        {
            return null;
        }

        var u = (c.X * b.Y - c.Y * b.X) / bDotDPerp;
        if (u < 0 || u > 1)
        {
            return null;
        }

        return new Vector(a1.X, a1.Y) + t * b;
    }
于 2013-04-28T17:53:25.977 回答