-2

I'm plotting points on a 3D plane so the coordinates are (X,Y,Z), but the height, Y, will be hardcoded so the algorithm doesn't have to include checking for that.

The way I currently plot the points is that on mount, a function is called to generate a random coordinates based on the sin and cos of a random angle. This results in duplicates and points really close together.

I was considering converting the array of positions to a set to remove duplicates, but that would not solve recursively checking each point to ensure a minimum distance.

Can someone help me brainstorm, or offer some guidance in designing this algorithm. Thanks in advance.

Random cooordinate algorithm

jabusir
  • 21
  • 5
  • Please don't post code as an image. – trincot Sep 02 '20 at 19:35
  • I think you need to scale the random numbers as generated. For a range of 0 to 360 degrees multiply the random number by 360. Then round if you do not want fractional results. If you round up, theoretically you can get "360" as a value. Read MSDN documentation you'll realize that you'll never get "360" as a value - but very close! without rounding up. – radarbob Sep 02 '20 at 19:52
  • Interesting radarbob, I'll try that out. Thanks :) – jabusir Sep 02 '20 at 19:54
  • hey @radarbob you smashed it! Your solution worked. Thanks for your help :). Incrementing the angle was the solution to get equidistant points. Incrementing by just the right amount also removed the possibility of duplicates. Thanks again. – jabusir Sep 02 '20 at 22:39

2 Answers2

0

I'm afraid the only way to ensure a minimum distance IS checking each point with each other.

To do this you can set up an empty array and inside a for-loop call a function that generates a random x,y,[z] position which checks the distance from this point to all points inside the array. If it doesn't fail this check put the point inside the array ultimately.

Here's a simple example:

class Point {
  constructor(x, y) {
    this.x = x;
    this.y = y;
  }
}
var points = [];
var numberOfPoints = 10;
var minDistance;
var context = document.getElementById("canvas").getContext("2d");

function randomize() {
  minDistance = parseInt(document.getElementById("minDistanceBox").value);
  points = [];
  for (var a = 0; a < numberOfPoints; a++) {
    points.push(getRandomPoint());
  }

  context.fillStyle = "#dddddd";
  context.fillRect(0, 0, 100, 100);
  context.fillStyle = "#ff0000";
  points.forEach(element => {
    context.beginPath();
    context.arc(element.x, element.y, 4, 0, 2 * Math.PI, true);
    context.fill();
  });
}

function getRandomPoint() {
  var tempPoint = new Point();
  var tempPoint2;
  var failed = false;
  do {
    failed = false;
    tempPoint.x = parseInt(Math.random() * 100);
    tempPoint.y = parseInt(Math.random() * 100);

    for (var a = 0; a < points.length; a++) {
      tempPoint2 = points[a];
      if (Math.sqrt(Math.pow(Math.abs(tempPoint.x - tempPoint2.x), 2) + Math.pow(Math.abs(tempPoint.y - tempPoint2.y), 2)) < minDistance) {
        failed = true;
      }
    }
  }
  while (failed);
  return tempPoint;
}

randomize();


document.getElementById("button").addEventListener("click", randomize);
<canvas id="canvas" width="100" height="100"></canvas>
<br>
<button id="button">randomize</button>
<label for="minDist">Min distance (1-25):</label>
<input type="number" id="minDistanceBox" name="minDist" min="1" max="25" value="25">
obscure
  • 11,916
  • 2
  • 17
  • 36
0

Radarbob's comment gave me a direction to begin searching in, where I stumbled upon this:

https://www.mathopenref.com/coordcirclealgorithm.html

Incrementing the angle by a certain amount each time removes the possibilities for duplicates, as well as creates an equal distance between points.

jabusir
  • 21
  • 5