How to plot connecting circles of different radius
8 次查看(过去 30 天)
显示 更早的评论
Trying to plot circles with different radius (min 1mm, max 5mm) connected to each other at random angles in order to reach a required length of 1000mm. I do not want the circles to overlap, but to touch each other.
My code is attached, and I feel like I am nearly there. Any help would be appreciated!
clc; close all; clear all; %housekeeping
%input constants
t=linspace(0,500000); %timeframe (s)
length = 1
theta = linspace(0, 2 * pi, 360); %setting angles to be considered
Rm = zeros (1,100);
Rm(1) = 0.002+0.005*rand(1,1); %inital random radius of circle (m)
D = 2*Rm(1);
x = Rm(1) * cos(theta); %horizontal value of points
y = Rm(1) * sin(theta); %vertical value of points
figure
plot(x,y)
axis('equal')
grid on
hold on
title('Circle Chain')
xlabel(' width (m)')
ylabel(' height (m)')
centre(:, 1) = [0 Rm(1)];
i = 2;
while (D < length)% keep adding circles till length is reached
Rm(i) = 0.002+0.005*rand(1,1); % setting random radius for future circles
angle(i-1)= %STUCK: setting connecting angle for next circle to join circle before it
X = % want to calculate the x and y coordinates for the centres to store
Y = % want to calculate the x and y coordinates for the centres to store
centre(:,i) = [X Y];
nD = % want to cacluate the additional disatnce created
D = D + nD; % want to calculate the new cumulative distance
x = Rm(i) * cos(theta) + centre(1,i);
y = Rm(1) * sin(theta) + centre(2,i);
plot(x,y)
i = i + 1;
end
2 个评论
Image Analyst
2019-1-2
Help us visualize what you want. Attach a diagram of what you'd like to achieve.
回答(1 个)
Jacob Mathew
2025-5-5
Hey Chanak,
This is a problem thats best approached using Polar Coordinate systems than in terms of x and y axis.
By constraining the minimum and maximum radii of the circle as well as the minimum and maximum variation in degree, you can then start plotting the circles quite easily as for two circles to be tangential, their origins only need to be as far apart as the sum of their radii.
The following sample code demonstrates the above approach
% Parameter constraints
N = 10; % Number of circles
rmin = 0.8; % Minimum possible radius
rmax = 1.5; % Maximum possible radius
degVar = -5; % The angle of variation i.e -degVar to +degVar variation
% Step 1: Generate random radii of circles
radii = rmin + (rmax - rmin) * rand(1, N);
% Step 2: Initialize positions and angles
positions = zeros(N,2); % Each row: [x, y] of circle center
theta = 0; % Initial direction (radians, 0 = along x-axis)
positions(1,:) = [0, 0]; % First circle at origin
% Step 3: Place the remaining circles
for i = 2:N
% Random angle deviation (in radians)
dtheta = deg2rad(degVar + 10*rand);
theta = theta + dtheta; % Update direction
% Distance between centers = sum of radii (just touching)
d = radii(i-1) + radii(i);
% Compute new center position using updated direction
positions(i,1) = positions(i-1,1) + d * cos(theta);
positions(i,2) = positions(i-1,2) + d * sin(theta);
end
% Step 4: Plot the circles and their centers
figure; hold on; axis equal;
for i = 1:N
% Draw each circle
rectangle( ...
'Position', ...
[positions(i,1)-radii(i), positions(i,2)-radii(i),2*radii(i), 2*radii(i)], ...
'Curvature', [1 1], ...
'EdgeColor', 'b', ...
'LineWidth', 2);
% Mark each center
plot(positions(i,1), positions(i,2), 'k.', 'MarkerSize', 15);
end
title('Random Touching Circles with Small Angle Deviations');
xlabel('X'); ylabel('Y');
hold off;
Hope this gives you a good starting point. By adjusting the parameters you can tweak it to match your needs.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!