public LineSegment2d[] getIntersectingEdgesCopy(Line2d line) { return ConvexPolygon2dCalculator.getIntersectingEdgesCopy(line, this); }
/** * Returns the intersecting edges between this polygon and the given FrameLine2d. * The FrameLine2d is treated as a ray. This method returns null if: * - The start point of the ray starts inside the Polygon, * - The ray points away from the polygon. * - The intersection is a single vertex, * - There is no intersection. * * @param frameLine2d FrameLine2d Ray to check intersection with this Polygon. * @return FrameLineSegment2d[] The two edges that the Ray intersects on this Polygon. */ public FrameLineSegment2d[] getIntersectingEdges(FrameLine2d frameLine2d) { frameLine2d.checkReferenceFrameMatch(referenceFrame); LineSegment2d[] lineSegments = ConvexPolygon2dCalculator.getIntersectingEdgesCopy(frameLine2d.getLine2dCopy(), convexPolygon); if (lineSegments == null) return null; FrameLineSegment2d[] ret = new FrameLineSegment2d[lineSegments.length]; for (int i = 0; i < lineSegments.length; i++) { ret[i] = new FrameLineSegment2d(referenceFrame, lineSegments[i]); } return ret; }
@ContinuousIntegrationTest(estimatedDuration = 0.0) @Test(timeout = 30000) public void testGetIntersectingEdges4() { // empty polygon ConvexPolygon2D polygon = new ConvexPolygon2D(); LineSegment2D result1 = new LineSegment2D(); LineSegment2D result2 = new LineSegment2D(); Line2D line1 = new Line2D(new Point2D(0.5, 1.5), new Vector2D(0.0, 0.1)); assertEquals(ConvexPolygon2dCalculator.getIntersectingEdges(line1, result1, result2, polygon), 0); assertTrue(ConvexPolygon2dCalculator.getIntersectingEdgesCopy(line1, polygon) == null); }
@ContinuousIntegrationTest(estimatedDuration = 0.0) @Test(timeout = 30000) public void testGetIntersectingEdges3() { // point polygon ConvexPolygon2D polygon = new ConvexPolygon2D(); polygon.addVertex(new Point2D(-1.0, -0.5)); polygon.update(); LineSegment2D result1 = new LineSegment2D(); LineSegment2D result2 = new LineSegment2D(); Line2D line1 = new Line2D(new Point2D(0.0, 0.0), new Vector2D(-0.5, -0.25)); assertEquals(ConvexPolygon2dCalculator.getIntersectingEdges(line1, result1, result2, polygon), 0); assertTrue(ConvexPolygon2dCalculator.getIntersectingEdgesCopy(line1, polygon) == null); Line2D line2 = new Line2D(new Point2D(0.5, 1.5), new Vector2D(0.0, 0.1)); assertEquals(ConvexPolygon2dCalculator.getIntersectingEdges(line2, result1, result2, polygon), 0); assertTrue(ConvexPolygon2dCalculator.getIntersectingEdgesCopy(line2, polygon) == null); Line2D line3 = new Line2D(new Point2D(-1.0, -0.5), new Vector2D(1.0, 0.1)); assertEquals(ConvexPolygon2dCalculator.getIntersectingEdges(line3, result1, result2, polygon), 0); assertTrue(ConvexPolygon2dCalculator.getIntersectingEdgesCopy(line3, polygon) == null); }
@ContinuousIntegrationTest(estimatedDuration = 0.0) @Test(timeout = 30000) public void testGetIntersectingEdges2() { // line polygon Point2D vertex1 = new Point2D(1.0, 1.0); Point2D vertex2 = new Point2D(1.0, 0.0); LineSegment2D edge1 = new LineSegment2D(vertex1, vertex2); LineSegment2D edge2 = new LineSegment2D(vertex2, vertex1); // add in order so update does not change indices: ConvexPolygon2D polygon = new ConvexPolygon2D(); polygon.addVertex(vertex1); polygon.addVertex(vertex2); polygon.update(); LineSegment2D result1 = new LineSegment2D(); LineSegment2D result2 = new LineSegment2D(); Line2D line1 = new Line2D(new Point2D(0.5, 1.5), new Vector2D(0.0, 0.1)); LineSegment2D[] expected1 = null; assertEdgesEqual(expected1, ConvexPolygon2dCalculator.getIntersectingEdgesCopy(line1, polygon), false); assertEquals(0, ConvexPolygon2dCalculator.getIntersectingEdges(line1, result1, result2, polygon)); Line2D line2 = new Line2D(new Point2D(-0.5, 0.0), new Vector2D(0.75, 0.25)); LineSegment2D[] expected2 = new LineSegment2D[] {edge1, edge2}; assertEdgesEqual(expected2, ConvexPolygon2dCalculator.getIntersectingEdgesCopy(line2, polygon), false); assertEquals(2, ConvexPolygon2dCalculator.getIntersectingEdges(line2, result1, result2, polygon)); assertEdgesEqual(expected2, new LineSegment2D[] {result1, result2}, false); Line2D line3 = new Line2D(new Point2D(1.0, -0.5), new Vector2D(0.0, 0.1)); LineSegment2D[] expected3 = null; assertEdgesEqual(expected3, ConvexPolygon2dCalculator.getIntersectingEdgesCopy(line3, polygon), false); assertEquals(0, ConvexPolygon2dCalculator.getIntersectingEdges(line3, result1, result2, polygon)); }
assertEdgesEqual(expected1, ConvexPolygon2dCalculator.getIntersectingEdgesCopy(line1, polygon), false); assertEquals(2, ConvexPolygon2dCalculator.getIntersectingEdges(line1, result1, result2, polygon)); assertEdgesEqual(expected1, new LineSegment2D[] {result1, result2}, false); assertEdgesEqual(expected2, ConvexPolygon2dCalculator.getIntersectingEdgesCopy(line2, polygon), false); assertEquals(0, ConvexPolygon2dCalculator.getIntersectingEdges(line2, result1, result2, polygon)); assertEdgesEqual(expected3, ConvexPolygon2dCalculator.getIntersectingEdgesCopy(line3, polygon), false); assertEquals(1, ConvexPolygon2dCalculator.getIntersectingEdges(line3, result1, result2, polygon)); assertEdgesEqual(expected3, new LineSegment2D[] {result1}, false); assertEdgesEqual(expected4, ConvexPolygon2dCalculator.getIntersectingEdgesCopy(line4, polygon), false); assertEquals(2, ConvexPolygon2dCalculator.getIntersectingEdges(line4, result1, result2, polygon)); assertEdgesEqual(expected4, new LineSegment2D[] {result1, result2}, false); assertEdgesEqual(expected5, ConvexPolygon2dCalculator.getIntersectingEdgesCopy(line5, polygon), false); assertEquals(2, ConvexPolygon2dCalculator.getIntersectingEdges(line5, result1, result2, polygon)); assertEdgesEqual(expected5, new LineSegment2D[] {result1, result2}, false); assertEdgesEqual(expected6, ConvexPolygon2dCalculator.getIntersectingEdgesCopy(line6, polygon), false); assertEquals(2, ConvexPolygon2dCalculator.getIntersectingEdges(line6, result1, result2, polygon)); assertEdgesEqual(expected6, new LineSegment2D[] {result1, result2}, false); assertEdgesEqual(expected7, ConvexPolygon2dCalculator.getIntersectingEdgesCopy(line7, polygon), false); assertEquals(2, ConvexPolygon2dCalculator.getIntersectingEdges(line7, result1, result2, polygon)); assertEdgesEqual(expected7, new LineSegment2D[] {result1, result2}, false); assertEdgesEqual(expected8, ConvexPolygon2dCalculator.getIntersectingEdgesCopy(line8, polygon), false); assertEquals(2, ConvexPolygon2dCalculator.getIntersectingEdges(line8, result1, result2, polygon));
assertEquals(1, polygonWithOnePoint.getNumberOfVertices()); assertTrue(polygonWithOnePoint.getVertexCCW(0).equals(pointThatDefinesThePolygon)); assertTrue(ConvexPolygon2dCalculator.getIntersectingEdgesCopy(arbitraryLine, polygonWithOnePoint) == null); assertTrue(polygonWithOnePoint.getVertex(polygonWithOnePoint.lineOfSightStartIndex(arbitraryPoint0)).equals(pointThatDefinesThePolygon)); assertTrue(polygonWithOnePoint.getVertex(polygonWithOnePoint.lineOfSightEndIndex(arbitraryPoint0)).equals(pointThatDefinesThePolygon));
LineSegment2D[] intersectingEdges = ConvexPolygon2dCalculator.getIntersectingEdgesCopy(arbitraryLine, polygonWithTwoPoints); boolean isLineAbovePoint0 = ((pointThatDefinesThePolygon0.getX() - arbitraryLine.getPoint().getX()) * arbitraryLine.slope() + arbitraryLine.getPoint().getY()) >= pointThatDefinesThePolygon0.getY();