Fixed: CalculateArea was doing some weird shit (resolves #372)

This commit is contained in:
ZZYZX 2020-01-18 22:08:48 +02:00
parent 6e4a02fb2b
commit 973a4b1447

View file

@ -73,22 +73,21 @@ namespace CodeImp.DoomBuilder.Geometry
// This calculates the area // This calculates the area
public float CalculateArea() public float CalculateArea()
{ {
// Multiply the x coordinate of each vertex by the y coordinate of the next vertex. float area = 0;
// Multiply the y coordinate of each vertex by the x coordinate of the next vertex. LinkedListNode<EarClipVertex> v = First;
// Subtract these. do
float result = 0.0f;
int firstcalculated = 0;
LinkedListNode<EarClipVertex> n1 = base.First;
while(firstcalculated < 2)
{ {
LinkedListNode<EarClipVertex> n2 = n1.Next ?? base.First;
float a = n1.Value.Position.x * n2.Value.Position.y; EarClipVertex v1 = v.Value;
float b = n1.Value.Position.y * n2.Value.Position.x; EarClipVertex v2 = (v.Next != null) ? v.Next.Value : First.Value;
result += a - b;
n1 = n2; area += (v2.Position.x + v1.Position.x) * (v2.Position.y - v1.Position.y);
if(n2 == base.First) firstcalculated++;
v = v.Next;
} }
return Math.Abs(result / 2.0f); while (v != null);
return Math.Abs(area * 0.5f);
} }
// This creates a bounding box from the outer polygon // This creates a bounding box from the outer polygon