Pretty sure I'm done with triangle. Changelog next
This commit is contained in:
parent
63631632fe
commit
0e0922e236
@ -30,6 +30,7 @@ Here's the full changelog:
|
|||||||
+ : IIndexRangeAll<Float3>
|
+ : IIndexRangeAll<Float3>
|
||||||
+ : ISplittable<Line, (Float3[] As, Float3[] Bs)>
|
+ : ISplittable<Line, (Float3[] As, Float3[] Bs)>
|
||||||
+ IWithinRange<Float3, float>
|
+ IWithinRange<Float3, float>
|
||||||
|
+ Angle
|
||||||
+ Slope
|
+ Slope
|
||||||
+ Line()
|
+ Line()
|
||||||
+ ToFloatArrayAll(params Line[])
|
+ ToFloatArrayAll(params Line[])
|
||||||
|
|||||||
@ -52,11 +52,15 @@ public struct Angle : IAbsolute<Angle>, IAverage<Angle>, IClamp<Angle>, ICloneab
|
|||||||
_ => throw new ArgumentException("Unknown type.", nameof(valueType)),
|
_ => 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;
|
endA -= middleB;
|
||||||
endC -= middleB;
|
endC -= middleB;
|
||||||
|
|
||||||
float dot = Float3.Dot(endA, endC);
|
float dot = Float3.Dot(endA, endC);
|
||||||
return Mathf.ArcCos(dot * endA.InverseMagnitude * endC.InverseMagnitude);
|
return Mathf.ArcCos(dot * endA.InverseMagnitude * endC.InverseMagnitude);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -16,6 +16,7 @@ public class Line : IAverage<Line>, IClosestTo<Float3>, IContains<Float3>, IEqua
|
|||||||
public static Line One => (Float3.Zero, Float3.One);
|
public static Line One => (Float3.Zero, Float3.One);
|
||||||
public static Line Zero => (Float3.Zero, Float3.Zero);
|
public static Line Zero => (Float3.Zero, Float3.Zero);
|
||||||
|
|
||||||
|
public Angle Angle => Mathf.ArcTan(Slope);
|
||||||
public float Length => (b - a).Magnitude;
|
public float Length => (b - a).Magnitude;
|
||||||
public Float3 Midpoint => (a + b) / 2;
|
public Float3 Midpoint => (a + b) / 2;
|
||||||
public float Slope => (b.y - a.y) / (b.x - a.x);
|
public float Slope => (b.y - a.y) / (b.x - a.x);
|
||||||
@ -253,5 +254,5 @@ public class Line : IAverage<Line>, IClosestTo<Float3>, IContains<Float3>, IEqua
|
|||||||
public static implicit operator Line(Fill<Int3> fill) => new(fill);
|
public static implicit operator Line(Fill<Int3> fill) => new(fill);
|
||||||
public static implicit operator Line(Fill<float> fill) => new(fill);
|
public static implicit operator Line(Fill<float> fill) => new(fill);
|
||||||
public static implicit operator Line(Fill<int> fill) => new(fill);
|
public static implicit operator Line(Fill<int> 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);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -45,6 +45,10 @@ public class Triangle : IClosestTo<Float3>, IContains<Float3>,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 Float3 a, b, c;
|
||||||
|
|
||||||
public Triangle() : this(Float3.Zero, Float3.Zero, Float3.Zero) { }
|
public Triangle() : this(Float3.Zero, Float3.Zero, Float3.Zero) { }
|
||||||
@ -176,6 +180,24 @@ public class Triangle : IClosestTo<Float3>, IContains<Float3>,
|
|||||||
return result;
|
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
|
public bool Equals(Triangle? other) => other is not null && a == other.a
|
||||||
&& b == other.b && c == other.c;
|
&& b == other.b && c == other.c;
|
||||||
public override bool Equals(object? obj)
|
public override bool Equals(object? obj)
|
||||||
@ -191,7 +213,7 @@ public class Triangle : IClosestTo<Float3>, IContains<Float3>,
|
|||||||
Triangle pab = (point, a, b),
|
Triangle pab = (point, a, b),
|
||||||
pbc = (point, b, c),
|
pbc = (point, b, c),
|
||||||
pca = (point, c, a);
|
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 };
|
public Float3[] GetAllVerts() => new[] { a, b, c };
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user