public void testRayCast() { // Static RayCast tests Assert.AreEqual(double.PositiveInfinity, testRect.rayCast(new Ray2D(1.0d, 1.0d, -7.0d, -8.0d)), 0.000005d); Assert.AreEqual(System.Math.Sqrt(2), testRect.rayCast(new Ray2D(2.0d, 3.0d, 3.0d, 4.0d)), 0.000005d); // Serial RayCast tests Rect2D randomRect; Line2D currentSide; Point2D randomPointOnSide; Point2D randomPoint; int i = 100; do { randomRect = new Rect2D(Util.generateRandomDoubleBetween(-1000.0d, 1000.0d), Util.generateRandomDoubleBetween(-1000.0d, 1000.0d), Util.generateRandomDoubleBetween(-1000.0d, 1000.0d), Util.generateRandomDoubleBetween(-1000.0d, 1000.0d)); int j = 50; do { currentSide = new Line2D(randomRect.getUpperLeft(), randomRect.getUpperRight()); randomPointOnSide = currentSide.randomPoint(); randomPoint = new Point2D(Util.generateRandomDoubleBetween(-1000.0d, 1000.0d), Util.generateRandomDoubleBetween(randomPointOnSide.getY(), 1000.0d)); Assert.AreEqual(randomPoint.distance(randomPointOnSide), randomRect.rayCast(new Ray2D(randomPoint, randomPoint.vec(randomPointOnSide))), 0.000005d); currentSide = new Line2D(randomRect.getLowerLeft(), randomRect.getLowerRight()); randomPointOnSide = currentSide.randomPoint(); randomPoint = new Point2D(Util.generateRandomDoubleBetween(-1000.0d, 1000.0d), Util.generateRandomDoubleBetween(-1000.0d, randomPointOnSide.getY())); Assert.AreEqual(randomPoint.distance(randomPointOnSide), randomRect.rayCast(new Ray2D(randomPoint, randomPoint.vec(randomPointOnSide))), 0.000005d); currentSide = new Line2D(randomRect.getLowerLeft(), randomRect.getUpperLeft()); randomPointOnSide = currentSide.randomPoint(); randomPoint = new Point2D(Util.generateRandomDoubleBetween(-1000.0d, randomPointOnSide.getX()), Util.generateRandomDoubleBetween(-1000.0d, 1000.0d)); Assert.AreEqual(randomPoint.distance(randomPointOnSide), randomRect.rayCast(new Ray2D(randomPoint, randomPoint.vec(randomPointOnSide))), 0.000005d); currentSide = new Line2D(randomRect.getLowerRight(), randomRect.getUpperRight()); randomPointOnSide = currentSide.randomPoint(); randomPoint = new Point2D(Util.generateRandomDoubleBetween(randomPointOnSide.getX(), 1000.0d), Util.generateRandomDoubleBetween(-1000.0d, 1000.0d)); Assert.AreEqual(randomPoint.distance(randomPointOnSide), randomRect.rayCast(new Ray2D(randomPoint, randomPoint.vec(randomPointOnSide))), 0.000005d); j -= 1; } while (j > 0); i -= 1; } while (i > 0); }
/** * Calculates the length of a ray until it intersects with a shape.<br/> * As this class uses the set maximum ray range to speed up this ray casting,<br/> * this function may return {@code double.PositiveInfinity} for a ray that intersects with an obstacle that is further away than the rayRange from the given starting point. * @param ray the ray to be used for ray casting. * @return the length of the ray. */ public double rayCast(Ray2D ray) { double result = double.PositiveInfinity; Rect2D rayBounding = new Rect2D(ray.getStart().getX() - rayRange, ray.getStart().getY() - rayRange, ray.getStart().getX() + rayRange, ray.getStart().getY() + rayRange); for (int i = 0; i < shapes.Size(); ++i) { Rect2D bounding = boundaries.Get(i); if (rayBounding.isInsideBorder(bounding.getLowerLeft()) || rayBounding.isInsideBorder(bounding.getUpperLeft()) || rayBounding.isInsideBorder(bounding.getLowerRight()) || rayBounding.isInsideBorder(bounding.getUpperRight())) { double tmp = shapes.Get(i).rayCast(ray); result = tmp < result ? tmp : result; } } return(result); }
public void testTransform() { Assert.AreEqual(((Rect2D)testRect.transform(TransformMatrix2D.UNITY_MATRIX)).getUpperLeft().getX(), testRect.getUpperLeft().getX(), 0.000005d); Assert.AreEqual(((Rect2D)testRect.transform(TransformMatrix2D.UNITY_MATRIX)).getUpperLeft().getY(), testRect.getUpperLeft().getY(), 0.000005d); Assert.AreEqual(((Rect2D)testRect.transform(TransformMatrix2D.UNITY_MATRIX)).getLowerRight().getX(), testRect.getLowerRight().getX(), 0.000005d); Assert.AreEqual(((Rect2D)testRect.transform(TransformMatrix2D.UNITY_MATRIX)).getLowerRight().getY(), testRect.getLowerRight().getY(), 0.000005d); Assert.AreEqual(((Rect2D)testRect.transform(TransformMatrix2D.translate(3.0d, 5.0d))).getUpperLeft().getX(), 6.0d, 0.000005d); Assert.AreEqual(((Rect2D)testRect.transform(TransformMatrix2D.translate(3.0d, 5.0d))).getUpperLeft().getY(), 13.0d, 0.000005d); Assert.AreEqual(((Rect2D)testRect.transform(TransformMatrix2D.translate(3.0d, 5.0d))).getLowerRight().getX(), 8.0d, 0.000005d); Assert.AreEqual(((Rect2D)testRect.transform(TransformMatrix2D.translate(3.0d, 5.0d))).getLowerRight().getY(), 9.0d, 0.000005d); Assert.AreEqual(((Rect2D)testRect.transform(TransformMatrix2D.scale(2.0d, 4.0d))).getUpperLeft().getX(), 6.0d, 0.000005d); Assert.AreEqual(((Rect2D)testRect.transform(TransformMatrix2D.scale(2.0d, 4.0d))).getUpperLeft().getY(), 32.0d, 0.000005d); Assert.AreEqual(((Rect2D)testRect.transform(TransformMatrix2D.scale(2.0d, 4.0d))).getLowerRight().getX(), 10.0d, 0.000005d); Assert.AreEqual(((Rect2D)testRect.transform(TransformMatrix2D.scale(2.0d, 4.0d))).getLowerRight().getY(), 16.0d, 0.000005d); }