Triangle implements Shape { public Map<Line2d, Point2d> intersectionSides(Line2d line) { final Map<Line2d, Point2d> ret = new HashMap<Line2d, Point2d>(); final Line2d first = new Line2d(this.vertices[0], this.vertices[1]); final Line2d second = new Line2d(this.vertices[1], this.vertices[2]); final Line2d third = new Line2d(this.vertices[2], this.vertices[0]); addIntersect(ret, first, line); addIntersect(ret, second, line); addIntersect(ret, third, line); return ret; } Triangle(Point2d vertex1, Point2d vertex2, Point2d vertex3);  Triangle(Point2d[] vertices); final boolean isInsideOnLine(Point2d point); @Override final boolean isInside(Point2d point); @Override Rectangle calculateRegularBoundingBox(); @Override void translate(float x, float y); @Override void scale(float sc); @Override void scale(Point2d centre, float sc); @Override void scaleCentroid(float sc); @Override Point2d calculateCentroid(); @Override double calculateArea(); @Override double minX(); @Override double minY(); @Override double maxX(); @Override double maxY(); @Override double getWidth(); @Override double getHeight(); @Override Triangle transform(Matrix transform); @Override Polygon asPolygon(); Point2d firstVertex(); Point2d secondVertex(); Point2d thirdVertex(); List<Line2d> getEdges(); boolean sharesVertex(Triangle other); @Override double intersectionArea(Shape that); @Override double intersectionArea(Shape that, int nStepsPerDimention); @Override Triangle clone(); @Override String toString(); @Override int hashCode(); @Override boolean equals(Object obj); Map<Line2d, Point2d> intersectionSides(Line2d line); @Override double calculatePerimeter(); @Override RotatedRectangle minimumBoundingRectangle(); @Override boolean isConvex(); public Point2d[] vertices; }
@Test public void testLineIntersection() throws Exception { Triangle t = new Triangle( new Point2dImpl(0, 0), new Point2dImpl(0.5f, 1f), new Point2dImpl(1, 0) ); final Point2d begin = new Point2dImpl(0f, 0.5f); final Point2d end = new Point2dImpl(1f, 0.5f); final Line2d midline = new Line2d(begin, end); Map<Line2d, Point2d> points = t.intersectionSides(midline); assertTrue(points.size() == 2); t = new Triangle( new Point2dImpl(0, 0), new Point2dImpl(0.5f, 1f), new Point2dImpl(0.5f, 0f) ); points = t.intersectionSides(midline); assertTrue(points.size() == 2); t = new Triangle( new Point2dImpl(0, 0.5f), new Point2dImpl(0.5f, 1f), new Point2dImpl(1, 0.5f) ); points = t.intersectionSides(midline); }