0

I believe I need a solution using PHP for the following problem. Let's start and say we have a map, that width is 100000 and height 100000.

I'd have a region into that map, designed by many X / Y / Z coordinates. something like:

{{-56000;190073;-4509};{-54955;190073;-4509};{-54954;190638;-4509}{-56000;190638;-4509}}

That's 4 points forming a square on our map. But the zones can be defined by 10+ points, so nothing like squares.

Now I'd need a way to generate N different random coordinates that are INSIDE that region.

I don't know where and how to start with this problem, but I know how to use PHP. Just actually lacking the theory part. What algorithm could I use?

halfer
  • 19,824
  • 17
  • 99
  • 186
Psychokiller1888
  • 620
  • 2
  • 10
  • 25
  • 1
    possible duplicate of [Random points inside a Polygon](http://stackoverflow.com/questions/240778/random-points-inside-a-polygon) – h2ooooooo Apr 23 '14 at 21:13
  • 1
    @nicolascolman That's 4 years old... And no, I did not want to create zones, I was asking to create random points inside a defined zone – Psychokiller1888 Feb 06 '18 at 15:09
  • I will try to close this question as a duplicate of the suggested link above. – halfer May 30 '20 at 19:32

1 Answers1

1

Use the rand function to generate x & y coordinates n the range specified by your bounds:

$x = rand($min_x, $max_x);
$y = rand($min_y, $max_y);

I'm not sure what range you want to use for your z coordinate.

Kryten
  • 15,230
  • 6
  • 45
  • 68
  • (in the instance of the OP all z coordinates are identical and need not to be randomized) – h2ooooooo Apr 23 '14 at 20:06
  • @h2ooooooo thanks. Although looking at the OP's question again, the coordinates listed do not quite form a square :-S – Kryten Apr 23 '14 at 20:07
  • Perhaps it's a skewed/rotated square rather than an exact square? If that's the case there's quite a bit more geometry involved in this question than I originally thought. – h2ooooooo Apr 23 '14 at 20:08
  • Yes... I think it's a typo though. I'm going to assume it's a typo. – Kryten Apr 23 '14 at 20:11
  • Nope it's not, zones can be whatever form they need to be, the exemple was just an exemple, but imagine a star per exemple? – Psychokiller1888 Apr 23 '14 at 20:15
  • 2
    @Psychokiller1888 There's a **huge** difference between finding a random point in a polygon and a random point in a regular square. Perhaps you're looking for [Random points inside a Polygon](http://stackoverflow.com/questions/240778/random-points-inside-a-polygon)? – h2ooooooo Apr 23 '14 at 20:19
  • Yes, it's inside a polygone, we can drop the third dimension. It can be in a square, but can be a polygon too. I'll take a look to your link, thank you – Psychokiller1888 Apr 23 '14 at 21:52