0

I'm looking to plot random points inside a box with radius of 5 and then implement an algorithm to form a closed polygonal path that passes through all these points.

Right now, I have a for loop to get my x,y coords for each of my points. N is defined by user input for the number of random points we want to display. Next, I plan to plot them using the x and y value. However, I have no idea how to connect them via a closed polygonal path. Any ideas?

 for (int point = 0; point < N; point++ ){
        double x = coords.nextDouble() * 10.0 - 5.0; // x and y coords for random point between -5 and 5
        double y = coords.nextDouble() * 10.0  - 5.0;
Paolo Forgia
  • 6,572
  • 8
  • 46
  • 58
Saskewan
  • 1
  • 2
  • Possible duplicate of http://stackoverflow.com/questions/14263284/create-non-intersecting-polygon-passing-through-all-given-points – G. Fiedler Apr 12 '17 at 09:29

1 Answers1

0

Create a Polygon object outside your for loop. add each point to the Polygon.

java.awt.Polygon myPolygon = new Polygon();
for (int point = 0; point < N; point++ ){
    double x = coords.nextDouble() * 10.0 - 5.0; // x and y coords for random point between -5 and 5
    double y = coords.nextDouble() * 10.0  - 5.0;
    myPolygon.addPoint(x, y);
}
// draw myPolygon
M. Haverbier
  • 383
  • 2
  • 13