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,353
  • 1
  • 32
  • 47
wtrclr
  • 105
  • 7

2 Answers2

7

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,166
  • 6
  • 49
  • 73
-1

This is a simple math question.

x2 = x1 - k + 2k*new Random().nextDouble()

Now you can calculate y2.

Nick Allen
  • 1,383
  • 12
  • 19