MATLAB Answers


random points

Asked by niko kauppinen on 26 Feb 2011

I want to generate points around area without overlaps. Every point generated i want to generate 2 new ones with random angle and continue until area is filled.

I don't know how to check previously generated points and see is there any overlap


My program is:

clear all

x = zeros(10,1);

y = zeros(10,1);

x(1,1)= 10*rand;

y(1,1)= 10*rand;

radius = 0.5;



for i=1:10


for k=1:2


theta = rand*2*pi;

x(m,1) = x(n,1) + radius*cos(theta);

y(m,1) = y(n,1) + radius*sin(theta);




hold on

axis equal;


Jan Simon
on 27 Feb 2011

What does "until area is filled" mean? If you speak of random DOUBLE variables, you need > 2^53 iterations. There I assume, you want something, which is not explained in your question.

My area is 10x10 in this case
And I would like to generate circles at each point and check does their circumference line overlap with "box" or other circles


Log in to comment.

2 Answers

Answer by bym
on 27 Feb 2011

I would be less concerned about checking overlap. I don't know what you are trying to do, but perhaps this may be of some help

clc;clear;close all
s = rand(10000,2).*.5;
s = [s,bsxfun(@hypot,s(:,1),s(:,2))];
in = s(s(:,3)<.5,1:2);
out = s(s(:,3)>=.5,1:2);
axis square


Show 1 older comment
Jan Simon
on 27 Feb 2011

The chance to get two equal random DOUBLE numbers is very very tiny. If you really need to be sure, that the values are distinct, create a single random vector at first and check the values with UNIQUE.

I would like to generate circles at each point and check does their circumference line overlap with other circles.


on 27 Feb 2011

That is clearer, thanks, you might want to have a look at voronoi(x,y)

Log in to comment.

Answer by Walter Roberson
on 27 Feb 2011

In your original problem statement, you said that you wanted to continue until the area was filled. The code outline you presented had no area boundaries, and thus unless you put a maximum on the number of points, will continue onward towards +/- infinity in the X and Y direction until you run out of memory.

Each circle you generate with have an area of pi*r^2 . Your r is 1/2, so the area will be pi/4 . Given any fixed x and y boundaries (rectangular), the available area will be

(max(x) - min(x)) * (max(y) - min(y))

If the bounds are the same length, say L, then the area would be L^2. You could only fill up the area if L^2 = N * pi/4 where N is the number of circles placed. The number of circles needed would thus be (2*L)^2/pi . This will not be an integer unless L is exactly divisible by sqrt(pi)/2. However, pi is an irrational number and so its square root must be irrational as well. No irrational number is exactly representable in IEEE 754 binary floating point, so it is not possible to fill any area with fixed-radius circles in Matlab (or any computer system with finite precision in any fixed or mixed integer base).

Your problem is thus not possible to solve in the form stated.


where i can save my drawing?
do I need to have own web page?

upload it on tinypic and paste the link as <<http://....>>

To do the check on each point that you have generated previously, vectorize the calculation.

You will need to generate an infinite number of circles of varying radii to fill the 10 x 10 area, under the usual definition of "fill".

Would I be correct in my suspicion that you intend to use a truncated power law distribution for your circle radii? If so, then you will not be able to do that and "fill" the area -- not unless you define quite different stopping criteria for the area being "filled". I have previously written a proof that a truncated power law cannot hold in a bounded area unless the circles are permitted to extend beyond the boundaries.

Log in to comment.

Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!