@Test public void getSegmentSplitByPolygon() throws Exception { Vector2D start = GeometryFactory.createVector(-5,0); Vector2D end = GeometryFactory.createVector(5,0); Segment2D segment = GeometryFactory.createSegment(start, end); ArrayList<Vector2D> nodeArray = new ArrayList<>(); nodeArray.add(GeometryFactory.createVector(-2, -2)); nodeArray.add(GeometryFactory.createVector(2, -2)); nodeArray.add(GeometryFactory.createVector(2, 2)); nodeArray.add(GeometryFactory.createVector(-2, 2)); Polygon2D polygon = GeometryFactory.createPolygon(nodeArray); ArrayList<Segment2D> splittedSegment = segment.getSegmentSplitByPolygon(polygon); assertEquals(3, splittedSegment.size()); assertEquals(-5, splittedSegment.get(0).getFirstPoint().getXComponent(), PRECISION); assertEquals(0, splittedSegment.get(0).getFirstPoint().getYComponent(), PRECISION); assertEquals(-2, splittedSegment.get(0).getLastPoint().getXComponent(), PRECISION); assertEquals(0, splittedSegment.get(0).getLastPoint().getYComponent(), PRECISION); assertEquals(-2, splittedSegment.get(1).getFirstPoint().getXComponent(), PRECISION); assertEquals(0, splittedSegment.get(1).getFirstPoint().getYComponent(), PRECISION); assertEquals(2, splittedSegment.get(1).getLastPoint().getXComponent(), PRECISION); assertEquals(0, splittedSegment.get(1).getLastPoint().getYComponent(), PRECISION); assertEquals(2, splittedSegment.get(2).getFirstPoint().getXComponent(), PRECISION); assertEquals(0, splittedSegment.get(2).getFirstPoint().getYComponent(), PRECISION); assertEquals(5, splittedSegment.get(2).getLastPoint().getXComponent(), PRECISION); assertEquals(0, splittedSegment.get(2).getLastPoint().getYComponent(), PRECISION); System.out.println(splittedSegment.toString()); }
public ArrayList<Segment2D> getSegmentSplitByPolygon(Polygon2D polygon) { ArrayList<Segment2D> segmentList = new ArrayList<>(); ArrayList<Vector2D> segmentListPoints = new ArrayList<>(); segmentListPoints.add(this.getFirstPoint()); segmentListPoints.addAll(this.getIntersection(polygon)); segmentListPoints.add(this.getLastPoint()); Comparator<Vector2D> byDistance = (e1, e2) -> Double.compare( e1.distance(this.getFirstPoint()), e2.distance(this.getFirstPoint())); segmentListPoints = segmentListPoints.stream() .sorted(byDistance) .collect(Collectors.toCollection(ArrayList::new)); Vector2D tempPosPrevious = null; for(Vector2D tempPosCurrent : segmentListPoints) { if(tempPosPrevious == null) { tempPosPrevious = tempPosCurrent; continue; } if(!tempPosPrevious.equals(tempPosCurrent)) { segmentList.add(GeometryFactory.createSegment(tempPosPrevious, tempPosCurrent)); tempPosPrevious = tempPosCurrent; } } return segmentList; }
Segment2D extends Geometry2D { public ArrayList<Segment2D> getSegmentSplitByPolygon(Polygon2D polygon) { ArrayList<Segment2D> segmentList = new ArrayList<>(); ArrayList<Vector2D> segmentListPoints = new ArrayList<>(); segmentListPoints.add(this.getFirstPoint()); segmentListPoints.addAll(this.getIntersection(polygon)); segmentListPoints.add(this.getLastPoint()); Comparator<Vector2D> byDistance = (e1, e2) -> Double.compare( e1.distance(this.getFirstPoint()), e2.distance(this.getFirstPoint())); segmentListPoints = segmentListPoints.stream() .sorted(byDistance) .collect(Collectors.toCollection(ArrayList::new)); Vector2D tempPosPrevious = null; for(Vector2D tempPosCurrent : segmentListPoints) { if(tempPosPrevious == null) { tempPosPrevious = tempPosCurrent; continue; } if(!tempPosPrevious.equals(tempPosCurrent)) { segmentList.add(GeometryFactory.createSegment(tempPosPrevious, tempPosCurrent)); tempPosPrevious = tempPosCurrent; } } return segmentList; } }
Segment2D extends Geometry2D { public ArrayList<Segment2D> getSegmentSplitByPolygon(Polygon2D polygon) { ArrayList<Segment2D> segmentList = new ArrayList<>(); ArrayList<Vector2D> segmentListPoints = new ArrayList<>(); segmentListPoints.add(this.getFirstPoint()); segmentListPoints.addAll(this.getIntersection(polygon)); segmentListPoints.add(this.getLastPoint()); Comparator<Vector2D> byDistance = (e1, e2) -> Double.compare( e1.distance(this.getFirstPoint()), e2.distance(this.getFirstPoint())); segmentListPoints = segmentListPoints.stream() .sorted(byDistance) .collect(Collectors.toCollection(ArrayList::new)); Vector2D tempPosPrevious = null; for(Vector2D tempPosCurrent : segmentListPoints) { if(tempPosPrevious == null) { tempPosPrevious = tempPosCurrent; continue; } if(!tempPosPrevious.equals(tempPosCurrent)) { segmentList.add(GeometryFactory.createSegment(tempPosPrevious, tempPosCurrent)); tempPosPrevious = tempPosCurrent; } } return segmentList; } Segment2D(Vector2D point1, Vector2D point2);   Segment2D(List<Vector2D> nodeArray);   Segment2D(Vector2D[] nodeArray); }
Segment2D extends Geometry2D { public ArrayList<Segment2D> getSegmentSplitByPolygon(Polygon2D polygon) { ArrayList<Segment2D> segmentList = new ArrayList<>(); ArrayList<Vector2D> segmentListPoints = new ArrayList<>(); segmentListPoints.add(this.getFirstPoint()); segmentListPoints.addAll(this.getIntersection(polygon)); segmentListPoints.add(this.getLastPoint()); Comparator<Vector2D> byDistance = (e1, e2) -> Double.compare( e1.distance(this.getFirstPoint()), e2.distance(this.getFirstPoint())); segmentListPoints = segmentListPoints.stream() .sorted(byDistance) .collect(Collectors.toCollection(ArrayList::new)); Vector2D tempPosPrevious = null; for(Vector2D tempPosCurrent : segmentListPoints) { if(tempPosPrevious == null) { tempPosPrevious = tempPosCurrent; continue; } if(!tempPosPrevious.equals(tempPosCurrent)) { segmentList.add(GeometryFactory.createSegment(tempPosPrevious, tempPosCurrent)); tempPosPrevious = tempPosCurrent; } } return segmentList; } Segment2D(Vector2D point1, Vector2D point2);   Segment2D(List<Vector2D> nodeArray);   Segment2D(Vector2D[] nodeArray); List<Segment2D> getLineSegments(); @Override boolean equals(Object other); @Override synchronized List<Vector2D> getVertices(); double getLength(); double getLenghtDistance(); ArrayList<Vector2D> getIntersection(Segment2D openPolygon); ArrayList<Segment2D> getSegmentSplitByPolygon(Polygon2D polygon); @Override ArrayList<Vector2D> getIntersection(Polygon2D polygon); @Override ArrayList<Vector2D> getIntersection(Cycle2D cycle); Vector2D getFirstPoint(); Vector2D getLastPoint(); @Override boolean contains(Vector2D point); @Override boolean contains(Vector2D point, Transform transform); Vector2D getPointOnSegmentClosestToVector(Vector2D point); @Override void rotate(double theta, double x, double y); @Override void rotate(double theta); @Override void rotate(double theta, Vector2D point); @Override void translate(double x, double y); @Override void translate(Vector2D vector); @Override double distanceBetween(Vector2D point); @Override double minimalDistanceBetween(List<Vector2D> points); @Override Vector2D vectorBetween(Vector2D point); @Override Mass createMass(double density); @Override List<Segment2D> getSegments(); @Override double getRadius(); @Override double getRadius(Vector2D vector); @Override Vector2D getCenter(); @Override Vector2D getPointClosestToVector(Vector2D toVector); @Override boolean isOnLines(Vector2D vector, double precision); @Override boolean isOnCorners(Vector2D vector, double precision); List<Vector2D> getNormals(); List<Segment2D> getLineSegmentsSplit(double maxLength); List<Segment2D> getLineSegmentsSplitEqually(double maxLength, Double roundPrecision); ArrayList<Segment2D> calculateLineSegmentsToEquallySplit(double maxLength, Double roundPrecision); Double getSmallestVertixValueOfX(); Double getSmallestVertixValueOfY(); Double getLargestVertixValueOfX(); Double getLargestVertixValueOfY(); @Override double area(); }
Segment2D extends Geometry2D { public ArrayList<Segment2D> getSegmentSplitByPolygon(Polygon2D polygon) { ArrayList<Segment2D> segmentList = new ArrayList<>(); ArrayList<Vector2D> segmentListPoints = new ArrayList<>(); segmentListPoints.add(this.getFirstPoint()); segmentListPoints.addAll(this.getIntersection(polygon)); segmentListPoints.add(this.getLastPoint()); Comparator<Vector2D> byDistance = (e1, e2) -> Double.compare( e1.distance(this.getFirstPoint()), e2.distance(this.getFirstPoint())); segmentListPoints = segmentListPoints.stream() .sorted(byDistance) .collect(Collectors.toCollection(ArrayList::new)); Vector2D tempPosPrevious = null; for(Vector2D tempPosCurrent : segmentListPoints) { if(tempPosPrevious == null) { tempPosPrevious = tempPosCurrent; continue; } if(!tempPosPrevious.equals(tempPosCurrent)) { segmentList.add(GeometryFactory.createSegment(tempPosPrevious, tempPosCurrent)); tempPosPrevious = tempPosCurrent; } } return segmentList; } Segment2D(Vector2D point1, Vector2D point2);   Segment2D(List<Vector2D> nodeArray);   Segment2D(Vector2D[] nodeArray); List<Segment2D> getLineSegments(); @Override boolean equals(Object other); @Override synchronized List<Vector2D> getVertices(); double getLength(); double getLenghtDistance(); ArrayList<Vector2D> getIntersection(Segment2D openPolygon); ArrayList<Segment2D> getSegmentSplitByPolygon(Polygon2D polygon); @Override ArrayList<Vector2D> getIntersection(Polygon2D polygon); @Override ArrayList<Vector2D> getIntersection(Cycle2D cycle); Vector2D getFirstPoint(); Vector2D getLastPoint(); @Override boolean contains(Vector2D point); @Override boolean contains(Vector2D point, Transform transform); Vector2D getPointOnSegmentClosestToVector(Vector2D point); @Override void rotate(double theta, double x, double y); @Override void rotate(double theta); @Override void rotate(double theta, Vector2D point); @Override void translate(double x, double y); @Override void translate(Vector2D vector); @Override double distanceBetween(Vector2D point); @Override double minimalDistanceBetween(List<Vector2D> points); @Override Vector2D vectorBetween(Vector2D point); @Override Mass createMass(double density); @Override List<Segment2D> getSegments(); @Override double getRadius(); @Override double getRadius(Vector2D vector); @Override Vector2D getCenter(); @Override Vector2D getPointClosestToVector(Vector2D toVector); @Override boolean isOnLines(Vector2D vector, double precision); @Override boolean isOnCorners(Vector2D vector, double precision); List<Vector2D> getNormals(); List<Segment2D> getLineSegmentsSplit(double maxLength); List<Segment2D> getLineSegmentsSplitEqually(double maxLength, Double roundPrecision); ArrayList<Segment2D> calculateLineSegmentsToEquallySplit(double maxLength, Double roundPrecision); Double getSmallestVertixValueOfX(); Double getSmallestVertixValueOfY(); Double getLargestVertixValueOfX(); Double getLargestVertixValueOfY(); @Override double area();  }