Equally spaced points in a circle
35 次查看(过去 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
0 个评论
采纳的回答
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)
%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 Center 和 File Exchange 中查找有关 Surface and Mesh Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!