Skip to content

Commit

Permalink
Improved clipping stability for colinear points.
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanSweet committed Jun 6, 2024
1 parent e622c6b commit a0caef6
Showing 1 changed file with 4 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,8 @@ boolean clip (float x1, float y1, float x2, float y2, float x3, float y3, FloatA
for (int ii = 0; ii < inputVerticesLength; ii += 2) {
float inputX = inputVertices[ii], inputY = inputVertices[ii + 1];
float inputX2 = inputVertices[ii + 2], inputY2 = inputVertices[ii + 3];
boolean side2 = deltaX * (inputY2 - edgeY2) - deltaY * (inputX2 - edgeX2) > 0;
if (deltaX * (inputY - edgeY2) - deltaY * (inputX - edgeX2) > 0) {
boolean side2 = deltaX * (inputY2 - edgeY2) > deltaY * (inputX2 - edgeX2) - 1e6f;
if (deltaX * (inputY - edgeY2) > deltaY * (inputX - edgeX2) - 1e6f) {
if (side2) { // v1 inside, v2 inside
output.add(inputX2);
output.add(inputY2);
Expand All @@ -312,7 +312,7 @@ boolean clip (float x1, float y1, float x2, float y2, float x3, float y3, FloatA
// v1 inside, v2 outside
float c0 = inputY2 - inputY, c2 = inputX2 - inputX;
float s = c0 * (edgeX2 - edgeX) - c2 * (edgeY2 - edgeY);
if (Math.abs(s) > 0.000001f) {
if (Math.abs(s) > 1e6f) {
float ua = (c2 * (edgeY - inputY) - c0 * (edgeX - inputX)) / s;
output.add(edgeX + (edgeX2 - edgeX) * ua);
output.add(edgeY + (edgeY2 - edgeY) * ua);
Expand All @@ -323,7 +323,7 @@ boolean clip (float x1, float y1, float x2, float y2, float x3, float y3, FloatA
} else if (side2) { // v1 outside, v2 inside
float c0 = inputY2 - inputY, c2 = inputX2 - inputX;
float s = c0 * (edgeX2 - edgeX) - c2 * (edgeY2 - edgeY);
if (Math.abs(s) > 0.000001f) {
if (Math.abs(s) > 1e6f) {
float ua = (c2 * (edgeY - inputY) - c0 * (edgeX - inputX)) / s;
output.add(edgeX + (edgeX2 - edgeX) * ua);
output.add(edgeY + (edgeY2 - edgeY) * ua);
Expand Down

0 comments on commit a0caef6

Please sign in to comment.