mirror of
https://gitee.com/akwkevin/aistudio.-wpf.-diagram
synced 2026-04-12 20:26:35 +08:00
使用PointBase代替Point
This commit is contained in:
43
AIStudio.Wpf.DiagramDesigner/Geometry/LineBase.cs
Normal file
43
AIStudio.Wpf.DiagramDesigner/Geometry/LineBase.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
namespace AIStudio.Wpf.DiagramDesigner.Geometry
|
||||
{
|
||||
public struct LineBase
|
||||
{
|
||||
public LineBase(PointBase start, PointBase end)
|
||||
{
|
||||
Start = start;
|
||||
End = end;
|
||||
}
|
||||
|
||||
public PointBase Start { get; }
|
||||
public PointBase End { get; }
|
||||
|
||||
public PointBase GetIntersection(LineBase line)
|
||||
{
|
||||
var pt1Dir = new PointBase(End.X - Start.X, End.Y - Start.Y);
|
||||
var pt2Dir = new PointBase(line.End.X - line.Start.X, line.End.Y - line.Start.Y);
|
||||
var det = (pt1Dir.X * pt2Dir.Y) - (pt1Dir.Y * pt2Dir.X);
|
||||
var deltaPt = new PointBase(line.Start.X - Start.X, line.Start.Y - Start.Y);
|
||||
var alpha = (deltaPt.X * pt2Dir.Y) - (deltaPt.Y * pt2Dir.X);
|
||||
var beta = (deltaPt.X * pt1Dir.Y) - (deltaPt.Y * pt1Dir.X);
|
||||
|
||||
if (det == 0 || alpha * det < 0 || beta * det < 0)
|
||||
return PointBase.Empty;
|
||||
|
||||
if (det > 0)
|
||||
{
|
||||
if (alpha > det || beta > det)
|
||||
return PointBase.Empty;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
if (alpha < det || beta < det)
|
||||
return PointBase.Empty;
|
||||
}
|
||||
|
||||
return new PointBase(Start.X + (alpha * pt1Dir.X / det), Start.Y + (alpha * pt1Dir.Y / det));
|
||||
}
|
||||
|
||||
public override string ToString() => $"Line from {Start} to {End}";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user