I'm trying to build a python script to create circles, place them randomly within specific limits and the most important part is that the algorithm must be able to detect any intersecting circles and if any is found, move them to another position.
This script is to be used with ABAQUS finite element simulation package to create a laminated composite plate with embedded random fibers.
This first piece of code is to create random coordinates for the circles and this is what i have so far:
Expand|Select|Wrap|Line Numbers
- # some parameters
- # X and Y
- PosMin_xy = radius + tolerance
- PosMax_xy = size - (radius + tolerance)
- step_xy = (PosMax_xy - PosMin_xy) / step_var
- #Vector with possible x and y coordiantes for circles
- XYpos = np.arange(PosMin_xy, PosMax_xy, step_xy)
- # Extraction of random samples from previous vector to
- # build the final position vectors for the circles
- # would shuffling the XYpos vector be more efficient than extracting random samples?!
- ElementPos_X = random.sample(XYpos, number_of_fibers)
- ElementPos_Y = random.sample(XYpos, number_of_fibers)
The following code is a first attempt of a intersection detection algorithm:
Expand|Select|Wrap|Line Numbers
- #This function is to check if given coordinates are inside of the reference circle (c_x,c_y)
- def in_radius(c_x, c_y, r, x, y):
- return math.hypot(c_x - x, c_y - y) <= r
- # This is for checking if all the circles created before intersects with reference circle
- for item in ElementPos_X:
- for item2 in ElementPos_Y:
- Val = in_radius(c_x, c_y, radius, item, item2)
- if Val == 1:
- print 'Circle with this coordinates: ({0},{1}), intersects reference circle'.format(item, item2)
What i want is a way of checking if there is any intersecting circles and them move them to new coordinates.
Also, i would be very appreciated for any tips on how i can improve efficiency of my pieces of code.
Thanks in advance for any help