From 0e0922e236f15061f28bdc8753df673356fab566 Mon Sep 17 00:00:00 2001 From: That_One_Nerd Date: Mon, 28 Aug 2023 12:46:35 -0400 Subject: [PATCH] Pretty sure I'm done with triangle. Changelog next --- Changelog.md | 1 + Nerd_STF/Mathematics/Angle.cs | 8 ++++++-- Nerd_STF/Mathematics/Geometry/Line.cs | 3 ++- Nerd_STF/Mathematics/Geometry/Triangle.cs | 24 ++++++++++++++++++++++- 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/Changelog.md b/Changelog.md index ff896fc..5f10265 100644 --- a/Changelog.md +++ b/Changelog.md @@ -30,6 +30,7 @@ Here's the full changelog: + : IIndexRangeAll + : ISplittable + IWithinRange + + Angle + Slope + Line() + ToFloatArrayAll(params Line[]) diff --git a/Nerd_STF/Mathematics/Angle.cs b/Nerd_STF/Mathematics/Angle.cs index f9233c5..c97d038 100644 --- a/Nerd_STF/Mathematics/Angle.cs +++ b/Nerd_STF/Mathematics/Angle.cs @@ -52,11 +52,15 @@ public struct Angle : IAbsolute, IAverage, IClamp, ICloneab _ => throw new ArgumentException("Unknown type.", nameof(valueType)), }; - public static Angle FromVerts(Float3 endA, Float3 middleB, Float3 endC) + public static Angle FromLines(Line ab, Line bc) + { + if (ab.b != bc.a) throw new DisconnectedLinesException(ab, bc); + return FromPoints(ab.a, ab.b, bc.b); + } + public static Angle FromPoints(Float3 endA, Float3 middleB, Float3 endC) { endA -= middleB; endC -= middleB; - float dot = Float3.Dot(endA, endC); return Mathf.ArcCos(dot * endA.InverseMagnitude * endC.InverseMagnitude); } diff --git a/Nerd_STF/Mathematics/Geometry/Line.cs b/Nerd_STF/Mathematics/Geometry/Line.cs index 0e8971a..7125e24 100644 --- a/Nerd_STF/Mathematics/Geometry/Line.cs +++ b/Nerd_STF/Mathematics/Geometry/Line.cs @@ -16,6 +16,7 @@ public class Line : IAverage, IClosestTo, IContains, IEqua public static Line One => (Float3.Zero, Float3.One); public static Line Zero => (Float3.Zero, Float3.Zero); + public Angle Angle => Mathf.ArcTan(Slope); public float Length => (b - a).Magnitude; public Float3 Midpoint => (a + b) / 2; public float Slope => (b.y - a.y) / (b.x - a.x); @@ -253,5 +254,5 @@ public class Line : IAverage, IClosestTo, IContains, IEqua public static implicit operator Line(Fill fill) => new(fill); public static implicit operator Line(Fill fill) => new(fill); public static implicit operator Line(Fill fill) => new(fill); - public static implicit operator Line((Float3 a, Float3 b) tuple) => (tuple.a, tuple.b); + public static implicit operator Line((Float3 a, Float3 b) tuple) => new(tuple.a, tuple.b); } diff --git a/Nerd_STF/Mathematics/Geometry/Triangle.cs b/Nerd_STF/Mathematics/Geometry/Triangle.cs index 4a7941f..d25cc72 100644 --- a/Nerd_STF/Mathematics/Geometry/Triangle.cs +++ b/Nerd_STF/Mathematics/Geometry/Triangle.cs @@ -45,6 +45,10 @@ public class Triangle : IClosestTo, IContains, } } + public Angle AngleABC => Angle.FromPoints(a, b, c); + public Angle AngleBCA => Angle.FromPoints(b, c, a); + public Angle AngleCAB => Angle.FromPoints(c, a, b); + public Float3 a, b, c; public Triangle() : this(Float3.Zero, Float3.Zero, Float3.Zero) { } @@ -176,6 +180,24 @@ public class Triangle : IClosestTo, IContains, return result; } + public Float3 ClosestTo(Float3 point) + { + Float3 abClosest = AB.ClosestTo(point), + bcClosest = BC.ClosestTo(point), + caClosest = CA.ClosestTo(point); + + // Very inefficient way to select the closest point. + + float abDist = (abClosest - point).Magnitude, + bcDist = (bcClosest - point).Magnitude, + caDist = (caClosest - point).Magnitude; + + float min = Mathf.Min(abDist, bcDist, caDist); + if (abDist == min) return abClosest; + else if (bcDist == min) return bcClosest; + else return caClosest; + } + public bool Equals(Triangle? other) => other is not null && a == other.a && b == other.b && c == other.c; public override bool Equals(object? obj) @@ -191,7 +213,7 @@ public class Triangle : IClosestTo, IContains, Triangle pab = (point, a, b), pbc = (point, b, c), pca = (point, c, a); - return Mathf.Absolute(Area - (pab.Area + pbc.Area + pca.Area)) < 0.025f; + return Mathf.Absolute(Area - (pab.Area + pbc.Area + pca.Area)) < 0.05f; } public Float3[] GetAllVerts() => new[] { a, b, c };