Skip to content

Commit

Permalink
feat: Ellipse shape
Browse files Browse the repository at this point in the history
  • Loading branch information
zHaytam committed Mar 14, 2021
1 parent 3c96748 commit c8d261d
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 6 deletions.
6 changes: 0 additions & 6 deletions samples/SharedDemo/Demos/Simple.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ protected override void OnInitialized()
var node1 = NewNode(50, 50);
var node2 = NewNode(300, 300);
diagram.Links.Add(new LinkModel(node1.GetPort(PortAlignment.Right), node2.GetPort(PortAlignment.Left)));
diagram.Links.Add(new LinkModel(node1, node2)
{
SourceMarker = LinkMarker.Arrow,
TargetMarker = LinkMarker.Arrow,
Segmentable = true
});
diagram.Nodes.Add(new[] { node1, node2, NewNode(300, 50) });
}

Expand Down
60 changes: 60 additions & 0 deletions src/Blazor.Diagrams.Core/Geometry/Ellipse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;

namespace Blazor.Diagrams.Core.Geometry
{
public class Ellipse : IShape
{
public Ellipse(double x, double y, double rx, double ry)
{
X = x;
Y = y;
Rx = rx;
Ry = ry;
}

public double X { get; }
public double Y { get; }
public double Rx { get; }
public double Ry { get; }

public IEnumerable<Point> GetIntersectionsWithLine(Line line)
{
var a1 = line.Start;
var a2 = line.End;
var dir = new Point(line.End.X - line.Start.X, line.End.Y - line.Start.Y);
var diff = a1.Substract(X, Y);
var mDir = new Point(dir.X / (Rx * Rx), dir.Y / (Ry * Ry));
var mDiff = new Point(diff.X / (Rx * Rx), diff.Y / (Ry * Ry));

var a = dir.Dot(mDir);
var b = dir.Dot(mDiff);
var c = diff.Dot(mDiff) - 1.0;
var d = b * b - a * c;

if (d > 0)
{
var root = Math.Sqrt(d);
var ta = (-b - root) / a;
var tb = (-b + root) / a;

if (ta >= 0 && 1 >= ta || tb >= 0 && 1 >= tb)
{
if (0 <= ta && ta <= 1)
yield return a1.Lerp(a2, ta);

if (0 <= tb && tb <= 1)
yield return a1.Lerp(a2, tb);
}
}
else
{
var t = -b / a;
if (0 <= t && t <= 1)
{
yield return a1.Lerp(a2, t);
}
}
}
}
}
4 changes: 4 additions & 0 deletions src/Blazor.Diagrams.Core/Geometry/Point.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public Point(double x, double y)
public double X { get; }
public double Y { get; }

public double Dot(Point other) => X * other.X + Y * other.Y;
public Point Lerp(Point other, double t)
=> new Point(X * (1.0 - t) + other.X * t, Y * (1.0 - t) + other.Y * t);

// Maybe just make Points mutable?
public Point Add(double value) => new Point(X + value, Y + value);
public Point Add(double x, double y) => new Point(X + x, Y + y);
Expand Down
8 changes: 8 additions & 0 deletions src/Blazor.Diagrams.Core/Geometry/Shapes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,13 @@ namespace Blazor.Diagrams.Core.Geometry
public static class Shapes
{
public static IShape Rectangle(NodeModel node) => new Rectangle(node.Position, node.Size!);

public static IShape Circle(NodeModel node)
{
var halfWidth = node.Size!.Width / 2;
var centerX = node.Position.X + halfWidth;
var centerY = node.Position.Y + node.Size!.Height / 2;
return new Ellipse(centerX, centerY, halfWidth, halfWidth);
}
}
}

0 comments on commit c8d261d

Please sign in to comment.