random points
2 次查看(过去 30 天)
显示 更早的评论
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;
2 个评论
Jan
2011-2-27
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.
回答(2 个)
bym
2011-2-27
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
4 个评论
Walter Roberson
2011-2-27
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.
6 个评论
Walter Roberson
2011-2-28
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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!