0

我正在尝试从球体的特定半径开始绘制随机线,但我只想要上半球,如图所示

到目前为止,我能够创建随机起点(但对于 R=15)、随机交叉点、随机斜率,但我不知道如何连接所有这些以绘制线条。

我的代码是

%Create the random starting points, slopes, intersections
tracks=input('Give me the number of muon tracks: ');
theta=180.*rand(tracks,1);
rho=15*ones(tracks,1);
startPoint = [theta rho];
[X,Y]=pol2cart(theta*pi/180,rho);
intersection =-6371+(2*6371).*rand(tracks,1);
slope = tand(360.*rand(tracks,1));

我知道我只需要两个元素来画一条线,但我现在有点困惑......关于如何做到这一点的任何想法?

4

1 回答 1

1

因为您不希望 MATLAB 在绘制它们时将所有线连接起来,所以您需要在一个循环中单独绘制它们,例如,类似

theta = 2 * pi * rand(tracks, 2); % 2 rows of random points on a circle, in radians
X = cos(theta); Y = sin(theta);
close all;
figure;
hold on;
for nPlot = 1:tracks
    plot(X(nPlot, :), Y(nPlot, :), 'r-o');
end

请注意,此代码生成的 X 和 Y 也与您的原始代码不同 -pol2cart并且上述方法都期望以弧度而不是度数为单位的值。

于 2013-04-08T10:17:12.667 回答