Исправлено отсечение по треугольнику

This commit is contained in:
Victor 2014-03-02 21:03:30 +02:00
parent 91cb4953b3
commit b8da58f82b
2 changed files with 22 additions and 28 deletions

View File

@ -6,7 +6,7 @@ package com.annimon.graphics;
*/
public class Point {
public double x, y;
private double x, y;
public Point() {
x = y = 0;

View File

@ -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,31 +45,26 @@ 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) {
@ -75,7 +72,7 @@ public class TriangleClip extends Clip {
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
};
}