Equally spaced points in a circle
39 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
george korris
il 9 Giu 2023
Commentato: george korris
il 9 Giu 2023
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 Commenti
Risposta accettata
Dyuman Joshi
il 9 Giu 2023
Modificato: Dyuman Joshi
il 9 Giu 2023
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
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Surface and Mesh Plots in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!