Placing Circles into a given area - matlab

I have a given area (let's say 1000 x 1000), and I want to place circles in this area with the following requirements:
The number of circles is arbitrary, but fixed after it was chosen at the begin of the algorithm. The number should be so that most of the area is covered by the circles.
The circles shall have in general different radii, and the sizes of the radii shall be in a certain interval (e. g. between 20 and 80).
The circles shall not overlap.
I want to implement a code which does this with matlab. So far I have accomplished this code, which contains for simplicity only one value for the radius:
%% Area
perix=1000;
periy=1000;
%Number of circles
numbercircles=100;
radii(1:numbercircles)=70
%% Placing Circles
%first circle
xi=rand*perix; % array for storing x-values of circle centers
yi=radii(1); %array for storing y-values of circle centers
radiusarray=[radii(1)] ; %array for storing radii
plot(sin(linspace(0,2*pi,100))*radii(1)+xi,cos(linspace(0,2*pi,100))*radii(1)+yi);
hold on
axis([0 perix 0 perix])
% Idea:
%* Step 1: Random x coordinate for each circle middle point, y-coordinate at
% the top of the area, y_init=periy, and given radius.
%* Step 2: Lower y coordinate with constant x-coordinate until the distance to
%neighbour spheres is lower than the combined radii of those spheres.
%* Step 3: Move sphere on the x-axis to decrease the distance and further
%decrease the y-value if possible.
for lauf=2:numbercircles;
disp(numbercircles-lauf)
deltaz=10;
%% Step 1
% random x coordinate of sphere
x1=rand*100;
% y coordinate of circle is on the edge of the area and will be
% lower in the following
y1=periy;
Radnew=radii(lauf);
%% Step 2
%distance to other circle
d=min([sqrt((xi-x1).^2+(yi-y1).^2)-(Radnew+radiusarray) sqrt(((xi+perix)-x1).^2+(yi-y1).^2-(Radnew+radiusarray)) sqrt(((xi-perix)-x1).^2+(yi-y1).^2)-(Radnew+radiusarray)]);
while deltaz > 1e-4
%lower till y1=Radnew or distance to other spheres < 2*Rad
while ((y1>Radnew) & (d > deltaz))
%number=number+1
% lower y1
% if a<2
% deltaz
% end
y1=y1-deltaz;
% recalculate distance to all other spheres
d=min([sqrt((xi-x1).^2+(yi-y1).^2)-(Radnew+radiusarray) sqrt(((xi+perix)-x1).^2+(yi-y1).^2)-(Radnew+radiusarray) sqrt(((xi-perix)-x1).^2+(yi-y1).^2)-(Radnew+radiusarray)]);
end;
dmaxakt=d;
%adjust position in x direction and y direction: Increasing
%x coordinate iteratively in small steps
if (y1>Radnew)
xz(1)=x1+deltaz*rand;
if xz(1)>perix
xz(1)=x1-perix;
elseif xz(1)<0
xz(1)=x1+perix;
end;
dz(1)=min([sqrt((xi-xz(1)).^2+(yi-y1).^2)-(Radnew+radiusarray) sqrt(((xi+perix)-xz(1)).^2+(yi-y1).^2)-(Radnew+radiusarray) sqrt(((xi-perix)-xz(1)).^2+(yi-y1).^2)-(Radnew+radiusarray)]);
xz(2)=x1-deltaz*rand;
if xz(2)>perix
xz(2)=x1-perix;
elseif xz(1)<0
xz(2)=x1+perix;
end;
dz(2)=min([sqrt((xi-xz(2)).^2+(yi-y1).^2)-(Radnew+radiusarray) sqrt(((xi+perix)-xz(2)).^2+(yi-y1).^2)-(Radnew+radiusarray) sqrt(((xi-perix)-xz(2)).^2+(yi-y1).^2)-(Radnew+radiusarray)]);
%determine which distance z is the largest
vmax=find(max(dz)==dz);
%set the x-value to the value which belongs to the largest
%distance
x1=xz(vmax(1));
end;
%calculate new distance
d=min([sqrt((xi-x1).^2+(yi-y1).^2)-(Radnew+radiusarray) sqrt(((xi+perix)-x1).^2+(yi-y1).^2)-(Radnew+radiusarray) sqrt(((xi-perix)-x1).^2+(yi-y1).^2)-(Radnew+radiusarray)]);
if ((d>dmaxakt) & (y1>Radnew))
dmaxakt=d;
else
deltaz=deltaz*1e-1;
end;
end;
% if (y1<1e-5)
% y1=rand;
% end;
%last check: test if y-coordinate is still in the area
if (y1<periy-Radnew)
%Assembling the arrays for the circle places
xi=[xi x1];
yi=[yi y1];
radiusarray=[radiusarray Radnew];
end;
%Plotting
%zeit(lauf)=cputime-t;
plot(sin(linspace(0,2*pi,20))*Radnew+x1,cos(linspace(0,2*pi,20))*Radnew+y1);
%plot(sin(linspace(0,2*pi,20))*Rad1+x1+perix,cos(linspace(0,2*pi,20))*Rad1+y1);
%plot(sin(linspace(0,2*pi,20))*Rad1+x1-perix,cos(linspace(0,2*pi,20))*Rad1+y1);
hold on
axis([0 perix 0 perix])
pause(0.0001);
saveas(gcf, 'circle.png')
end;
The code basically assumes an initial x-coordinate and the maximum y-coordinate and lowers the y-coordinate until overlap is detected. Then the x-coordinate and the y-coordinate are modified to achieve high density of the circles.
The problem of this code is, that it is very slow, because the distance of lowering the y-coordinate is decreasing in every while-loop, which means that the time of lowering the spheres can be very long. I would appreciate if somebody could come up with an idea how to increase the speed of this code.

Not sure if this helps, but i checked your code as you posted it with the profiler and it says 98% of the time is saving the .png file
Line Number 111 / saveas(gcf, 'circle.png') / 99 / 47.741s / 98.4%
Do you want a picture everytime a new circle is drawn or just the last one? In the ladder case just put the 'saveas(...)' behind the last end, and caluclation is down to 1% of the time

Related

Keystoning in Matlab - projecting a square on the floor

So, as a part of an experiment I need a function that could use the projector to project something on the floor. The projector is angled so the Keystone effect comes in to play and I'd like to correct it.
I try to do that by calculating the homography matrix H. I project a square at the bottom of the projected area, measure its properties (it is shown as a trapezoid on the ground) and calculate the matrix. And then I reverse the process with the idea to use the homography matrix H (it's inverse) to calculate and project a shape on the ground that would look like the original square and not a trapezoid.
But I'm not successful. I calculate a shorter quadrangle and still get a trapeze on the ground. I'm pretty sure I'm doing something wrong with the calculations but I'm not sure. I am new to Matlab :) Thx for the help. The code, with a lot of comments, is bellow.
function keystoning()
projArea = 100; % 100%
testSquare = 20; % test square dimension
%% Drawing a "what's supposed to be a square" on the floor
% black background, red "sqare" dimensions 20x20, attached to the base
% of the projection area so the base dimension of the "sqare" don't change
% and minimal measuring is required
testX=[40 40+testSquare 40+testSquare 40];
testY=[0 0 testSquare testSquare];
scrsz = get(groot,'screensize');
f = figure('position',[0 0 scrsz(3) scrsz(4)],...
'OuterPosition',[0 0 scrsz(3) scrsz(4)],...
'toolbar','none','menubar','none','name','none',...
'units','normalized',...
'Color','black','Resize','Off'...
);
figure(f); % focus window
fill(testX, testY,'r');
axis ([0 projArea 0 projArea])
pbaspect([1 1 1])
drawnow
disp('*** PLACE THE NEAR PART OF THE ELEMENT AT THE END OF THE TREADMILL ***');
%% Measuring of the shape in order to calculate the homography matrix H
disp('*** Measure the width of the base of the projection area in centimeters. ***');
prompt = 'Enter the measured distance: ';
projAreaM = input(prompt);
projAreaCoef=projAreaM/projArea; % coefficient between the axes scale and the projection area
disp('*** Measure the distance between the paralel lines of the trapezoid in centimeters. ***');
prompt = 'Enter the measured distance: ';
trapYm = input(prompt)/projAreaCoef
disp('*** Measure the length of the longer paralel line of the trapezoid in centimeters. ***');
prompt = 'Enter the measured length: ';
trapXm = input(prompt)/projAreaCoef
%% Calculating the homography matrix H
% trapezoid definition
% coordinates of the first two points for the trapezoid and the square
% are the same since its at the start of the projection area
difX=(trapXm-testSquare)/2; % length of the "pertrusions"
trapX=[testX(1) testX(2) testX(3)+difX testX(4)-difX];
trapY=[testY(1) testY(2) trapYm trapYm];
% variable definition
P=[testX;testY;ones(size(testX))]; % test rectangle
Q=[trapX;trapY;ones(size(trapX))]; % shown trapezoid
% homography matrix H calculation
H=Q/P; % solution to the equation HP = Q
%% Testing the homography matrix H
% we want to show the rectangle on the ground
Pnew=inv(H)*P;
fill(Pnew(1,:),Pnew(2,:),'r');
axis ([0 projArea 0 projArea])
pbaspect([1 1 1])
drawnow

Create equidistant points around a circle, based on Euclidean distance: MATLAB

I want N points on a circle, set apart by a Euclidean distance (straight line, not around the circumference) of 50. The radius is determined by the number of points and the distance between consecutive points.
However, when I choose one of the points as a reference and calculate the distances to other points, I do not get any distance values equal to 50.
Below is my code:
N = 100; % number of points
eclddst = 50; % euclidean distance between adjacent points
r = eclddst/(2*sin(pi/N)); % radius of the circle
cord = r*exp((0:1/(N-1):1)*pi*2*1i)'; % coordinates
XCor = real(cord);
YCor = imag(cord);
N_COORD = [XCor YCor];
% taking location 3 as the reference point to check the distance between the points
DSTNT = sqrt( (N_COORD(3,1)-N_COORD(:,1)).^2 + ( N_COORD(3,2)- N_COORD(:,2)).^2)';
The distance values around the third point which I obtain are:
100.959
50.505
0.000
50.505
100.959
151.311
The points adjacent to point 3 should have 50 as their distance value and not 50.505.
Why do I get this error?
Thanks in advance.
Your issue is in the number of points which your formula generates, because of a misleading statement at the link you were following. Note that it was stated:
r = A*exp((0:1/300:1)*pi*2j); % 300 point circle, radius A
However, this will give 301 points not 300. If you plotted this though, you would see only 300 points (much easier to see if you use, say, 4 points). The first and last points are identical since
exp(0*pi*2j) = exp(1*pi*2j) = 1
To get around this, the simplest options are to change 0:1/300:1 to not reach 1, or simply create and remove an extra point as shown here:
N = 100; % number of points
d = 50; % Euclidean distance between adjacent points
% Euclidean distance around circle is essentially side length of N-sided polygon.
% Triangular sector angle within polygon (rads): 2*pi/N
% By bisecting the triangle to get a right-triangle, we can deduce that
% sin((2*pi/N)/2) = (d/2)/r => r = (d/2)/sin(pi/N)
r = (d/2)/sin(pi/N);
% Use linspace as it's clearer than colon array, create N+1 points
% Note that point 1 and point N+1 will be identical!
complexcoords = r*exp(linspace(0,1,N+1)*pi*2*1i).';
% Remove the last point as you put it in an array
coords = [real(complexcoords(1:N)) imag(complexcoords(1:N))];
As a check:
% Euclidean distances from point 3
dists = sqrt((coords(3,1)-coords(:,1)).^2 + (coords(3,2)-coords(:,2)).^2);
dists(1:5)
>> 99.951
50 % Neighbouring points are distance 50!
0
50 % Ditto
99.951
Note that you should be careful using '. This is the complex conjugate transpose, meaning that x + yi becomes x - yi. You won't notice this on a circle centred around 0 and an even number of points, but when you want to transpose something in MATLAB always use .', otherwise you may get some hard to diagnose issues! I have corrected this in my above code.
Doc links: ctranspose/', transpose/.'.

The Monte Carlo method for estimating pi in MATLAB

I want to estimate the value of pi using the Monte Carlo method, this is, A random number generator can be used to estimate the value of pi. So I have found the following code
n=input('Number of points: ');
x=rand(n,1);
y=rand(n,1);
figure('color','white');
hold all
axis square;
x1=x-0.5;
y1=y-0.5; %cirle has centre at (0.5,0.5)
r=x1.^2+y1.^2;
m=0; %Number of points inside circle
for i=1:n
if r(i)<=0.25
m=m+1;
plot(x(i),y(i),'b.');
else
plot(x(i),y(i),'r.');
end
end
m/(0.25*n)
The thing is How Can I modify it so it gives me a square of length 2 and a circle of radius 1?
I've done this so far but it only give a quarter of what is suppose to give me:
n=input('Number of points: ');
x=rand(n,1);
y=rand(n,1);
figure('color','white');
hold all
axis square;
axis ([0 2 0 2])
x1=x-1;
y1=y-1; %cirle has centre at (0.5,0.5)
r=x1.^2+y1.^2;
m=0; %Number of points inside circle
for i=1:n
if r(i)<=1
m=m+1;
plot(x(i),y(i),'b.');
else
plot(x(i),y(i),'r.');
end
end
m/(0.25*n)
NOTE: the method is the following: generate random points in a square with sides of length 2 and count what proportion of these points falls in the unit radius circle that fits exactly into the square.This proportion will be the ratio of the area of the circle to the area of the square, hence, this estimates pi.
Try that
n=input('Number of points: ');
x=2*rand(n,1);
y=2*rand(n,1);
figure('color','white');
hold all
axis square;
x1=x-1;
y1=y-1; %cirle has centre at (1,1)
r=x1.^2+y1.^2;
m=0; %Number of points inside circle
ii = r<=1;
m = sum(ii);
plot(x(ii), y(ii), '.b');
plot(x(~ii), y(~ii), '.r');
m/(0.25*n)

Getting the points of a certain plot in matlab

If I have points in a graph in matlab that are randomly sorted and when plotted produce various closed shapes. Given a specific point on the left side of one of the closed shapes how can I get all the points of that shape in a vector form keeping in mind the convention of collecting the points is moving in a clockwise direction.
A commented example:
cla
% Random data
x = rand(15,1);
y = rand(15,1);
scatter(x,y)
% Random point to the left
hold on
p = [-0.1, rand(1)*0.2 + 0.5];
plot(p(1),p(2),'*r')
% Separate points that lie above your point
idx = y >= p(2);
% Sort x then y increasing for upper half and x then y decreasing for lower half
shape = [sortrows([x( idx) y( idx)],[1 2])
sortrows([x(~idx) y(~idx)],[-1 -2])];
Check that shape contains clockwise sorted coordinates by plotting an open line:
% Plot clockwise open line
plot(shape(1:end ,1),shape(1:end,2),'k')

Calculate circular bins around a point + matlab

My question is related to this link stackoverflow ques
In essence repeating the figure drawn there .. I have a central point ( x , y ) in an image around which I have to draw 4 circles of 1-4 unit radius with 8 angles between them.
In this diagram there are 12 angle bins but I have 8. There is a code solution there but it is for plotting the above figure.
I want to calculate the maximum intensity point in each of the 4 regions of each wedge. Is there any inbuilt function in matlab ? I looked at rose but could'nt understand if it would help me....
I would greatly appreciate if someone could help me how to calculate it in matlab....
Thanks
I put some code below that should be the basic skeleton of what you want to do. But I left an important function unimplemented because I think you will be able to do it and it will help you understand this process better.
% I assume that data_points is an M-by-2 array, where each row corresponds
% to an (x,y) coordinate pair, and M is the number of data points.
data_points = ... ;
% I assume this array stores the intensities at each data point.
intensities = ... ;
% I assume that this stores the total number of gridded polar regions you want
% to find the max intensity in (i.e. 4*(number of cells) in your picture above).
total_num_bins = ... ;
% This will store the max intensities. For places that have no nearby
% data points, the max intensity will remain zero.
max_intensities = zeros(total_num_bins);
% I assume these store the values of the center point.
x = ... ; y = ... ;
% The number of different data points.
num_data_points = length(intensities); % also equals size(data_points,1)
% Now, loop through the data points, decide which polar bin they fall in, and
% update the max intensity of that area if needed.
for ii = 1:num_data_points
% Grab the current point coordinates.
cur_x = data_points[ii,1];
cur_y = data_points[ii,2];
% Convert the current data point to polar coordinates,
% keeping in mind that we are treating (x,y) like the center.
cur_radius = sqrt( (cur_x - x)^2 + (cur_y - y)^2 );
cur_angle = atan2(cur_y - y, cur_x - x)
% You have to write this yourself, but it
% will return an index for the bin that this
% data point falls into, i.e. which of the 4 segments
% of one of the radial cells it falls into.
cur_bin = get_bin_number(cur_radius, cur_angle);
% Check if this data point intensity is larger than
% the current max value for its bin.
if ( intensities(ii) >= max_intensities(cur_bin))
max_intensities(cur_bin) = intensities(ii);
end
end
You will now have to make the function get_bin_number() which takes as its input the angle and radius of the data point away from the center point. It should return just an index between 1 and total_num_bins, because you will be keeping the max intensities in a linear array. So, for example, index number 1 might correspond to the first 1/4 piece of the closest radial cell in the upper right quadrant, index 2 might correspond to the next 1/4 of that same cell, moving counter-clockwise, or something like this. You have to devise your own convention for keeping track of the bins.
A late answer, but I believe an even easier solution would just be to convert your data from (x,y) coordinates to (r,theta) by using (r = sqrt(x.^2 + y.^2), theta = atan(y,x)) then use the hist3 function on the (r,theta) data set to get a radial histogram.
Therefore solution is as follows:
% I assume you have some M-by-2 matrix X that's in the form (x,y)
% Convert (x,y) to (r,theta)
xVect = X(:,1);
yVect = X(:,2);
X = [sqrt(xVect.^2 + yVect.^2), ...%formula for r
atan(yVect,xVect)]; %formula for theta
% 5 is the number of wedges along 'r', your radial axis
% 12 is the number of wedges along 'theta', your theta 'axis'
dist = hist3(X,5,12);
Even if you have solved this, I hope this helps anybody else who wants to create a radial/angular histogram!