MATLAB: I am working code to create the hexagon with a circle in it and to create the random points in hexagon and circle separately.

random circle

i am working on a code to create the hexagon with a circle in it and to create the random points in hexagon and circle separately. please help me how can i create random points in hexagon and circle separately.and they should remain in their boundaries. below is the graph of my code uptil now.

Best Answer

No graph has yet appeared.
Here is how I would do it:
hex_ang = linspace(0, 2*pi, 7); % Define Hexagon
crc_ang = linspace(0, 2*pi); % Define Circle
hex_r = 1.0; % Hexagon Radius
crc_r = 0.7; % Circle Radius
hex_xy = [hex_r*cos(hex_ang); hex_r*sin(hex_ang)]; % Hexagon Coordinates
crc_xy = [crc_r*cos(crc_ang); crc_r*sin(crc_ang)]; % Circle Coordinates
all_pts = 1-2*rand(2, 500); % Create Points
crc_ix = inpolygon(all_pts(1,:), all_pts(2,:), crc_xy(1,:), crc_xy(2,:)); % Points In Circle
hex_ix = inpolygon(all_pts(1,:), all_pts(2,:), hex_xy(1,:), hex_xy(2,:)); % Points In Hexagon
hex_in = setdiff(find(hex_ix), find(crc_ix)); % Indices Of Points In Hexagon But Not In Circle
crc_in = crc_ix; % Indices Of Points In Circle
crc_in = [all_pts(1,crc_in); all_pts(2,crc_in)]; % Points In Circle Only
hex_in = [all_pts(1,hex_in); all_pts(2,hex_in)]; % Points In Hexagon Only
figure(1)
plot(hex_xy(1,:), hex_xy(2,:))
hold on
plot(crc_xy(1,:), crc_xy(2,:))
plot(crc_in(1,:), crc_in(2,:), 'cp')
plot(hex_in(1,:), hex_in(2,:), 'mp')
hold off
axis([-1.0 1.0 -1.0 1.0])
axis equal
Related Question