フィルターのクリア

Moving a star marker along a hexagon trajectory?

5 ビュー (過去 30 日間)
Zeinab Ahmadi93
Zeinab Ahmadi93 2017 年 7 月 7 日
回答済み: Geoff Hayes 2017 年 7 月 7 日
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
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.
Zeinab Ahmadi93
Zeinab Ahmadi93 2017 年 7 月 7 日
編集済み: Zeinab Ahmadi93 2017 年 7 月 7 日
Thanks for your comment... Actually I want to simulate a wireless sensor network and the star marker is a sensor node which is equipped with GPS, this node traverses a predefined path(here the path is look like a hexagon) and broadcast its coordinate periodically, and other unknown nodes calculate their coordinates with the help of that node(using some geometric rules). I had written a code for circle trajectory in which a red star marker traverses concentric circles and now i want to write code that creates the similar situation for hexagon trajectory but I have problem with writing its code: This is my code for circle trajectory:
% Initialization steps.
format long g;
format compact;
fontSize = 20;
r1 = 50;
r2 = 35;
r3= 20;
xc = 50;
yc = 50;
% Since arclength = radius * (angle in radians),
% (angle in radians) = arclength / radius = 5 / radius.
deltaAngle1 = 5 / r1;
deltaAngle2 = 5 / r2;
deltaAngle3 = 5 / r3;
theta1 = 0 : deltaAngle1 : (2 * pi);
theta2 = 0 : deltaAngle2 : (2 * pi);
theta3 = 0 : deltaAngle3 : (2 * pi);
x1 = r1*cos(theta1) + xc;
y1 = r1*sin(theta1) + yc;
x2 = r2*cos(theta2) + xc;
y2 = r2*sin(theta2) + yc;
x3 = r3*cos(theta3) + xc;
y3 = r3*sin(theta3) + yc;
plot(x1,y1,'color',[1 0.5 0])
hold on
plot(x2,y2,'color',[1 0.5 0])
hold on
plot(x3,y3,'color',[1 0.5 0])
hold on
% Connecting Line:
plot([70 100], [50 50],'color',[1 0.5 0])
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0, 1, 1]);
drawnow;
axis square;
for i = 1 : length(theta1)
plot(x1(i),y1(i),'r*')
pause(0.1)
end
for i = 1 : length(theta2)
plot(x2(i),y2(i),'r*')
pause(0.1)
end
for i = 1 : length(theta3)
plot(x3(i),y3(i),'r*')
pause(0.1)
end

サインインしてコメントする。

採用された回答

Geoff Hayes
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 件)

カテゴリ

Help Center および File ExchangeAssumptions についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by