Info

この質問は閉じられています。 編集または回答するには再度開いてください。

The code below generates random points inside a regular hexagon. I need the second coordinate y of the points to be an angle from 0:2pi. How can I modify the code please?

1 回表示 (過去 30 日間)
Jenny
Jenny 2018 年 1 月 21 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
numEdges = 6;
R = 8;
xVertex = R * cos((0:6)*pi/3);
yVertex = R * sin((0:6)*pi/3);
xVertex = [xVertex , xVertex(1)];
yVertex = [yVertex , yVertex(1)];
requiredPoints = 20;
plot(xVertex, yVertex, 'b+-', 'LineWidth', 3);
grid on;
numPointsIn = 1;
while numPointsIn < requiredPoints
testx = 2 * R * rand(1) - R;
testy = 2 * R * rand(1) - R;
if inpolygon(testx, testy, xVertex, yVertex)
x(numPointsIn) = testx;
y(numPointsIn) = testy;
numPointsIn = numPointsIn + 1;
end
end
hold on;
plot(x,y,'r+', 'MarkerSize', 10, 'LineWidth', 2);

回答 (1 件)

Image Analyst
Image Analyst 2018 年 1 月 21 日
Something like
yAngle = atan2(y, x);
  7 件のコメント
Image Analyst
Image Analyst 2018 年 1 月 21 日
yAngle is in the range 0-2*pi, not -8 to 8. y is in that range, but not the angles.
To have the angle show up on the graph, use the sprintf() and text() functions.
str = sprintf('yAngle(1) = %f', yAngle(1));
text(1., .2, str);
Greg
Greg 2018 年 1 月 22 日
I think the desired output is
yAngle = atan2(y, x) + pi;
plot(x,yAngle,'r+', 'MarkerSize', 10, 'LineWidth', 2);

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by