Equally spaced points in a circle

45 次查看(过去 30 天)
Hi everyone! I am trying to create a function that gets as inputs coordinates of two points with the first point being the center and the distance between the two points defining the radius of the circle. And get as an output the coordinates of equally spaces points along this circle. This is my function but it isn't giving me the correct result
function points = get_equally_spaced_points(x1, y1, x2, y2) % Calculate the distance between the two points
distance = sqrt((x2 - x1)^2 + (y2 - y1)^2);
% Calculate the angle between the x-axis and the line connecting the two points
angle = atan2(y2 - y1, x2 - x1);
% Calculate the angular separation between the points
angular_separation = 2 * pi / 3;
% Calculate the coordinates of the additional points
points = zeros(4, 2);
points(1, :) = [x1, y1];
points(2, :) = [x2, y2];
for i = 1:2
current_angle = angle + (i - 1) * angular_separation;
x = x1 + distance * cos(current_angle); y = y1 + distance * sin(current_angle);
points(i + 2, :) = [x, y];
end
end

采纳的回答

Dyuman Joshi
Dyuman Joshi 2023-6-9
编辑:Dyuman Joshi 2023-6-9
Change the current_angle formula and use i instead of (i-1)
%Random points with (x1,y1) - center point
%and (x2,y2) - point on circle
x1 = rand;
y1 = rand;
x2 = rand;
y2 = rand;
%points
points = get_equally_spaced_points(x1,y1,x2,y2)
points = 4×2
0.2866 0.7691 0.7344 0.7210 0.1043 1.1810 0.0210 0.4053
%plot the points obtained
scatter(points(:,1),points(:,2))
hold on
%plot the corresponding circle
fimplicit(@(x,y) (x-x1).^2+(y-y1).^2-hypot(x1-x2,y1-y2)^2)
function points = get_equally_spaced_points(x1, y1, x2, y2)
% Calculate the distance between the two points
distance = sqrt((x2 - x1)^2 + (y2 - y1)^2);
% Calculate the angle between the x-axis and the line connecting the two points
angle = atan2(y2 - y1, x2 - x1);
% Calculate the angular separation between the points
angular_separation = 2 * pi / 3;
% Calculate the coordinates of the additional points
points = zeros(4, 2);
points(1, :) = [x1, y1];
points(2, :) = [x2, y2];
for i = 1:2
current_angle = angle + i * angular_separation;
%Modified definition ^
x = x1 + distance * cos(current_angle);
y = y1 + distance * sin(current_angle);
points(i + 2, :) = [x, y];
end
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Surface and Mesh Plots 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by