我有几个随机大小的画布对象并应用了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
}
}
答案 0 :(得分: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;
}