Assume that I have a vector with 6 distance elements as
D = [10.5 44.8 30.01 37.2 23.4 49.1].
I'm trying to create random pair positions of a given distances, inside a 200 meters circle. Note that the distance D created by using (b - a).*rand(6,1) + a
, with a = 10
and b = 50
in Matlab. I do not know how to generate the random pairs with given the distances.
Could anybody help me in generating this kind of scenario?
You can tackle the problem using a two-steps approach. You can
D
D
from the first point previously created and by random selection of one of these candidates you'll have the second pointLet's see with an example: let's say you main circle has radius
200
and its center is(0,0)
so we start by declaring some main variables.Let's now consider the first distance,
D(1)=10.5
and we now generate the first random point which (along with its paired point - I reckon you don't want one point inside and the other outside of the main circle) must lie inside the main circleand at the end of this loop
x
andy
will be our first point coordinates.Now we shall generate all its neighbours, thus several candidates to be the second point in the pair. As said before, these candidates will be the points that lie on the circle whose center is
(x,y)
and whose radius isD(1)=10.5
. We can create these candidates as follows:Now
xp
andyp
are two vectors that contain, respectively, the x-coordinates and y-coordinates of our candidates, thus we shall now randomly select one of theseFinally, the pair
The red circle is the main area (center in (0,0) and radius 200), the red asterisk is the first point
(x,y)
is the first point and the pair(secondPoint_x, secondPoint_y)
is our second point. The following plot helps summarizing these steps:(x,y)
the blue little circle has center(x,y)
and radius10.5
and finally the black asterisk is the second point of the pair(secondPoint_x, secondPoint_y)
, randomly extracted amongst the candidates lying on the blue little circle.You must certainly can repeat the same process for all elements in
D
or rely on the following code, which does the very same thing without iterating through all the elements inD
.Now![enter image description here](https://i.stack.imgur.com/a3COC.png)
x
andy
(andsecondPoint_x
andsecondPoint_y
by extension) will be vector of length 6 (because 6 are the distances) in which the i-th element is the i-th x (or y) component for the first (or second) point.This is an improvement to Alessiox's answer. It follows same logic, first generate a set of points (
[X1 Y1]
) that have at least distanceD
from the main circle border, then generate the second set of points ([X2 Y2]
) that have exact distanceD
from the first set.