Skip to content

Commit 5bd263f

Browse files
committed
Draw directional arrows without trigonometry
1 parent cfaa9b8 commit 5bd263f

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

Nodify/Connections/BaseConnection.cs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -609,15 +609,22 @@ protected virtual void DrawDirectionalArrowsGeometry(StreamGeometryContext conte
609609

610610
protected virtual void DrawDirectionalArrowheadGeometry(StreamGeometryContext context, Vector direction, Point location)
611611
{
612-
double headWidth = ArrowSize.Width;
613-
double headHeight = ArrowSize.Height / 2;
614-
615-
double angle = Math.Atan2(direction.Y, direction.X);
616-
double sinT = Math.Sin(angle);
617-
double cosT = Math.Cos(angle);
618-
619-
var from = new Point(location.X + (headWidth * cosT - headHeight * sinT), location.Y + (headWidth * sinT + headHeight * cosT));
620-
var to = new Point(location.X + (headWidth * cosT + headHeight * sinT), location.Y - (headHeight * cosT - headWidth * sinT));
612+
var x = direction;
613+
x.Normalize();
614+
// If Normalization failed (e.g. because the vector is too small), just don't draw anything.
615+
#if !NETFRAMEWORK
616+
if (!double.IsFinite(x.X)) return;
617+
#else
618+
if (double.IsNaN(x.X) || double.IsInfinity(x.X)) return;
619+
#endif
620+
var y = new Vector(x.Y, -x.X);
621+
622+
var headWidth = ArrowSize.Width * x;
623+
var headHeight = ArrowSize.Height / 2 * y;
624+
var headMiddle = location + headWidth;
625+
626+
var from = headMiddle + headHeight;
627+
var to = headMiddle - headHeight;
621628

622629
context.BeginFigure(location, true, true);
623630
context.LineTo(from, true, true);

0 commit comments

Comments
 (0)