How can i make a bunch of hexagons with different radii ?

2 次查看(过去 30 天)
Hello ! I need quick help. i have created a bunch of hexagons within an area. all hexagons have same radii but i want to make a bunch with different radii of each hexagon. the attached figure here is a way i created my plot.
this way i have done
j = 0;
i = 1;
Centre_x(i) = 100;
Centre_y(i) = 100;
Radius = 250;
R=1000; % radius of macrocell in meters
v_x = 100+R * cos((0:6)*pi/3); %Vertexes
v_y = 100+R * sin((0:6)*pi/3);
v_x_b = v_x;
v_y_b = v_y;
while Centre_x(i) < R+100
while Centre_y(i) < ((((3)^0.5)*R)/2)+100
i = i + 1;
Centre_x(i) = Centre_x(i-1);
Centre_y(i) = Centre_y(i-1) + (((3)^0.5)*Radius);
end
j = j + 1;
if mod(j,2) == 0
i = i + 1;
Centre_x(i) = Centre_x(i-1) + 3*Radius/2;
Centre_y(i) = 100;
else
i = i + 1;
Centre_x(i) = Centre_x(i-1) + 3*Radius/2;
Centre_y(i) = ((((3)^0.5)*Radius)/2)+100;
end
end
for i = 1 : length(Centre_x)
Tempx(i,1) = Centre_x(i)- Radius;
Tempx(i,2) = Centre_x(i) - Radius/2;
Tempx(i,3) = Centre_x(i) + Radius/2;
Tempx(i,4) = Centre_x(i) + Radius;
Tempx(i,5) = Centre_x(i) + Radius/2;
Tempx(i,6) = Centre_x(i) - Radius/2;
Tempx(i,7) = Centre_x(i) - Radius;
Tempy(i,1) = Centre_y(i);
Tempy(i,2) = Centre_y(i) - ((((3)^0.5)*Radius)/2);
Tempy(i,3) = Centre_y(i) - ((((3)^0.5)*Radius)/2);
Tempy(i,4) = Centre_y(i);
Tempy(i,5) = Centre_y(i) + ((((3)^0.5)*Radius)/2);
Tempy(i,6) = Centre_y(i) + ((((3)^0.5)*Radius)/2);
Tempy(i,7) = Centre_y(i);
end
LL1 = length(Tempx);
j = 0;
for i = 1 : LL1
in = inpolygon(Tempx(i,1),Tempy(i,2),v_x_b,v_y_b);
if in == 1
j = j +1;
V_X_New(j,:) = Tempx(i,:);
V_Y_New(j,:) = Tempy(i,:);
end
end
j = 1;
i = length(Centre_x) + 1;
Centre_x(i) = 100 - 3*Radius/2;
Centre_y(i) = ((((3)^0.5)*Radius)/2)+100;
while Centre_x(i) > -R+100
while Centre_y(i) < (((((3)^0.5)*R)/2))+100
i = i + 1;
Centre_x(i) = Centre_x(i-1);
Centre_y(i) = Centre_y(i-1) + (((3)^0.5)*Radius);
end
j = j + 1;
if mod(j,2) == 0
i = i + 1;
Centre_x(i) = Centre_x(i-1) - 3*Radius/2;
Centre_y(i) = 100;
else
i = i + 1;
Centre_x(i) = Centre_x(i-1) - 3*Radius/2;
Centre_y(i) = ((((3)^0.5)*Radius)/2)+100;
end
end
for i = LL1+1 : length(Centre_x)
Tempx(i,1) = Centre_x(i)- Radius;
Tempx(i,2) = Centre_x(i) - Radius/2;
Tempx(i,3) = Centre_x(i) + Radius/2;
Tempx(i,4) = Centre_x(i) + Radius;
Tempx(i,5) = Centre_x(i) + Radius/2;
Tempx(i,6) = Centre_x(i) - Radius/2;
Tempx(i,7) = Centre_x(i) - Radius;
Tempy(i,1) = Centre_y(i);
Tempy(i,2) = Centre_y(i) - ((((3)^0.5)*Radius)/2);
Tempy(i,3) = Centre_y(i) - ((((3)^0.5)*Radius)/2);
Tempy(i,4) = Centre_y(i);
Tempy(i,5) = Centre_y(i) + ((((3)^0.5)*Radius)/2);
Tempy(i,6) = Centre_y(i) + ((((3)^0.5)*Radius)/2);
Tempy(i,7) = Centre_y(i);
end
LL2 = length(Tempx);
j = length(V_X_New);
for i = LL1+1 : LL2
in = inpolygon(Tempx(i,4),Tempy(i,3),v_x_b,v_y_b);
if in == 1
j = j +1;
V_X_New(j,:) = Tempx(i,:);
V_Y_New(j,:) = Tempy(i,:);
end
end
j = 0;
i = length(Centre_x) + 1;
Centre_x(i) = 100;
Centre_y(i) = -((((3)^0.5)*Radius))+100;
while Centre_x(i) > -R+100
while Centre_y(i) > -((((3)^0.5)*R)/2)+100
i = i + 1;
Centre_x(i) = Centre_x(i-1);
Centre_y(i) = Centre_y(i-1) - (((3)^0.5)*Radius);
end
j = j + 1;
if mod(j,2) == 0
i = i + 1;
Centre_x(i) = Centre_x(i-1) - 3*Radius/2;
Centre_y(i) = -((((3)^0.5)*Radius))+100;
else
i = i + 1;
Centre_x(i) = Centre_x(i-1) - 3*Radius/2;
Centre_y(i) = -((((3)^0.5)*Radius)/2)+100;
end
end
for i = LL2+1 : length(Centre_x)
Tempx(i,1) = Centre_x(i)- Radius;
Tempx(i,2) = Centre_x(i) - Radius/2;
Tempx(i,3) = Centre_x(i) + Radius/2;
Tempx(i,4) = Centre_x(i) + Radius;
Tempx(i,5) = Centre_x(i) + Radius/2;
Tempx(i,6) = Centre_x(i) - Radius/2;
Tempx(i,7) = Centre_x(i) - Radius;
Tempy(i,1) = Centre_y(i);
Tempy(i,2) = Centre_y(i) - ((((3)^0.5)*Radius)/2);
Tempy(i,3) = Centre_y(i) - ((((3)^0.5)*Radius)/2);
Tempy(i,4) = Centre_y(i);
Tempy(i,5) = Centre_y(i) + ((((3)^0.5)*Radius)/2);
Tempy(i,6) = Centre_y(i) + ((((3)^0.5)*Radius)/2);
Tempy(i,7) = Centre_y(i);
end
LL3 = length(Tempx);
j = length(V_X_New);
for i = LL2+1 : LL3
in = inpolygon(Tempx(i,4),Tempy(i,5),v_x_b,v_y_b);
if in == 1
j = j +1;
V_X_New(j,:) = Tempx(i,:);
V_Y_New(j,:) = Tempy(i,:);
end
end
j = 1;
i = length(Centre_x) + 1;
Centre_x(i) = 100 + 3*Radius/2;
Centre_y(i) = -((((3)^0.5)*Radius)/2)+100;
while Centre_x(i) < R+100
while Centre_y(i) > -((((3)^0.5)*R)/2)+100
i = i + 1;
Centre_x(i) = Centre_x(i-1);
Centre_y(i) = Centre_y(i-1) - (((3)^0.5)*Radius);
end
j = j + 1;
if mod(j,2) == 0
i = i + 1;
Centre_x(i) = Centre_x(i-1) + 3*Radius/2;
Centre_y(i) = -((((3)^0.5)*Radius))+100;
else
i = i + 1;
Centre_x(i) = Centre_x(i-1) + 3*Radius/2;
Centre_y(i) = -((((3)^0.5)*Radius)/2)+100;
end
end
for i = LL3+1 : length(Centre_x)
Tempx(i,1) = Centre_x(i)- Radius;
Tempx(i,2) = Centre_x(i) - Radius/2;
Tempx(i,3) = Centre_x(i) + Radius/2;
Tempx(i,4) = Centre_x(i) + Radius;
Tempx(i,5) = Centre_x(i) + Radius/2;
Tempx(i,6) = Centre_x(i) - Radius/2;
Tempx(i,7) = Centre_x(i) - Radius;
Tempy(i,1) = Centre_y(i);
Tempy(i,2) = Centre_y(i) - ((((3)^0.5)*Radius)/2);
Tempy(i,3) = Centre_y(i) - ((((3)^0.5)*Radius)/2);
Tempy(i,4) = Centre_y(i);
Tempy(i,5) = Centre_y(i) + ((((3)^0.5)*Radius)/2);
Tempy(i,6) = Centre_y(i) + ((((3)^0.5)*Radius)/2);
Tempy(i,7) = Centre_y(i);
end
LL4 = length(Tempx);
j = length(V_X_New);
for i = LL3+1 : LL4
in = inpolygon(Tempx(i,1),Tempy(i,6),v_x_b,v_y_b);
if in == 1
j = j +1;
V_X_New(j,:) = Tempx(i,:);
V_Y_New(j,:) = Tempy(i,:);
end
end
for i = 1 : length(V_X_New)
plot(V_X_New(i,:),V_Y_New(i,:),'c--');
x = V_X_New(i,6);
y = V_Y_New(i,6);
text1=[num2str(i)]; dx = 0; dy = 90; text(x+dx,y-dy,text1);
end

回答(1 个)

Image Analyst
Image Analyst 2017-6-22
Can't you just make R and/or Radius a random number within some range?

Community Treasure Hunt

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

Start Hunting!

Translated by