@Test
  public void shouldCorrectlyDiscoverIfElementIsInSET() {
    cut.insert(new Point2D(.1, .1));

    assertFalse(cut.contains(new Point2D(.0, .0)));
    assertTrue(cut.contains(new Point2D(.1, .1)));
  }
 @Test
 public void shouldReturnTheCorrectSize() {
   assertEquals(0, cut.size());
   cut.insert(new Point2D(.0, .0));
   assertEquals(1, cut.size());
   cut.insert(new Point2D(.1, .1));
   assertEquals(2, cut.size());
 }
  @Test
  public void shouldGiveTheCorrectNearestPoint() {
    cut.insert(new Point2D(.0, .0));
    cut.insert(new Point2D(.0, .1));
    cut.insert(new Point2D(.0, .2));
    cut.insert(new Point2D(.0, .3));
    cut.insert(new Point2D(.0, .4));

    Point2D nearest = cut.nearest(new Point2D(.2, .2));

    assertEquals(new Point2D(.0, .2), nearest);
  }
  @Test
  public void shouldGiveTheCorrectRange() {
    cut.insert(new Point2D(.0, .0));
    cut.insert(new Point2D(.1, .1));
    cut.insert(new Point2D(.2, .2));
    cut.insert(new Point2D(.3, .3));
    cut.insert(new Point2D(.4, .4));
    cut.insert(new Point2D(.5, .5));

    List<Point2D> range = (List<Point2D>) cut.range(new RectHV(.1, .1, .4, .4));
    assertEquals(4, range.size());
    assertTrue(range.contains(new Point2D(.1, .1)));
    assertTrue(range.contains(new Point2D(.2, .2)));
    assertTrue(range.contains(new Point2D(.3, .3)));
    assertTrue(range.contains(new Point2D(.4, .4)));
  }
  public static KdTree createTreeDuplicates() {

    KdTree tree = new KdTree(2);

    tree.root = new KdTree.Node(new double[] {1, 2}, null);
    tree.root.split = 1;
    tree.root.left = new KdTree.Node(new double[] {1, 2}, null);
    tree.root.left.split = 0;
    tree.root.left.left = new KdTree.Node(new double[] {1, 2}, null);
    tree.root.left.left.split = -1;
    tree.root.left.right = null;
    tree.root.right = new KdTree.Node(new double[] {1, 2}, null);
    tree.root.right.split = -1;

    return tree;
  }
  /** @param args */
  public static void main(String[] args) {
    KdTree tree = new KdTree();

    Point2D p1 = new Point2D(0.5, 0.5);
    Point2D p2 = new Point2D(0.25, 0.5);
    Point2D p3 = new Point2D(0.75, 0.5);

    tree.insert(p1);
    tree.insert(p2);
    tree.insert(p3);

    Point2D qp = new Point2D(0.5, 0.49);
    Point2D np = tree.nearest(qp);

    StdOut.println(np.x() + ", " + np.y());
  }
  /** See if max distance is being respected */
  @Test
  public void findClosest_maxDistance() {
    KdTree tree = new KdTree(2);
    tree.root = new KdTree.Node(new double[] {1, 2}, null);

    KdTreeSearchN alg = createAlg();
    alg.setTree(tree);
    alg.setMaxDistance(2);

    found.reset();
    alg.findNeighbor(new double[] {11, 8}, 1, found);
    assertEquals(0, found.size);
    found.reset();
    alg.findNeighbor(new double[] {1, 1.5}, 1, found);
    assertEquals(1, found.size);
    assertTrue(found.data[0].node == tree.root);
  }
  public double findMaxCurrentPoolRadius(Point2D areaCenter, double areaWidth, double areaHeight) {

    Point2D nearestColumn = tree.nearest(areaCenter);

    double nearestColumnDistance = nearestColumn.distanceTo(areaCenter) - columnRadius;

    double lrMinWallDistance = Math.min(areaCenter.x(), roomX - areaCenter.x());
    double buMinWallDistance = Math.min((areaCenter.y()), roomY - areaCenter.y());

    double nearestWallDistance = Math.min(lrMinWallDistance, buMinWallDistance);

    double nearestObstacleDistance = Math.min(nearestColumnDistance, nearestWallDistance);

    if (nearestObstacleDistance > refPoolRadius) {
      refPoolRadius = nearestObstacleDistance;
      refPoolCenter = areaCenter;
    }

    return nearestObstacleDistance;
  }
Exemplo n.º 9
0
  public LightingInfo isIlluminati(
      final Vector4 point, Light light, CustomStack stack, final RayCollisionInfo collision) {
    if (light instanceof DirectionalLight) {
      return new LightingInfo(null, true, light.getDirection(point));
    }

    final PositionLight pointLight = (PositionLight) light;
    Vector4 position = pointLight.getPosition();
    Vector4 aux = new Vector4(position);
    aux.sub(point);
    Ray ray = new Ray(point, aux);

    RayCollisionInfo rci = tree.getCollision(point.distanceTo(position) + 0.0001, ray, stack, 0);

    if (rci == null) {
      if (light instanceof AreaLight) {}
      return new LightingInfo(null, !(light instanceof AreaLight), light.getDirection(point));
    }

    if (rci.obj.material.light != null) {
      return new LightingInfo(rci, true, rci.getRay().getDir());
    }
    return new LightingInfo(null, false, null);
  }
 @Test(expected = NullPointerException.class)
 public void shouldThrowNullPointerExceptionWhenCheckingNearestToNull() {
   cut.nearest(null);
 }
 @Test(expected = NullPointerException.class)
 public void shouldThrowNullPointerExceptionWhenCheckingRangeWithinNull() {
   cut.range(null);
 }
 @Test(expected = NullPointerException.class)
 public void shouldThrowNullPointerExceptionWhenCheckingIfSETContainsNull() {
   cut.contains(null);
 }
 @Test(expected = NullPointerException.class)
 public void shouldThrowNullPointerExceptionWhenInsertingNull() {
   cut.insert(null);
 }
 @Test
 public void shouldReturnFalseIfSetIsNotEmpty() {
   cut.insert(new Point2D(.0, .0));
   assertFalse(cut.isEmpty());
 }
 @Test
 public void shouldReturnTrueIfSetIsEmpty() {
   assertTrue(cut.isEmpty());
 }
Exemplo n.º 16
0
  public static void main(String[] args) {

    String filename = args[0];
    In in = new In(filename);

    StdDraw.show(0);

    // initialize the data structures with N points from standard input
    PointSET brute = new PointSET();
    KdTree kdtree = new KdTree();
    while (!in.isEmpty()) {
      double x = in.readDouble();
      double y = in.readDouble();
      Point2D p = new Point2D(x, y);
      kdtree.insert(p);
      brute.insert(p);
    }

    double x0 = 0.0, y0 = 0.0; // initial endpoint of rectangle
    double x1 = 0.0, y1 = 0.0; // current location of mouse
    boolean isDragging = false; // is the user dragging a rectangle

    // draw the points
    StdDraw.clear();
    StdDraw.setPenColor(StdDraw.BLACK);
    StdDraw.setPenRadius(.01);
    brute.draw();

    while (true) {
      StdDraw.show(40);

      // user starts to drag a rectangle
      if (StdDraw.mousePressed() && !isDragging) {
        x0 = StdDraw.mouseX();
        y0 = StdDraw.mouseY();
        isDragging = true;
        continue;
      }

      // user is dragging a rectangle
      else if (StdDraw.mousePressed() && isDragging) {
        x1 = StdDraw.mouseX();
        y1 = StdDraw.mouseY();
        continue;
      }

      // mouse no longer pressed
      else if (!StdDraw.mousePressed() && isDragging) {
        isDragging = false;
      }

      RectHV rect =
          new RectHV(Math.min(x0, x1), Math.min(y0, y1), Math.max(x0, x1), Math.max(y0, y1));
      // draw the points
      StdDraw.clear();
      StdDraw.setPenColor(StdDraw.BLACK);
      StdDraw.setPenRadius(.01);
      brute.draw();

      // draw the rectangle
      StdDraw.setPenColor(StdDraw.BLACK);
      StdDraw.setPenRadius();
      rect.draw();

      // draw the range search results for brute-force data structure in
      // red
      StdDraw.setPenRadius(.03);
      StdDraw.setPenColor(StdDraw.RED);
      for (Point2D p : brute.range(rect)) p.draw();

      // draw the range search results for kd-tree in blue
      StdDraw.setPenRadius(.02);
      StdDraw.setPenColor(StdDraw.BLUE);
      for (Point2D p : kdtree.range(rect)) p.draw();

      StdDraw.show(40);
    }
  }