how to make circle in matlab and generate random points inside it

后端 未结 3 911
隐瞒了意图╮
隐瞒了意图╮ 2021-01-12 18:52

\"enterhello i want to ask a question how to make a circle in matlab and mark its center and g

相关标签:
3条回答
  • 2021-01-12 19:39

    Think this is what you need -

    %%// Plot the circle
    x = linspace(-sqrt(10),sqrt(10));
    y1 = sqrt(10-x.^2);
    y2 = -sqrt(10-x.^2);
    plot(x,y1,x,y2)
    axis equal
    
    %%// Choose from 1000 random point pairs
    N = 1000; 
    %%// Radius of circle
    radius = sqrt(10); 
    
    %%// Create a random point matrix Nx2
    points_mat = [ radius*2*(rand(N,1)-0.5) radius*2*(rand(N,1)-0.5)];
    
    %%// Select the first 50 pairs that lies inside circle
    ind1 = find(sqrt( points_mat(:,1).^2 + points_mat(:,2).^2 )<radius);
    points_mat=points_mat(ind1(1:50),:);
    
    %%// Plot the 50 points on the circle
    hold on
    text(0,0,'x Center') %%// Center
    text(points_mat(:,1),points_mat(:,2),'o') %%// 50 points
    

    Plot

    enter image description here

    0 讨论(0)
  • 2021-01-12 19:47

    Here's another option:

    %// Set parameters
    R = 0.5;   %// radius
    C = [3 4]; %// center [x y]
    N = 50;    %// number of points inside circle
    
    %// generate circle boundary
    t = linspace(0, 2*pi, 100);
    x = R*cos(t) + C(1);
    y = R*sin(t) + C(2);
    
    %// generate random points inside it
    th = 2*pi*rand(N,1);
    r  = R*rand(N,1);
    
    xR = r.*cos(th) + C(1);
    yR = r.*sin(th) + C(2);
    
    %// Plot everything
    figure(1), clf, hold on
    plot(x,y,'b')
    plot(C(1),C(2),'r.', 'MarkerSize', 100)
    plot(xR,yR,'k.')
    axis equal
    

    enter image description here

    Here's why that could be useful:

    %// Set parameters
    R = 0.5;     N = 50;
    C = [3 4];   M = 100;  %// points on boundary
    
    %// generate all points at once
    t  = linspace(0, 2*pi, M)';
    th = 2*pi*rand(N,1);
    r  = R*rand(N,1);
    xR = [R*ones(M,1); r] .* cos([t; th]) + C(1);
    yR = [R*ones(M,1); r] .* sin([t; th]) + C(2);
    
    %// Plot everything
    figure(1), clf, hold on
    plot(xR(1:M),yR(1:M),'b')                %// circle boundary
    plot(C(1),C(2),'r.', 'MarkerSize', 100)  %// center
    plot(xR(M+1:end),yR(M+1:end),'k.')       %// random points
    axis equal
    
    0 讨论(0)
  • 2021-01-12 19:53

    I don't know matlab, so I can't help you there, but if you want to do this without rejection you can generate the points in polar coordinates. If rand() returns a Uniform(0,1) random number, then:

    r = radius * sqrt(rand())
    theta = 2 * Pi * rand()
    x = r * cos(theta)
    y = r * sin(theta)
    

    will yield values which are uniformly distributed within a circle of radius radius. Note the square root on the calculation of r, which adjusts the distribution of distance from the center of the circle so that the number of points at a given distance is always proportional to the area and hence is uniform. For spherical uniformity you'd take the cube root to keep proportionality to the volume, and in general the kth root for a k-dimensional hypersphere.

    0 讨论(0)
提交回复
热议问题