0

I am using code from How to get a random point on the interior of an irregular polygon? slightly modified in order to draw a random path2D by connecting random points together one after the other inside a polygon Shape. The problem is when the Shape is a non-convex polygon, the path can intersect with the polygon sides. What would be a propper way to stop intersecting and create a path with all its segments inside the Shape area? A solution could be the reordering of points in a way that never intersect with Shape. A similar reordering example might help.

//generate random points inside a polygon Shape


public ArrayList<Point2D> generateDoubleRandomPointList(Shape region, int numberRandomPoints){ 
   

   ArrayList<Point2D> randomPointList = new ArrayList<>();
   for (int i = 0; i < numberRandomPoints; i++) {
      
  
          Rectangle2D r = region.getBounds();
    double x = 0, y = 0;
    Random rand = new Random();
          if(!r.isEmpty()) { // prevent null exception
           
           do {
        x = (double) (r.getX() + rand.nextDouble( (double) r.getWidth() ));
        y = (double) (r.getY() + rand.nextDouble( (double) r.getHeight() ));        
    }  while(!region.contains(x,y));
       }
       else{

       //nothing here
       }  

randomPointList.add(new Point2D.Double(x,y)); // new
   }

    return randomPointList;
}

// create the path
        public  static Path2D createPathFromListOfPoints2D(List<Point2D> points) { 
          
          // Create a Path2D object
      java.awt.geom.Path2D path = new java.awt.geom.Path2D.Double();  
    
             Point2D p = points.get(0);
             double xΑ = p.getX();
             double yΑ = p.getY();
    
               // Start at the first coordinate given
      path.moveTo(xΑ, yΑ);
      // Loop over the remaining coordinates:
      for(int i=1; i<points.size(); i++) {
         p = points.get(i);
         
              
        // And draw lines from corner to corner
        path.lineTo(p.getX(), p.getY());
         
      }
            // After the loop, close the path to finish the polygon
          path.closePath();
          return path;
       
        }
jazznbass
  • 24
  • 6
  • You can build visibility graph for your points and get any Euler path (if exists) – MBo Feb 15 '23 at 02:29
  • thanks for this suggestion, I will take a look at JGraphT library and see if I can get a way around it. – jazznbass Feb 16 '23 at 12:20

0 Answers0