0

Edit: I've left this question here, though I've subsequently figured out the problem was the value I was sending as minDistance was the problem. The code below works as expected with no weird behaviour.

I'm stumped by (too me) weird behaviour from a JavaScript calculation. What I want to do is get a random point (whose coordinates I've called x2 and y2) which is a minimum distance from a given point (x1 and y1). My code below usually works, but in about 1 in 20 tests it returns a point too close.

This isn't simply a floating point rounding error since the incorrect return points are often under half the minimum distance away.

function random_distance(x1, y1, minDistance) {
  let x2;
  let y2;
  let d;
  do {
    x2 = Math.random() * (canvas.width + 1);
    y2 = Math.random() * (canvas.height + 1);
    d = Math.hypot(x2 - x1, y2 - y1);
  } while (d < minDistance);
  return [x2, y2];
}

Checks I've put within the function seem to believe the x2, y2 points are valid, so I suspect this is some referencing problem where the returned [x2, y2] array doesn't contain the last calculated values, but my JavaScript knowledge isn't sufficient to figure out what's going on here.

joeblog
  • 1,101
  • 11
  • 17
  • canvas is not defined – DCR Jul 31 '20 at 18:24
  • 1
    1) try using `Math.hypot()`; 2) check if arguments are passed in correct order – Vasily Liaskovsky Jul 31 '20 at 18:27
  • you could at least try to output the values in your console with console.log(variable). Then you would at least find out what values cause your problem – Christoph Kern Jul 31 '20 at 18:30
  • I wasn't aware of Math.hypot(), so thanks Vasily Liaskovsky for introducing me to it. Unfortunately, that hasn't solved the problem. canvas is globally defined, so that's not the problem. – joeblog Jul 31 '20 at 18:33
  • What about the comparison `d < minDistance`? Could it be that it's false when you expect it to be true? Are you sure that JS is not [coercing types](https://stackoverflow.com/questions/19915688/what-exactly-is-type-coercion-in-javascript) in an unexpected way? – Pipetus Jul 31 '20 at 20:32

1 Answers1

0

The bug turned out to be a typo in how I calculate minDistance, so JavaScript actually doesn't do anything weird here at all.

Apologies for the false alarm.

joeblog
  • 1,101
  • 11
  • 17