# CustomQuad

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;
private double[] m = new double;
private double[] x1 = new double;
private double[] y1 = new double;

public CustomQuad(Point2D.Double corner1, Point2D.Double corner2, Point2D.Double corner3, Point2D.Double corner4){
m = (corner2.y - corner1.y)/(corner2.x - corner1.x + 0.000000001);
m = (corner3.y - corner2.y)/(corner3.x - corner2.x + 0.000000001);
m = (corner4.y - corner3.y)/(corner4.x - corner3.x + 0.000000001);
m = (corner1.y - corner4.y)/(corner1.x - corner4.x + 0.000000001);
x1 = corner1.x;
x1 = corner2.x;
x1 = corner3.x;
x1 = corner4.x;
y1 = corner1.y;
y1 = corner2.y;
y1 = corner3.y;
y1 = 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 < m*(insidePoint.x - x1) - insidePoint.y + y1;
modifier = 0 < m*(insidePoint.x - x1) - insidePoint.y + y1;
modifier = 0 < m*(insidePoint.x - x1) - insidePoint.y + y1;
modifier = 0 < m*(insidePoint.x - x1) - insidePoint.y + y1;
}
public boolean contains(Point2D.Double test){
return (modifier^(0 >= m*(test.x - x1) - test.y + y1))
&& (modifier^(0 >= m*(test.x - x1) - test.y + y1))
&& (modifier^(0 >= m*(test.x - x1) - test.y + y1))
&& (modifier^(0 >= m*(test.x - x1) - test.y + y1));
}
}
```

Robo Home | Changes | Preferences | AllPages
Edit text of this page | View other revisions
Last edited December 5, 2007 17:57 EST by Skilgannon (diff)
Search: