forked from Blazor-Diagrams/Blazor.Diagrams
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
72 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters