Generate random circles with her vertices number
Show older comments
I need to generate a random number of circles with these properities (radius, center and number of vertices). the goal here is to replace these circles for example with hexagons or triangles ( if number of vertices = 3 eq => triangle, if numbre of vertices = 6 eq => hexagon).
If you have any idea how to do so, please help me. Thanks
2 Comments
James Tursa
on 2 Jan 2018
Can you show a small example of input(s) and desired output(s)?
sadedbouta
on 2 Jan 2018
Answers (2)
Image Analyst
on 2 Jan 2018
See my shape recognition program, attached. It places various shapes at random locations. Specifically this function in the demo:
%----------------------------------------------------------------------------------------------------------------------------------
% Create a single polygon with the specified number of sides in a binary image of the specified number of rows and columns.
% centroidToVertexDistance is the distance from the centroid to each vertex.
% If centroidToVertexDistance is a length 2 vector, then this indicated the minimum and maximum size range and
% it will create a random size polygon between the min and max distance.
function binaryImage = CreatePolygon(numSides, centroidToVertexDistance, rows, columns)
5 Comments
sadedbouta
on 4 Jan 2018
Image Analyst
on 4 Jan 2018
Not sure what you're saying said because it clearly DOES create polygon shapes randomly placed. Look, here is a screenshot from when I just ran it:

Surely you can see the shapes. Perhaps it threw an error for you. Copy and paste all the red text in that case.
sadedbouta
on 4 Jan 2018
Image Analyst
on 4 Jan 2018
Not sure what "No" means. The function does return a polygon with the specified size specified number of sides, and with random location and orientation. When you call
binaryImage = CreatePolygon(numSides, centroidToVertexDistance, rows, columns)
you specify the number of sides. If you want all hexagons, then just pass in 6 for the number of sides. You can also pass in the size. The output doesn't have to be a square but can be any rectangle you want. And yes, each polygon is located at a random location, like you wanted. Anyway, if you don't want it then don't use it.
Guillaume
on 4 Jan 2018
How about this? Requires R2017b:
%inputs
radius = 5;
vertices = 6;
polycount = 80;
canvassize = 100; %can also be 1x2 vector [width, height]
maxretry = 1000;
%centre generation, generate one at a time and check for overlap
centres = zeros(polycount, 2);
hitcount = 0;
polyindex = 1;
while polyindex <= polycount
xy = rand(1, 2) .* (canvassize - 2*radius) + radius;
if any(hypot(xy(1) - centres(1:polyindex-1, 1), xy(2) - centres(1:polyindex-1, 2)) <= 2*radius)
%high risk of intersection with previous polygon. Do not add
hitcount = hitcount + 1;
if hitcount >= maxretry
%too many consecutive attempts have resulted in collisions. Abort
centres(polyindex:end, :) = [];
warning('Aborted generation due to too may collisions');
break;
end
else
hitcount = 0;
centres(polyindex, :) = xy;
polyindex = polyindex + 1;
end
end
%convert to polyshape
polys = cellfun(@(xy) nsidedpoly(vertices, 'Center', xy, 'Radius', radius), num2cell(centres, 2));
%plot
plot(polys);
xlim([0 canvassize(1)]);
ylim([0 canvassize(min(numel(canvassize), 2))]);
19 Comments
sadedbouta
on 4 Jan 2018
sadedbouta
on 6 Jan 2018
Image Analyst
on 6 Jan 2018
Again the answer is in my m-file I attached. You have to make a rotation matrix to multiply the boundary coordinates by. Here's a snippet:
% Rotate the coordinates by a random angle between 0 and 360
angleToRotate = 360 * rand(1);
rotationMatrix = [cosd(angleToRotate), sind(angleToRotate); -sind(angleToRotate), cosd(angleToRotate)];
% Do the actual rotation
xy = [x', y']; % Make a numSides*2 matrix;
xyRotated = xy * rotationMatrix; % A numSides*2 matrix times a 2*2 = a numSides*2 matrix.
x = xyRotated(:, 1); % Extract out the x as a numSides*2 matrix.
etc. See my demo if you want to see it in action.
sadedbouta
on 6 Jan 2018
sadedbouta
on 6 Jan 2018
Image Analyst
on 6 Jan 2018
Ok, looks like what you want, I think.
Guillaume
on 7 Jan 2018
It is trivial to add random rotations to my script. Just use the rotate method of the polygon objects that are created.
So after the line that creates the polygons:
%convert to polyshape
polys = cellfun(@(xy) nsidedpoly(vertices, 'Center', xy, 'Radius', radius), num2cell(centres, 2));
simply add:
polysrotated = cellfun(@rotate, num2cell(polys), num2cell(rand(size(polys))*360), num2cell(centres, 2))
and plot as before
%plot
plot(polysrotated);
xlim([0 canvassize(1)]);
ylim([0 canvassize(min(numel(canvassize), 2))]);
sadedbouta
on 7 Jan 2018
Image Analyst
on 7 Jan 2018
Note, Guillaume's solution put the shapes into a graph, like objects in an overlay. Mine creates a digital image (a 2-D array with shapes "burned in" to the array). Either wau is fine, you just have to use the method that produces the output that you want, and I wanted to be sure you knew of the difference.
sadedbouta
on 25 Feb 2018
Image Analyst
on 25 Feb 2018
It doesn't show up when I do
>> imformats
so, I don't know how to do it.
Guillaume
on 25 Feb 2018
dxf is a vector format not an image format so it's certainly not going to be supported by imwrite and imformats.
Thankfully my code does generate vector graphics as a figure (while Image Analyst's generates images) so the output could theoretically be converted to dxf. However, there' no built-in dxf writers in matlab. You could try this FileExchance submission. No idea what it's worth.
sadedbouta
on 3 Mar 2018
sadedbouta
on 3 Mar 2018
Guillaume
on 5 Mar 2018
I know nothing about DXFLib, you'll have to work out how to use it on your own.
Why do you want to output it as DXF which is a CAD format. Why not use more standard vector formats such as SVG, which matlab supports out of the box. It is very likely that the software you want to use with your DXF also supports SVG
saveas(gcf, 'somename.svg')
sadedbouta
on 8 Mar 2018
Walter Roberson
on 8 Mar 2018
"Scalable Vector Graphics (SVG) is an XML-based vector image format for two-dimensional graphics with support for interactivity and animation."
Guillaume
on 9 Mar 2018
Guillaume
on 9 Mar 2018
It looks like you're trying to use matlab to generate a mesh (?) for some other program. In my opinion, it would have made more sense to first see if there was an easy way of transfering the mesh from matlab to your other program before even trying to generate that mesh.
it's a completely different topic from your original question, though. To have more chance of getting help, I suggest you start a new question specifically about transfering a figure to whichever program you're using. Perhaps somebody else has already solved that problem. It's completely outside of my domain, so not something I can help with.
And if one of the answers posted here has resolved your initial question, then you should also accept it.
Categories
Find more on Graphics Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

