Generate random circles with her vertices number

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

Can you show a small example of input(s) and desired output(s)?

Sign in to comment.

Answers (2)

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

This script is used to generate graphs and curves bt not forme chape is there any script to generate random shape objects (triangular or hexagon) in a 2D, as the figure above.
Since R2017b, there is the polyshape class.
edit: and actually, the nsidedpoly function.
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.
No, Each polygon shape is randomly distributed in the square
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.

Sign in to comment.

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

I will try this script and inform you of the results
It work, thank's, 2nd question, How can I make an randomly rotation of these particles
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.
I tried your code but I have not yet understood the curves and other things, an example of results triangle case:
Ok, looks like what you want, I think.
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))]);
Good Guillaume, that is what I am looking
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.
Sorry for retard repling, its possible to convert this image to dxf?
It doesn't show up when I do
>> imformats
so, I don't know how to do it.
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.
For me, I want to calculate the thermal properties of these cells, when I use the comsol, I find only the case of dxf file, so if there is a help to convert these images to dxf, in addition the dxf is a format of a vector, how can this file be obtained if these script does not work.
I did not understand how to add this script DXFLib and could solve the problem !!!
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')
I think this format (SVG) does not exist for the 2D case
"Scalable Vector Graphics (SVG) is an XML-based vector image format for two-dimensional graphics with support for interactivity and animation."
Said boutani's comment mistakenly posted as an Answer copied here
In 2D, the formats exist are:
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.

Sign in to comment.

Categories

Find more on Graphics Performance in Help Center and File Exchange

Asked:

on 2 Jan 2018

Commented:

on 9 Mar 2018

Community Treasure Hunt

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

Start Hunting!