Robo Home | Changes | Preferences | AllPages

A quick class I whipped up so that I'll be able to out-of-bounds checking while still keeping a much quicker play-it-forward algorithm. It should work for any dimensioned quad that doesn't have any almost-vertical lines (ie. that 0.00000001 I added in causes a division by 0). I haven't tested it, but it looks right. So be warned! It's released as free code - no restrictions, as long as you don't charge money for it =). The other thing is that you can't put two diagonally opposite points as adjacent arguments when creating the quad.

```    public class  CustomQuad{
//using the two-point formula for a straight line (Linear Equation) - from Wikipedia
private boolean[] modifier = new boolean[4];
private double[] m = new double[4];
private double[] x1 = new double[4];
private double[] y1 = new double[4];

public CustomQuad(Point2D.Double corner1, Point2D.Double corner2, Point2D.Double corner3, Point2D.Double corner4){
m[0] = (corner2.y - corner1.y)/(corner2.x - corner1.x + 0.000000001);
m[1] = (corner3.y - corner2.y)/(corner3.x - corner2.x + 0.000000001);
m[2] = (corner4.y - corner3.y)/(corner4.x - corner3.x + 0.000000001);
m[3] = (corner1.y - corner4.y)/(corner1.x - corner4.x + 0.000000001);
x1[0] = corner1.x;
x1[1] = corner2.x;
x1[2] = corner3.x;
x1[3] = corner4.x;
y1[0] = corner1.y;
y1[1] = corner2.y;
y1[2] = corner3.y;
y1[3] = corner4.y;
calibrate(new Point2D.Double(
(corner1.x + corner2.x + corner3.x + corner4.x)/4,
(corner1.y + corner2.y + corner3.y + corner4.y)/4
));

}
private void calibrate(Point2D.Double insidePoint){
modifier[0] = 0 < m[0]*(insidePoint.x - x1[0]) - insidePoint.y + y1[0];
modifier[1] = 0 < m[1]*(insidePoint.x - x1[1]) - insidePoint.y + y1[1];
modifier[2] = 0 < m[2]*(insidePoint.x - x1[2]) - insidePoint.y + y1[2];
modifier[3] = 0 < m[3]*(insidePoint.x - x1[3]) - insidePoint.y + y1[3];
}
public boolean contains(Point2D.Double test){
return (modifier[0]^(0 >= m[0]*(test.x - x1[0]) - test.y + y1[0]))
&& (modifier[1]^(0 >= m[1]*(test.x - x1[1]) - test.y + y1[1]))
&& (modifier[2]^(0 >= m[2]*(test.x - x1[2]) - test.y + y1[2]))
&& (modifier[3]^(0 >= m[3]*(test.x - x1[3]) - test.y + y1[3]));
}
}
```

Robo Home | Changes | Preferences | AllPages