## ~zjm/Moon3D

b97e3afad828942c3b14958521ee0e52e28b5b75 — Zack Michener 4 months ago
```sidestep weird clipping bug
```
```2 files changed, 6 insertions(+), 7 deletions(-)

M src/3d/clip.c
M test/clip.test.c
```
`M src/3d/clip.c => src/3d/clip.c +2 -2`
```@@ 72,7 72,7 @@ bool ClipT(double den, double num, double *tE, double *tL)
}

bool ClipPolygon(Polygon **polygon)
-/* alters the vertices of a polygon to fit within -1 <= x, y, z <= 1
+/* alters the vertices of a polygon to fit within -w <= x, y, z <= w
* returns whether polygon is visible after clipping */
{
// planes in W-space

@@ 93,7 93,7 @@ bool ClipPolygon(Polygon **polygon)
// floating point errors can make some vertices still be out of bounds...
ConstrainPolygon(*polygon);

-	return NumVertices(*polygon) > 0;
+	return NumVertices(*polygon) > 2;
}

void ConstrainPolygon(Polygon *polygon)

```
`M test/clip.test.c => test/clip.test.c +4 -5`
```@@ 106,18 106,16 @@ void TestClipPolygonPlane(void)
Plane x_neg = Pl(V4D(0, 0, 0, 0), V4D(-1, 0, 0, -1));
Plane x_pos = Pl(V4D(0, 0, 0, 0), V4D(1, 0, 0, -1));
Polygon *p = NewPolygon();
ClipPolygonPlane(&p, x_neg);
ClipPolygonPlane(&p, x_pos);
for (int i = 0; i < NumVertices(p); i++) {
Vertex *v = PolygonVertex(p, i);
-		PrintVec(*v);
AssertTrue(v->x <= v->w);
+		AssertTrue(v->x >= -v->w);
}
}

@@ 185,13 183,14 @@ void TestClipPolygon(void)
}
FreePolygon(p);

+	// weird test case
p = NewPolygon();