Part 1
Determine the mean distance between two random points on the perimeter of a unit (convex) regular hexagon.
Part 2
Determine the mean distance between two random points on the interior of a unit (convex) regular hexagon.
Part 1:
dist=0; xvals=[]; yvals=[];
longleg=cos(pi/6);
for trial=1:120000
dista=rand*6;
sect=floor(dista);
tanangle=(dista-sect-1/2)/longleg;
theta=sect*pi/3+pi/6+atan(tanangle);
rho=longleg/cos(atan(tanangle));
[x y]=pol2cart(theta,rho);
pt1=[x y];xvals(end+1)=x; yvals(end+1)=y;
dista=rand*6;
sect=floor(dista);
tanangle=(dista-sect-1/2)/longleg;
theta=sect*pi/3+pi/6+atan(tanangle);
rho=longleg/cos(atan(tanangle));
[x y]=pol2cart(theta,rho);
pt2=[x y];xvals(end+1)=x; yvals(end+1)=y;
dist=dist+norm(pt2-pt1);
end
disp(dist/trial)
plot(xvals,yvals,'.k'); grid on;
finds 1.163...
This is less than the disstance between opposite sides but larger than the unit length of one side.
The plot at the end was to assure that the hexagon was being filled in properly and randomly.
Part 2:
clearvars
xvals=[]; yvals=[];
dist=0; ct=0;
slope=sqrt(3);
for trial=1:3000000
good=false;
x=2*rand-1;
y=slope*rand-slope/2;
if abs(y)<abs((x+1)*slope)
if abs(y)<abs(slope/2-(x-.5)*slope)
xvals(end+1)=x; yvals(end+1)=y;
good=true;
end
end
pt1=[x,y];
if good
good=false;
x=2*rand-1;
y=slope*rand-slope/2;
if abs(y)<abs((x+1)*slope)
if abs(y)<abs(slope/2-(x-.5)*slope)
xvals(end+1)=x; yvals(end+1)=y;
good=true;
end
end
pt2=[x,y];
end
if good
dist=dist+norm(pt2-pt1);
ct=ct+1;
end
end
dist/ct
plot(xvals,yvals,'.k'); grid on; axis equal;
finds the average distance as around 0.826..., slightly under the length of one side of the hexagon.
The plot at the end, again, was to assure the proper hexagonal shape and the randomness of the points, i.e., that any region of a given area was just as likely to recieve a given point as any other region with that area. That randomness arose from making x and y coordinates independently random and not using any (x,y) pair not found within the hexagon.
Corrected bug in second program, changing the answer.
Edited on October 14, 2022, 10:37 am
|
Posted by Charlie
on 2022-10-14 08:28:24 |