Search code examples
matlabrandomgeometrycoordinate

draw circle in matlab and generate random coordinate inside the circle and the negative of the coordinate


I am required to plot a circle in matlab and mark its center and generate a random coordinate inside the circle and the negative of this coordinate and measure the distance between these two points I tried this

x = linspace(-sqrt(10),sqrt(10));
y1 = sqrt(10-x.^2);
y2 = -sqrt(10-x.^2);
plot(x,y1,x,y2)
axis equal

to make the circle and its ok but i don't know how to proceed to generate the random coordinate ad its negative and measure the distance between them


Solution

  • Add this to your code -

    %%// Choose from 100 random point pairs
    N = 100; 
    %%// Radius of circle
    radius = sqrt(10); 
    
    %%// Create a random point matrix Nx2
    pt1 = [ radius*2*(rand(N,1)-0.5) radius*2*(rand(N,1)-0.5)];
    
    %%// Select the first pair that lies inside circle
    pt1 = pt1(find(sqrt( pt1(:,1).^2 + pt1(:,2).^2 )<radius,1),:);
    
    %%// Negative of the point, i.e. on the other side of the center of the circle but equidistant from the center
    pt2 = -pt1;
    
    %%// Distance between the two points
    dist1 = sqrt((pt1(1)-pt2(1)).^2 + (pt1(2)-pt2(2)).^2); 
    
    %%// Overlay the center and the two points on the circle plot
    hold on
    text(0,0,'C') %%// Center
    text(pt1(1),pt1(2),'P') %%// First point
    text(pt2(1),pt2(2),'MP') %%// Second point (Mirror Point, MP)
    

    Plot

    enter image description here