Generate random circles with her vertices number

22 visualizzazioni (ultimi 30 giorni)
sadedbouta
sadedbouta il 2 Gen 2018
Commentato: Guillaume il 9 Mar 2018
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 Commenti
James Tursa
James Tursa il 2 Gen 2018
Can you show a small example of input(s) and desired output(s)?

Accedi per commentare.

Risposte (2)

Image Analyst
Image Analyst il 2 Gen 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 Commenti
sadedbouta
sadedbouta il 4 Gen 2018
No, Each polygon shape is randomly distributed in the square
Image Analyst
Image Analyst il 4 Gen 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.

Accedi per commentare.


Guillaume
Guillaume il 4 Gen 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 Commenti
Guillaume
Guillaume il 9 Mar 2018
Said boutani's comment mistakenly posted as an Answer copied here
In 2D, the formats exist are:
Guillaume
Guillaume il 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.

Accedi per commentare.

Categorie

Scopri di più su Graphics Performance in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by