@Test public void distanceBetween() { double x = penAreaBoundary.getCorner1().x(); double y = penAreaBoundary.getCorner2().y(); assertThat(penAreaBoundary.distanceBetween(Vector2.fromXY(x, 50), Vector2.fromXY(x, 0))) .isCloseTo(50, within(1e-10)); assertThat(penAreaBoundary.distanceBetween(Vector2.fromXY(x, -50), Vector2.fromXY(x, -100))) .isCloseTo(50, within(1e-10)); assertThat(penAreaBoundary.distanceBetween(Vector2.fromXY(x - 50, y), Vector2.fromXY(x - 100, y))) .isCloseTo(50, within(1e-10)); assertThat(penAreaBoundary.distanceBetween(Vector2.fromXY(x - 50, -y), Vector2.fromXY(x - 100, -y))) .isCloseTo(50, within(1e-10)); assertThat(penAreaBoundary.distanceBetween(Vector2.fromXY(x - 50, -y), Vector2.fromXY(x, -y + 80))) .isCloseTo(130, within(1e-10)); assertThat(penAreaBoundary.distanceBetween(Vector2.fromXY(x - 50, -y), Vector2.fromXY(x - 10, y))) .isCloseTo(penAreaBoundary.getWidth() + 60, within(1e-10)); assertThat(penAreaBoundary.distanceBetween(Vector2.fromXY(x - 50, y), Vector2.fromXY(x, y - 80))) .isCloseTo(130, within(1e-10)); assertThat(penAreaBoundary.distanceBetween(Vector2.fromXY(x - 10, y), Vector2.fromXY(x - 50, -y))) .isCloseTo(penAreaBoundary.getWidth() + 60, within(1e-10)); assertThat(penAreaBoundary.distanceBetween(Vector2.fromXY(x, -y + 80), Vector2.fromXY(x - 50, -y))) .isCloseTo(130, within(1e-10)); assertThat(penAreaBoundary.distanceBetween(Vector2.fromXY(x, y - 80), Vector2.fromXY(x - 50, y))) .isCloseTo(130, within(1e-10)); }
public double distanceBetween(final IVector2 p1, final IVector2 p2) { ILineSegment edge1 = edgeOf(p1); ILineSegment edge2 = edgeOf(p2); if (edge1 == edge2) { return p1.distanceTo(p2); } boolean started = false; double distance = 0; for (ILineSegment edge : edges) { if (started) { if (edge1 == edge) { distance += edge1.getStart().distanceTo(p1); return distance; } else if (edge2 == edge) { distance += edge2.getStart().distanceTo(p2); return distance; } else { distance += edge.getLength(); } } else { if (edge1 == edge) { distance += edge1.getEnd().distanceTo(p1); started = true; } else if (edge2 == edge) { distance += edge2.getEnd().distanceTo(p2); started = true; } } } throw new IllegalStateException("Could not find all points on edges"); }
PenAreaBoundary implements Comparator<IVector2> { public double distanceBetween(final IVector2 p1, final IVector2 p2) { ILineSegment edge1 = edgeOf(p1); ILineSegment edge2 = edgeOf(p2); if (edge1 == edge2) { return p1.distanceTo(p2); } boolean started = false; double distance = 0; for (ILineSegment edge : edges) { if (started) { if (edge1 == edge) { distance += edge1.getStart().distanceTo(p1); return distance; } else if (edge2 == edge) { distance += edge2.getStart().distanceTo(p2); return distance; } else { distance += edge.getLength(); } } else { if (edge1 == edge) { distance += edge1.getEnd().distanceTo(p1); started = true; } else if (edge2 == edge) { distance += edge2.getEnd().distanceTo(p2); started = true; } } } throw new IllegalStateException("Could not find all points on edges"); } }
PenAreaBoundary implements Comparator<IVector2> { public double distanceBetween(final IVector2 p1, final IVector2 p2) { ILineSegment edge1 = edgeOf(p1); ILineSegment edge2 = edgeOf(p2); if (edge1 == edge2) { return p1.distanceTo(p2); } boolean started = false; double distance = 0; for (ILineSegment edge : edges) { if (started) { if (edge1 == edge) { distance += edge1.getStart().distanceTo(p1); return distance; } else if (edge2 == edge) { distance += edge2.getStart().distanceTo(p2); return distance; } else { distance += edge.getLength(); } } else { if (edge1 == edge) { distance += edge1.getEnd().distanceTo(p1); started = true; } else if (edge2 == edge) { distance += edge2.getEnd().distanceTo(p2); started = true; } } } throw new IllegalStateException("Could not find all points on edges"); } private  PenAreaBoundary(final double width, final double depth, final double sign); }
PenAreaBoundary implements Comparator<IVector2> { public double distanceBetween(final IVector2 p1, final IVector2 p2) { ILineSegment edge1 = edgeOf(p1); ILineSegment edge2 = edgeOf(p2); if (edge1 == edge2) { return p1.distanceTo(p2); } boolean started = false; double distance = 0; for (ILineSegment edge : edges) { if (started) { if (edge1 == edge) { distance += edge1.getStart().distanceTo(p1); return distance; } else if (edge2 == edge) { distance += edge2.getStart().distanceTo(p2); return distance; } else { distance += edge.getLength(); } } else { if (edge1 == edge) { distance += edge1.getEnd().distanceTo(p1); started = true; } else if (edge2 == edge) { distance += edge2.getEnd().distanceTo(p2); started = true; } } } throw new IllegalStateException("Could not find all points on edges"); } private  PenAreaBoundary(final double width, final double depth, final double sign); static PenAreaBoundary ownWithMargin(final double margin); PenAreaBoundary withMargin(final double margin); IVector2 closestPoint(final IVector2 p); IVector2 projectPoint(final IVector2 p); IVector2 projectPoint(final IVector2 p1, final IVector2 p2); Optional<IVector2> nextTo(final IVector2 p, final double margin, final int direction); Optional<IVector2> nextIntermediateCorner(final IVector2 pFrom, final IVector2 pTo); double distanceBetween(final IVector2 p1, final IVector2 p2); @Override int compare(IVector2 p1, IVector2 p2); Optional<IVector2> stepAlongBoundary(final IVector2 start, final double distance); boolean isPointInShape(IVector2 point); double getWidth(); double getDepth(); IVector2 getStart(); IVector2 getCorner1(); IVector2 getCorner2(); IVector2 getEnd(); IVector2 getCenter(); }
PenAreaBoundary implements Comparator<IVector2> { public double distanceBetween(final IVector2 p1, final IVector2 p2) { ILineSegment edge1 = edgeOf(p1); ILineSegment edge2 = edgeOf(p2); if (edge1 == edge2) { return p1.distanceTo(p2); } boolean started = false; double distance = 0; for (ILineSegment edge : edges) { if (started) { if (edge1 == edge) { distance += edge1.getStart().distanceTo(p1); return distance; } else if (edge2 == edge) { distance += edge2.getStart().distanceTo(p2); return distance; } else { distance += edge.getLength(); } } else { if (edge1 == edge) { distance += edge1.getEnd().distanceTo(p1); started = true; } else if (edge2 == edge) { distance += edge2.getEnd().distanceTo(p2); started = true; } } } throw new IllegalStateException("Could not find all points on edges"); } private  PenAreaBoundary(final double width, final double depth, final double sign); static PenAreaBoundary ownWithMargin(final double margin); PenAreaBoundary withMargin(final double margin); IVector2 closestPoint(final IVector2 p); IVector2 projectPoint(final IVector2 p); IVector2 projectPoint(final IVector2 p1, final IVector2 p2); Optional<IVector2> nextTo(final IVector2 p, final double margin, final int direction); Optional<IVector2> nextIntermediateCorner(final IVector2 pFrom, final IVector2 pTo); double distanceBetween(final IVector2 p1, final IVector2 p2); @Override int compare(IVector2 p1, IVector2 p2); Optional<IVector2> stepAlongBoundary(final IVector2 start, final double distance); boolean isPointInShape(IVector2 point); double getWidth(); double getDepth(); IVector2 getStart(); IVector2 getCorner1(); IVector2 getCorner2(); IVector2 getEnd(); IVector2 getCenter();  }