public void KtLine2DTest1() { var start = new KtPoint2D(1, 2); var end = new KtPoint2D(3, 4); var line = new KtLine2D(1, 2, 3, 4); Assert.AreEqual(line.Start, start); Assert.AreEqual(line.End, end); }
public bool HasThePoint(KtPoint2D point) { var parameter = (point - Start).ToKtVector2D() / Vector; if (parameter is null) { return(false); } return(ValidParametricValue(parameter)); }
public KtPoint2D Centroid() { var Atotal = 0d; var Ad = new KtPoint2D(); foreach (var r in _regions) { Atotal += r.Area(); Ad += r.AreaCentroid(); } return(Ad / Atotal); }
public void LineIntersectionTests() { var watch = new Stopwatch(); var intersection = new KtPoint2D(5, 7); var line1 = new KtLine2D((1, 1), (3, 4)); var line2 = new KtLine2D((9, 3), (7, 5)); var line3 = new KtLine2D((3, 1), (5, 4)); var ray1 = new KtRay2D((9, 3), (7, 5)); var ray2 = new KtRay2D((7, 5), (9, 3)); var ray3 = new KtRay2D((1, 1), (3, 4)); var ray4 = new KtRay2D((3, 4), (1, 1)); var segment1 = new KtSegment2D((7, 5), (9, 3)); var segment2 = new KtSegment2D((4, 8), (9, 3)); var segment3 = new KtSegment2D((1, 1), (3, 4)); var segment4 = new KtSegment2D((1, 1), (7, 10)); watch.Start(); for (int i = 0; i < 100001; i++) { // line to line with intersection #pragma warning disable CS0618 // Type or member is obsolete Assert.AreEqual(line1.IntersectWith3(line2), intersection); //line to line parallel Assert.IsNull(line1.IntersectWith3(line3)); // line to ray with intersection Assert.AreEqual(line1.IntersectWith3(ray1), new KtPoint2D(5, 7)); // line to ray no Intersection Assert.AreEqual(line1.IntersectWith3(ray2), null); //line to segment no intersection Assert.AreEqual(line1.IntersectWith3(segment1), null); //line to segment with intersection Assert.AreEqual(line1.IntersectWith3(segment2), new KtPoint2D(5, 7)); //ray to ray with intersection Assert.AreEqual(ray3.IntersectWith3(ray1), new KtPoint2D(5, 7)); //ray to ray no intersection Assert.AreEqual(ray3.IntersectWith3(ray2), null); Assert.AreEqual(ray4.IntersectWith3(ray2), null); Assert.AreEqual(ray4.IntersectWith3(ray1), null); //ray to segment with intersection Assert.AreEqual(ray3.IntersectWith3(segment2), new KtPoint2D(5, 7)); //ray to segment no intersection Assert.AreEqual(ray3.IntersectWith3(segment1), null); Assert.AreEqual(ray4.IntersectWith3(segment2), null); //segment to segment no intersection Assert.AreEqual(segment3.IntersectWith3(segment1), null); //segement to segment with intersection Assert.AreEqual(segment4.IntersectWith3(segment2), new KtPoint2D(5, 7)); #pragma warning restore CS0618 // Type or member is obsolete } watch.Stop(); System.Console.WriteLine(((double)watch.ElapsedMilliseconds).ToString()); //System.Windows.Forms.MessageBox.Show(watch.ElapsedMilliseconds.ToString()); }
public KtPoint2D IntersectWith3(KtLine2DBase line2) { var determinant = Vector.Cross(line2.Vector); if (determinant.NearZero(5)) { return(null); } var(a1, a2, b1, b2, c1, c2) = (A, line2.A, B, line2.B, C, line2.C); var p = new KtPoint2D((b2 * c1) - (b1 * c2), (c2 * a1) - (c1 * a2)) / determinant; return(Bounds(p) && line2.Bounds(p) ? p : null); }
internal override bool Bounds(KtPoint2D p) { if (p is null) { return(false); } if (p == Start) { return(true); } var directionOfPoint = (p - Start).ToKtVector2D().Direction; var directionOfLine = Vector.Direction; return(directionOfPoint == directionOfLine); }
public static KtRegion Rectangle(KtPoint2D center, double width, double height, bool solid = true) { if (center == null) { center = new KtPoint2D(); } var corners = new List <KtPoint2D> { center + new KtPoint2D(-width / 2.0, -height / 2.0), center + new KtPoint2D(-width / 2.0, height / 2.0), center + new KtPoint2D(width / 2.0, height / 2.0), center + new KtPoint2D(width / 2.0, -height / 2.0) }; return(solid ? (KtRegion) new KtSolidRegion(corners) : new KtHollowRegion(corners)); }
protected KtLine2DBase(KtPoint2D start, KtPoint2D end) { if (start == end) { throw new ArgumentException("Cannot create zero length line where start and end are equal"); } if (start == null || end == null) { throw new ArgumentNullException($"Cannot create a 2D line with points ({start}) and ({end}) where one is null"); } Start = start; End = end; Vector = (end - start).ToKtVector2D(); A = Vector.Y; B = -Vector.X; C = Start.ToKtVector2D().Cross(Vector); }
public static KtRegion RegularPolygon(KtPoint2D center, int cornerCount, double circumRadius, bool solid) { if (circumRadius.NearZero(5) || cornerCount < 2) { return(null); } if (center == null) { center = new KtPoint2D(); } var corners = new List <KtPoint2D>(); var intAngle = Cycle.Create(1) / cornerCount; for (var i = 0; i < cornerCount; i++) { var corner = center + (new KtVector2D(intAngle * i) * circumRadius).ToKtPoint2D(); corners.Add(corner); } return(solid ? (KtRegion) new KtSolidRegion(corners) : new KtHollowRegion(corners)); }
public KtSegment2D(KtPoint2D start, KtPoint2D end) : base(start, end) { }
public KtLine2D(KtPoint2D start, KtPoint2D end) : base(start, end) { }
internal static bool LiesOnSegments(this KtPoint2D point, IEnumerable <KtSegment2D> segments) => segments.Any(segment => segment.HasThePoint(point));
internal abstract bool Bounds(KtPoint2D p);
internal static bool Inscribes(this CircularStack <KtPoint2D> points, KtPoint2D point) => !points.EdgesHasThePoint(point) && !points.InteriorAngle(point).IsZero;
public KtPolygon2D Translate(KtPoint2D point) => new(_regions.Select(reg => reg.Translate(point)).ToArray());
private static bool EdgesHasThePoint(this CircularStack <KtPoint2D> points, KtPoint2D point) => points.ToEdges().Any(edge => edge.HasThePoint(point));
public KtRay2D(KtPoint2D start, KtVector2D direction) : this(start, start + direction) { }
public double SubstitutePoint(KtPoint2D p) => (A * p.X) + (B * p.Y) - C;
public double Distance(KtPoint2D p) => SubstitutePoint(p) / Vector.Magnitude;
internal override bool Bounds(KtPoint2D p) => p.X.IsWithin(Start.X, End.X) && p.Y.IsWithin(Start.Y, End.Y);
public static Angle TotalInternalAngle(this CircularStack <KtPoint2D> points, KtPoint2D point) => points.Aggregate(Degrees.Create(0), (total, c) => total + (c?.Next?.Value - point).ToKtVector2D().AngleBetween((c?.Value - point).ToKtVector2D()));
internal static bool Inscribes(this IEnumerable <KtPoint2D> points, KtPoint2D point) => points.ToCircularStack().Inscribes(point);
public static Angle TotalInternalAngle(this IEnumerable <KtPoint2D> points, KtPoint2D point) => points.ToCircularStack().TotalInternalAngle(point);
private static Angle InteriorAngle(this CircularStack <KtPoint2D> points, KtPoint2D point) => points.Aggregate(Degrees.Create(0.0), (total, c) => total + c.InteriorAngle(point));
public bool Inline(KtPoint2D p) => p != null && SubstitutePoint(p).NearZero() && Bounds(p);
private static Angle InteriorAngle(this CircularStackElement <KtPoint2D> c, KtPoint2D point) => (c?.Next?.Value - point).ToKtVector2D().AngleBetween((c?.Value - point).ToKtVector2D());
public KtRay2D(KtPoint2D start, KtPoint2D end) : base(start, end) { }