
Circle plot and random point on circle
조회 수: 45 (최근 30일)
이전 댓글 표시
Hi,
I drawn a circle on matlab with parametric equations. I give the xcenter,ycenter,r(x1,y1,r1) and the s.S is the number of points which equidistant(have the same distance on circle). I drawn some points on circle and I want to draw 4 random points around. My code is:
t1 = 0:(pi/(5*s1)):2*pi; % s1 is the number of points I want
x1unit = r1 * cos(t1) + x1;
y1unit = r1 * sin(t1) + y1;
a1=x1unit(1:10:s1*10); %points around on circle
b1=y1unit(1:10:s1*10);
plot(x1unit, y1unit, 'b', x1, y1, 'ok');%circle
plot(a1, b1, 'o');
I want random points on circle 3 ,4,or 5 or more on circle.But I couldn't to success it , yes. Could you help me?
Thanks in andvance,
댓글 수: 0
채택된 답변
Image Analyst
2018년 9월 16일
편집: Image Analyst
2018년 9월 16일
Try this:
% Plot a circle.
angles = linspace(0, 2*pi, 720); % 720 is the total number of points
radius = 20;
xCenter = 50;
yCenter = 40;
x = radius * cos(angles) + xCenter;
y = radius * sin(angles) + yCenter;
% Plot circle.
plot(x, y, 'b-', 'LineWidth', 2);
% Plot center.
hold on;
plot(xCenter, yCenter, 'k+', 'LineWidth', 2, 'MarkerSize', 16);
grid on;
axis equal;
xlabel('X', 'FontSize', 16);
ylabel('Y', 'FontSize', 16);
% Now get random locations along the circle.
s1 = 5; % Number of random points to get.
randomIndexes = randperm(length(angles), s1)
xRandom = x(randomIndexes);
yRandom = y(randomIndexes);
plot(xRandom, yRandom, 'ro', 'LineWidth', 2, 'MarkerSize', 16);

댓글 수: 5
mehrdad tanomand
2021년 9월 3일
How can I do this for multiple circles?
I want to get a random point on the perimeter of each circle
thank you
Image Analyst
2021년 9월 3일
It's the same process. Wrap it all in a function if you want where you pass it the center and the radius and return the randomly located point.
% Demo by Image Analyst
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
for k = 1 : 5
xCenter = 10 * k;
yCenter = 10 * k;
radius = k;
numPoints = 4;
[xRandom, xRandom] = GetRandomPointsOnACircle(xCenter, yCenter, radius, numPoints)
end
%-----------------------------------------------------------------------------------------------
function [xRandom, yRandom] = GetRandomPointsOnACircle(xCenter, yCenter, radius, numPoints)
angles = linspace(0, 2*pi, 720); % 720 is the total number of points
x = radius * cos(angles) + xCenter;
y = radius * sin(angles) + yCenter;
% Now get random locations along the circle.
randomIndexes = randperm(length(angles), numPoints);
xRandom = x(randomIndexes);
yRandom = y(randomIndexes);
% Optional plotting of the circle and points..
showPlot = true;
% Plot a circle.
if showPlot
% Plot circle perimeter.
plot(x, y, 'b-', 'LineWidth', 2);
% Plot center.
hold on;
plot(xCenter, yCenter, 'k+', 'LineWidth', 2, 'MarkerSize', 16);
grid on;
axis equal;
xlabel('X', 'FontSize', 16);
ylabel('Y', 'FontSize', 16);
% Plot the random points.
plot(xRandom, yRandom, 'ro', 'LineWidth', 2, 'MarkerSize', 6);
end
end % of function GetRandomPointsOnACircle()

추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Scatter Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!