Moving a star marker along a hexagon trajectory?
1 回表示 (過去 30 日間)
古いコメントを表示
Hello I have tried to move a star marker along the circumference of a hexagon trajectory which consists of four concentric hexagons but I couldn't write a code that works. Thanks in advance.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
angles = linspace(0, 360, 7);
radii = [20, 35, 50];
% First create and draw the hexagons.
numberOfHexagons = 4;
% Define x and y arrays. Each row is one hexagon.
% Columns are the vertices.
x1=radii(1) * cosd(angles)+50;
y1 = radii(1) * sind(angles)+50;
x2=radii(2) * cosd(angles)+50;
y2 = radii(2) * sind(angles)+50;
x3=radii(3) * cosd(angles)+50;
y3 = radii(3) * sind(angles)+50;
plot(x1 , y1, 'b');
hold on
plot(x2, y2, 'b');
hold on
plot(x3, y3, 'b');
hold on
% Connecting Line:
plot([70 100], [50 50],'color','b')
axis([0 100 0 100])
hold on
2 件のコメント
Jan
2017 年 7 月 7 日
What exactly is a "star marker" and what is "a hexagon trajectory which consists of four concentric hexagons"? It is not clear to me what you want toachieve.
採用された回答
Geoff Hayes
2017 年 7 月 7 日
Zeinab - if you want to traverse the outer hexagon, then you can do something like
hStarMarker = plot(NaN, NaN, 'r*');
for k=1:length(x3)-1
xCoords = linspace(x3(k), x3(k+1), 50);
yCoords = linspace(y3(k), y3(k+1), 50);
for m=1:length(xCoords)
set(hStarMarker, 'XData', xCoords(m), 'YData', yCoords(m));
pause(0.01);
end
end
We create a graphics object, hStarMarker, which will represent the red star that will traverse the hexagon. We then loop over each hexagon edge and create 50 points along that edge using linspace. We can then use the x- and y-coordinate for each point along the edge and update the position of the star marker. We pause for 100th of a second before drawing the star at its next position.
0 件のコメント
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!