2
$\begingroup$

I want to generate circles as the distribution of random circles in a square cell. these circles have the same radius. constraints applied to these circles are: (i) these circles not overlap each other. (ii) these circles do not touch the square cell walls.

I want to create this algorithm with python, can you help me?

$\endgroup$

2 Answers 2

2
$\begingroup$

enter image description here This script should do the trick:

import bpy
from mathutils import Vector
from random import random

def check_circle_bounbox( circC, circR, rectC, rectR ):
    ''' Make sure this circle does not protrude outside the rectangle's bounding box '''
    maxX = rectC.x + rectR
    minX = rectC.x - rectR
    maxY = rectC.y + rectR
    minY = rectC.y - rectR

    withinX = ( circC.x + circR <= maxX ) and ( circC.x - circR >= minX )
    withinY = ( circC.y + circR <= maxY ) and ( circC.y - circR >= minY )

    return withinX and withinY

def check_overlap( circles, circC, circR ):
    ''' Make sure the distance between the current circle's center and all
        other circle centers is greater than or equal to the circle's perimeter (2r)
    '''
    return len(
        [ True for c in circles if ( c - circC ).length >= circR * 2 ]
    ) == len( circles )

circleRadius = 1.5
circleCount  = 50
rectCenter   = Vector((0, 0, 0))
rectRadius   = 10

circles = []

maxIterations = 500 # Set max number of loop iterations to prevent infinite loop

z = 0 # All circles lie on Z = 0
i = 0 # Current loop iteration
while len( circles ) < circleCount and i < maxIterations:
    x = rectCenter.x + 2 * rectRadius * random() - rectRadius
    y = rectCenter.y + 2 * rectRadius * random() - rectRadius
    circC = Vector((x, y, z ))

    if check_circle_bounbox( circC, circleRadius, rectCenter, rectRadius ) \
       and check_overlap( circles, circC, circleRadius ):
           circles.append( circC )

    i += 1

for c in circles:
    bpy.ops.mesh.primitive_circle_add( radius = circleRadius, location = c, fill_type = 'NGON'  )
$\endgroup$
6
  • $\begingroup$ good work, thank you TLouskyn for Full circle, how to add this type $\endgroup$
    – saded
    May 8, 2016 at 13:01
  • $\begingroup$ the faces of circles are full (ex: ekladata.com/2xrMvGO8ZBWexfsolnVoKFhqeNg.jpg) $\endgroup$
    – saded
    May 8, 2016 at 13:45
  • $\begingroup$ @saded, I changed the fill type to NGON (default is no fill). $\endgroup$
    – TLousky
    May 8, 2016 at 13:49
  • $\begingroup$ yes, how to add this? $\endgroup$
    – saded
    May 8, 2016 at 13:52
  • $\begingroup$ Use the updated script in the answer above. The last line (#48) was changed to set the fill type parameter with fill_type = 'NGON'. $\endgroup$
    – TLousky
    May 8, 2016 at 13:53
2
$\begingroup$

Particle System

enter image description here

enter image description here

enter image description here

Some stages of creation.

User Ortho View above

  • [A] Plane with Loop Cuts
  • [B] Edit Mode. Face Mode. Inset Faces. Vertex Mode. Invert selection. Delete Vertices. Faces remain with empty margin.
  • [C] Edit Mode. Face Mode. Random Select from Menu. Delete Faces.
  • [D] Particle System using blue torus as Render [Object]. The radius of torus < (.5 Margin) is the intention.

Image Editor Render Result

  • Particles Rendered

Particle System settings shown above.

Of course your variations and improvements are welcome. Click on image to see larger version.

$\endgroup$
2
  • $\begingroup$ I don't think this is Random $\endgroup$
    – Chebhou
    May 7, 2016 at 19:19
  • $\begingroup$ Good work, but I want a script python $\endgroup$
    – saded
    May 7, 2016 at 19:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .