Equally spaced points in a circle

조회 수: 39 (최근 30일)
george korris
george korris 2023년 6월 9일
댓글: george korris 2023년 6월 9일
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