Refactor 2

This commit is contained in:
Syntriax 2023-12-07 11:14:18 +03:00
parent 6183d2225f
commit 2e72347721
7 changed files with 41 additions and 13 deletions

View File

@ -91,8 +91,12 @@ public static class PhysicsMath
public static bool ApproximatelyEquals(this float a, float b) public static bool ApproximatelyEquals(this float a, float b)
=> ApproximatelyEqualsEpsilon(a, b, float.Epsilon); => ApproximatelyEquals(a, b, float.Epsilon);
public static bool ApproximatelyEqualsEpsilon(this float a, float b, float epsilon) public static bool ApproximatelyEquals(this Vector2 a, Vector2 b)
=> ApproximatelyEquals(a, b, float.Epsilon);
public static bool ApproximatelyEquals(this Vector2 a, Vector2 b, float epsilon)
=> ApproximatelyEquals(a.X, b.X, epsilon) && ApproximatelyEquals(a.Y, b.Y, epsilon);
public static bool ApproximatelyEquals(this float a, float b, float epsilon)
{ {
if (a == b) if (a == b)
return true; return true;
@ -107,9 +111,4 @@ public static class PhysicsMath
return diff / Math.Min(absA + absB, float.MaxValue) < epsilon; return diff / Math.Min(absA + absB, float.MaxValue) < epsilon;
} }
public static bool ApproximatelyEquals(this Vector2 a, Vector2 b)
=> ApproximatelyEqualEpsilon(a, b, float.Epsilon);
public static bool ApproximatelyEqualEpsilon(this Vector2 a, Vector2 b, float epsilon)
=> ApproximatelyEqualsEpsilon(a.X, b.X, epsilon) && ApproximatelyEqualsEpsilon(a.Y, b.Y, epsilon);
} }

View File

@ -7,4 +7,11 @@ public record AABB(Vector2 LowerBoundary, Vector2 UpperBoundary)
public bool Overlaps(Vector2 point) public bool Overlaps(Vector2 point)
=> point.X >= LowerBoundary.X && point.X <= UpperBoundary.X && => point.X >= LowerBoundary.X && point.X <= UpperBoundary.X &&
point.Y >= LowerBoundary.Y && point.Y <= UpperBoundary.Y; point.Y >= LowerBoundary.Y && point.Y <= UpperBoundary.Y;
public bool Overlaps(AABB other)
=> LowerBoundary.X <= other.UpperBoundary.X && UpperBoundary.X >= other.LowerBoundary.X &&
LowerBoundary.Y <= other.UpperBoundary.Y && UpperBoundary.Y >= other.LowerBoundary.Y;
public bool ApproximatelyEquals(AABB other)
=> LowerBoundary.ApproximatelyEquals(other.LowerBoundary) && UpperBoundary.ApproximatelyEquals(other.UpperBoundary);
} }

View File

@ -4,13 +4,15 @@ namespace Syntriax.Engine.Physics2D.Primitives;
public record Circle(Vector2 Position, float Radius) public record Circle(Vector2 Position, float Radius)
{ {
public bool Intersects(Circle circleOther) public bool Intersects(Circle other)
{ {
float distanceSquared = (Position - circleOther.Position).LengthSquared(); float distanceSquared = (Position - other.Position).LengthSquared();
float radiusSumSquared = Radius * Radius + circleOther.Radius * circleOther.Radius; float radiusSumSquared = Radius * Radius + other.Radius * other.Radius;
return distanceSquared < radiusSumSquared; return distanceSquared < radiusSumSquared;
} }
public bool Overlaps(Vector2 point) => (Position - point).LengthSquared() <= Radius * Radius; public bool Overlaps(Vector2 point) => (Position - point).LengthSquared() <= Radius * Radius;
public bool ApproximatelyEquals(Circle other)
=> Position.ApproximatelyEquals(other.Position) && Radius.ApproximatelyEquals(other.Radius);
} }

View File

@ -118,4 +118,7 @@ public record Line(Vector2 From, Vector2 To)
return result; return result;
} }
public bool ApproximatelyEquals(Line other)
=> From.ApproximatelyEquals(other.From) && To.ApproximatelyEquals(other.To);
} }

View File

@ -3,4 +3,6 @@ namespace Syntriax.Engine.Physics2D.Primitives;
public record LineEquation(float Slope, float OffsetY) public record LineEquation(float Slope, float OffsetY)
{ {
public float Resolve(float x) => Slope * x + OffsetY; // y = mx + b public float Resolve(float x) => Slope * x + OffsetY; // y = mx + b
public bool ApproximatelyEquals(LineEquation other)
=> Slope.ApproximatelyEquals(other.Slope) && OffsetY.ApproximatelyEquals(other.OffsetY);
} }

View File

@ -36,7 +36,7 @@ public record Shape(IList<Vector2> Vertices)
} }
} }
public IList<Line> Lines public List<Line> Lines
{ {
get get
{ {
@ -53,4 +53,16 @@ public record Shape(IList<Vector2> Vertices)
lines.Add(new(Vertices[i], Vertices[i + 1])); lines.Add(new(Vertices[i], Vertices[i + 1]));
lines.Add(new(Vertices[^1], Vertices[0])); lines.Add(new(Vertices[^1], Vertices[0]));
} }
public bool ApproximatelyEquals(Shape other)
{
if (Vertices.Count != other.Vertices.Count)
return false;
for (int i = 0; i < Vertices.Count; i++)
if (!Vertices[i].ApproximatelyEquals(other.Vertices[i]))
return false;
return true;
}
} }

View File

@ -27,7 +27,7 @@ public record Triangle(Vector2 A, Vector2 B, Vector2 C)
{ {
float x = (slopeAB * slopeBC * (A.Y - C.Y) + slopeBC * (A.X + B.X) - slopeAB * (B.X + C.X)) / (2 * (slopeBC - slopeAB)); float x = (slopeAB * slopeBC * (A.Y - C.Y) + slopeBC * (A.X + B.X) - slopeAB * (B.X + C.X)) / (2 * (slopeBC - slopeAB));
float y = -(x - (A.X + B.X) / 2) / slopeAB + (A.Y + B.Y) / 2; float y = -(x - (A.X + B.X) / 2) / slopeAB + (A.Y + B.Y) / 2;
center = new Vector2((float)x, (float)y); center = new Vector2(x, y);
} }
else else
center = (midAB + midBC) * .5f; center = (midAB + midBC) * .5f;
@ -46,6 +46,9 @@ public record Triangle(Vector2 A, Vector2 B, Vector2 C)
float pointTriangleAreasSum = pointTriangleArea1 + pointTriangleArea2 + pointTriangleArea3; float pointTriangleAreasSum = pointTriangleArea1 + pointTriangleArea2 + pointTriangleArea3;
return originalTriangleArea >= pointTriangleAreasSum; return originalTriangleArea.ApproximatelyEquals(pointTriangleAreasSum, float.Epsilon * 3f);
} }
public bool ApproximatelyEquals(Triangle other)
=> A.ApproximatelyEquals(other.A) && B.ApproximatelyEquals(other.B) && C.ApproximatelyEquals(other.C);
} }