【问题标题】:draw circle in matlab and generate random coordinate inside the circle and the negative of the coordinate在matlab中画圆并在圆内生成随机坐标和坐标的负值
【发布时间】:2014-03-21 17:24:05
【问题描述】:

我需要在matlab中绘制一个圆并标记它的中心并在圆内生成一个随机坐标和该坐标的负值并测量这两个点之间的距离 我试过这个

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

要使圆正常,但我不知道如何继续生成随机坐标及其负值并测量它们之间的距离

【问题讨论】:

  • “随机坐标及其负数”是什么意思?
  • 负数是指另一点必须位于圆心与点连线上的圆心的另一侧,但与圆心等距?

标签: matlab random geometry coordinate


【解决方案1】:

将此添加到您的代码中 -

%%// 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)

情节

【讨论】:

  • 非常感谢您的帮助
【解决方案2】:

您可以使用 rand() 函数生成与中心的随机距离,以及生成随机角度。然后,您可以转换为 x,y 坐标,并将它们取反以获得负坐标。最后,您可以使用距离公式计算两个坐标之间的距离。下面是一些示例代码:

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

r_max = sqrt(10); %set to radius of circle

r = rand(1)*r_max; %produces a random vector whose length is <=radius
theta = rand(1)*2*pi; % produces a random angle

x_coord = r*cos(theta); %calculate x coord
y_coord = r*sin(theta); % calculate y coord

x_coord_neg = -1*x_coord; % negate x coord
y_coord_neg = -1*y_coord; % negate y coord

plot(x_coord,y_coord, 'x')
plot(x_coord_neg,y_coord_neg, 'rx')

dist = sqrt((x_coord - x_coord_neg)^2 +  (y_coord - y_coord_neg)^2) % calculate distance

不确定您是否真的想要“负坐标”或坐标的复共轭。在后者的情况下,您只需否定 y 即可获得复共轭。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-07
    • 2017-02-13
    • 2016-01-08
    • 2013-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多