0

I'm trying to tackle the following use-case:

Input: A list of x,y-coordinates that will form an irregular polygon (they are guaranteed valid and lines will not intersect; it can however be concaved). Input-format is irrelevant, so I'm currently using two loose int-arrays for x and y respectively.

Output: A x,y-coordinate within this polygon, which is NOT directly on top of a corner nor edge.

Using the following code, I have been able to tackle the random x,y-coordinate within the polygon:

// Currying Function with two int-arrays as parameters and double-pair return-type
X->Y->{
  // Create a Path2D object
  java.awt.geom.Path2D path = new java.awt.geom.Path2D.Double();
  // Start at the first coordinate given
  path.moveTo(X[0], Y[0]);
  // Loop over the remaining coordinates:
  for(int i=1; i<X.length; i++)
    // And draw lines from corner to corner
    path.lineTo(X[i], Y[i]);
  // After the loop, close the path to finish the polygon
  path.closePath();
  // Create a Rectangle that encapsulates the entire Path2D-polygon
  java.awt.Rectangle rect = s.getBounds();
  // The resulting x,y-coordinate, starting uninitialized
  double x,y;
  // Do-while the Path2D polygon does not contain the random x,y-coordinate:
  do{
    // Select a random x,y-coordinate within the Rectangle
    x = rect.getX() + Math.random()*rect.getWidht();
    y = rect.getY() + Math.random()*rect.getHeight();
  }while(!path.contains(x,y));
  // After which we return this random x,y-coordinate as result:
  return new double[]{x,y};
}

This all works as intended. Now I want to make sure that the random x,y-coordinate is not one of the input x,y-coordinates (this is pretty easily) AND is not on an edge/line of the Path2D. This second part I'm not sure how to tackle, and a quick google search wasn't given any useful information, hence this question.

NOTE: I know the chances that the random point is exactly on top of an edge/corner are astronomical small and could probably be ignored, but this is for a challenge, hence the need to implement it regardless.

Kevin Cruijssen
  • 9,153
  • 9
  • 61
  • 135
  • 1
    Use `Line2D` and its `contains()` method to see if the random point is "on" a line segment. – Andreas Apr 24 '20 at 08:35
  • @Andreas Oh, that indeed sounds very useful. Is there a way to get all `Line2D` that form the `Path2D`? Or should I just loop over pairs of the input-arrays to create the `Line2D`s? – Kevin Cruijssen Apr 24 '20 at 08:48
  • 1
    You can use a `PathIterator` to see all the `lineTo` segments of the path, but it would be a lot easier to just create the `Line2D` objects from your `X` and `Y` arrays. – Andreas Apr 24 '20 at 08:53
  • @Andreas Yeah, I knew about `PathIterator`. Thanks thought! I've created a loop over the pair of input-coordinates to create the `Line2D`. If you want you can post it as an answer, then I will accept it. – Kevin Cruijssen Apr 24 '20 at 08:55
  • Nah. You can self-answer, if you feel the answer would be useful for other people. Otherwise just delete the question. – Andreas Apr 24 '20 at 08:56

1 Answers1

0

After the tip of @Andreas in the comments to use a Line2D, I've modified the do-while to:

// The resulting x,y-coordinate, starting uninitialized
double x,y;
// Flag to indicate whether the random x,y-coordinate is on a corner or edge, starting truthy
boolean flag;
// Do-while the Path2D polygon does not contain the random x,y-coordinate:
do{
  // Select a random x,y-coordinate within the Rectangle
  x = rect.getX() + Math.random()*rect.getWidht();
  y = rect.getY() + Math.random()*rect.getHeight();
  // Set the flag to false:
  flag = false;
  // Loop over the pair of x,y-coordinates of the input:
  for(int j=0; j<X.length; )
    // Create a Line2D using the current pair of x,y-coordinates:
    if(new java.awt.geom.Line2D.Double(X[j],Y[j++], X[j],Y[j])
    // And if it contains the random x,y-coordinate:
       .contains(x,y))
      // Change the flag to true
      flag = true;
}while(!path.contains(x,y) || flag);
Kevin Cruijssen
  • 9,153
  • 9
  • 61
  • 135