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

Learn moreOpportunities for recent engineering grads.

Apply Today
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

Thanks

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;

m=1;

n=0;

for i=1:10

n=n+1;

for k=1:2

m=m+1;

theta = rand*2*pi;

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

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

end

end

plot(x,y,'.');

hold on

axis equal;

*No products are associated with this question.*

Answer by proecsm 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);

plot(in(:,1),in(:,2),'g.') hold plot(out(:,1),out(:,2),'r.') 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.

niko kauppinen on 27 Feb 2011

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

Thanks

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.

Show 3 older comments

Walter Roberson on 28 Feb 2011

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.

## 2 Comments

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/2110#comment_4280

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.

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/2110#comment_4283

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

Thanks