2

I have point A (pointA = (x1, y1)) and I need to choose a random point B (pointB = (x2, y2)) such that the distance between the A and B is equal to K.

nana
  • 4,426
  • 1
  • 34
  • 48
wtrclr
  • 105
  • 8

1 Answers1

8

Let's solve in polar form.

We'll need these doubles distance, x1, and y1.

First, we want the angle in radians:

double angle = Math.random()*2*Math.PI;

Then we want to get the x and y offsets from our point:

double xOff = Math.cos(angle)*distance;
double yOff = Math.sin(angle)*distance;

Then we add these to our first point:

double x2 = x1 + xOff;
double y2 = y1 + yOff;

This will get you a point a certain distance away from your first point.

Anubian Noob
  • 13,426
  • 6
  • 53
  • 75