From 58ac9500688a6ca431b4a2a930585a602816bc7f Mon Sep 17 00:00:00 2001 From: That_One_Nerd Date: Tue, 22 Aug 2023 08:57:25 -0400 Subject: [PATCH] Some more triangle stuff (WIP) --- Nerd_STF/Mathematics/Abstract/IPolygon.cs | 3 ++ Nerd_STF/Mathematics/Geometry/Triangle.cs | 45 ++++++++++++++++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/Nerd_STF/Mathematics/Abstract/IPolygon.cs b/Nerd_STF/Mathematics/Abstract/IPolygon.cs index a665294..7283538 100644 --- a/Nerd_STF/Mathematics/Abstract/IPolygon.cs +++ b/Nerd_STF/Mathematics/Abstract/IPolygon.cs @@ -19,6 +19,9 @@ public interface IPolygon : IAverage, IEquatable, public static abstract T operator /(T poly, float scale); public static abstract T operator /(T poly, Float3 scale3); + public static abstract bool operator ==(T a, T b); + public static abstract bool operator !=(T a, T b); + public static abstract implicit operator T(Fill fill); public static abstract implicit operator T(Fill fill); public static abstract implicit operator T(Fill fill); diff --git a/Nerd_STF/Mathematics/Geometry/Triangle.cs b/Nerd_STF/Mathematics/Geometry/Triangle.cs index 16714d8..2af07f7 100644 --- a/Nerd_STF/Mathematics/Geometry/Triangle.cs +++ b/Nerd_STF/Mathematics/Geometry/Triangle.cs @@ -8,7 +8,7 @@ public class Triangle : IClosestTo, IContains, { get // Heron's Formula { - float a = AB.Length, b = AC.Length, c = BC.Length, + float a = AB.Length, b = BC.Length, c = CA.Length, s = (a + b + c) / 2; return Mathf.Sqrt(s * (s - a) * (s - b) * (s - c)); } @@ -77,6 +77,7 @@ public class Triangle : IClosestTo, IContains, } public Triangle(Fill fill) : this(fill(0), fill(1), fill(2)) { } public Triangle(Fill fill) : this(fill(0), fill(1), fill(2)) { } + public Triangle(Fill fill) : this(fill(0), fill(1), fill(2)) { } public Triangle(Fill fill) : this(fill(0), fill(1), fill(2), fill(3), fill(4), fill(5), fill(6), fill(7), fill(8)) { } public Triangle(Fill fill) : this(fill(0), fill(1), fill(2), fill(3), fill(4), @@ -134,5 +135,47 @@ public class Triangle : IClosestTo, IContains, } } + public bool Equals(Triangle? other) => other is not null && a == other.a + && b == other.b && c == other.c; + public override bool Equals(object? obj) + { + if (obj is null) return false; + else if (obj is Triangle tri) return Equals(tri); + return false; + } + public override int GetHashCode() => base.GetHashCode(); + + public bool Contains(Float3 point) + { + 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; + } + public Float3[] GetAllVerts() => new[] { a, b, c }; + + public static Triangle operator +(Triangle t, Float3 offset) => + new(t.a + offset, t.b + offset, t.c + offset); + public static Triangle operator -(Triangle t, Float3 offset) => + new(t.a - offset, t.b - offset, t.c - offset); + public static Triangle operator *(Triangle t, float factor) => + new(t.a * factor, t.b * factor, t.c * factor); + public static Triangle operator *(Triangle t, Float3 factor) => + new(t.a * factor, t.b * factor, t.c * factor); + public static Triangle operator /(Triangle t, float factor) => + new(t.a / factor, t.b / factor, t.c / factor); + public static Triangle operator /(Triangle t, Float3 factor) => + new(t.a / factor, t.b / factor, t.c / factor); + + public static bool operator ==(Triangle a, Triangle b) => a.Equals(b); + public static bool operator !=(Triangle a, Triangle b) => !a.Equals(b); + + public static implicit operator Triangle(Fill fill) => new(fill); + public static implicit operator Triangle(Fill fill) => new(fill); + public static implicit operator Triangle(Fill fill) => new(fill); + public static implicit operator Triangle(Fill fill) => new(fill); + public static implicit operator Triangle(Fill fill) => new(fill); + public static implicit operator Triangle((Float3 a, Float3 b, Float3 c) val) => + new(val.a, val.b, val.c); }