This project was inspired by Ken Goulding's block in JavaScript. _place_circle ( r ) circles = Circles ( n = 2000 ) circles. sort () # Do our best to place the circles, larger ones first. We use # random.random() * random.random() to favour small circles. print ( 'guard reached.' ) def make_circles ( self ): """Place the little circles inside the big one.""" # First choose a set of n random radii and sort them. They are computed to pass through the corners of the bounding box and. append ( circle ) return guard -= 1 # Warn that we reached the guard number of attempts and gave up for # for this circle. The picture shows what the 4 bounding circles look like when the radius is decreased. circles ): # The circle doesn't overlap any other circle: place it. Packing problems for regular shapes (circles and rectangles) of objects and/or. R : # The circle fits inside the larger circle. Keywords: circle packing, integer programming, large scale optimization. guard = 500 while guard : # Pick a random position, uniformly on the larger circle's interior cr, cphi = ( self. fo ) def _place_circle ( self, r ): # The guard number: if we don't place a circle within this number # of trials, we give up. colours ) with open ( filename, 'w' ) as self. fo ) def make_svg ( self, filename, * args, ** kwargs ): """Create the image as an SVG file with name filename.""" ncolours = len ( self. fo ) return wrapper def svg_styles ( self ): """Set the SVG styles: circles are coloured with no border.""" print ( 'circle '. fo ) def defs_decorator ( func ): """For convenience, wrap the CSS styles with the needed SVG tags.""" def wrapper ( self ): print ( """ """, file = self. colours = colours or def preamble ( self ): """The usual SVG preamble, including the image size.""" print ( ' \n ' ''. n = R, n # The centre of the canvas self. colours is a list of SVG fill colour specifiers to be referenced by the class identifiers c. rho_max is rmax/R, giving the maximum packing circle radius. rho_min is rmin/R, giving the minimum packing circle radius. n is the maximum number of circles to pack inside the large circle. width, height are the SVG canvas dimensions R is the radius of the large circle within which the small circles are to fit. icolour ), file = fo ) class Circles : """A class for drawing circles-inside-a-circle.""" def _init_ ( self, width = 600, height = 600, R = 250, n = 800, rho_min = 0.005, rho_max = 0.05, colours = None ): """Initialize the Circles object. icolour = icolour def overlap_with ( self, cx, cy, r ): """Does the circle overlap with another of radius r at (cx, cy)?""" d = np. icolour is the index of the circle's colour. Import numpy as np class Circle : """A little class representing an SVG circle.""" def _init_ ( self, cx, cy, r, icolour = None ): """Initialize the circle with its centre, (cx,cy) and radius, r.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |