diff --git a/src/com/annimon/graphics/Point.java b/src/com/annimon/graphics/Point.java index 841f656..7c2c83b 100644 --- a/src/com/annimon/graphics/Point.java +++ b/src/com/annimon/graphics/Point.java @@ -6,7 +6,7 @@ package com.annimon.graphics; */ public class Point { - public double x, y; + private double x, y; public Point() { x = y = 0; diff --git a/src/com/annimon/graphics/TriangleClip.java b/src/com/annimon/graphics/TriangleClip.java index 43b6b88..e8cb041 100644 --- a/src/com/annimon/graphics/TriangleClip.java +++ b/src/com/annimon/graphics/TriangleClip.java @@ -15,15 +15,17 @@ public class TriangleClip extends Clip { @Override public double[] clip(double x1, double y1, double x2, double y2) { - return clipCyrusBeck(new Point(x1, y1), new Point(x2, y2), calcNormals(poly, poly.v)); + return clipCyrusBeck(new Point(x1, y1), new Point(x2, y2), calcNormals(poly)); } - private Point[] calcNormals(Polygon p, Point[] n) { + private Point[] calcNormals(Polygon p) { + Point[] n = new Point[p.nPoints]; Point v = new Point(); for (int i = 0; i < p.nPoints; i++) { int j = (i+1) % p.nPoints; int k = (i+2) % p.nPoints; // make vector be -1/mI + 1J + n[i] = new Point(); n[i].setX( -(p.v[j].getY() - p.v[i].getY()) / (p.v[j].getX() - p.v[i].getX()) ); n[i].setY(1.0); @@ -43,39 +45,34 @@ public class TriangleClip extends Clip { } private double[] clipCyrusBeck(Point p1, Point p2, Point[] n) { - double t,num,den; - // vectors - Point dirV = new Point(); - Point F = new Point(); - // start largest at smallest legal value and smallest // at largest legal value double t1 = 0.0; double t2 = 1.0; + // vectors // compute the direction vector - dirV.x = p2.x - p1.x; - dirV.y = p2.y - p1.y; - + Point dirV = new Point(p2.getX() - p1.getX(), p2.getY() - p1.getY()); + Point F = new Point(); boolean visible = true; int i = 0; while ((i < poly.nPoints) && visible) { - F.x = p1.x - poly.v[i].x; - F.y = p1.y - poly.v[i].y; + F.setX(p1.getX() - poly.v[i].getX()); + F.setY(p1.getY() - poly.v[i].getY()); - num = pointProduction(n[i], F); - den = pointProduction(n[i], dirV); + double num = pointProduction(n[i], F); + double den = pointProduction(n[i], dirV); // Parallel or Point if (den == 0.0) { -// parallel - if outside then forget the line; if inside then there are no + // parallel - if outside then forget the line; if inside then there are no // intersections with this side // but there may be with other edges, so in this case just keep going if (num > 0.0) { - // Parallel and outside or point (p1 == p2) and outside + // Parallel and outside or point (p1 == p2) and outside visible = false; } } else { - t = -(num/den); + double t = -(num/den); if (den < 0.0) { // entering if (t <= 1.0 && t > t1) t1 = t; @@ -83,19 +80,16 @@ public class TriangleClip extends Clip { } i++; } - /*if (t1 <= t2) { + if (t1 <= t2) { return new double[] { - p1.x + t1*dirV.x, - p1.y + t1*dirV.y, - p1.x + t2*dirV.x, - p1.y + t2*dirV.y + p1.getX() + t1*dirV.getX(), + p1.getY() + t1*dirV.getY(), + p1.getX() + t2*dirV.getX(), + p1.getY() + t2*dirV.getY() }; - } else visible = false;*/ + } // else visible = false;*/ return new double[] { - p1.x + t1*dirV.x, - p1.y + t1*dirV.y, - p1.x + t2*dirV.x, - p1.y + t2*dirV.y + -1, -1, -1, -1 }; }