Detection of intersecting circles on python

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • NunoMPG
    New Member
    • Jun 2012
    • 2

    Detection of intersecting circles on python

    Hello all!

    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:


    Code:
    # 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:

    Code:
    #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)
    The problem is that this last script checks if created circles are intersecting with only one circle, the reference circle.
    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
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    The general theory, AFAIK, is to find the distance between the two centers and they intersect if this is less than radius_1+radius _2, so I assume this is what you want to do. You can use a function for this, as you have posted, and pass the x and y coordinates to it for any two circles.
    Code:
    return math.hypot(c_x - x, c_y - y) <= r
    I have not tested this, but when when x is greater than c_x you get a negative number. You may want to pass the abs() value (and you appear to pass r to the function, not r1+r2).

    To compare to all circles
    Code:
    for x_ref in range(len(ElementPos_X)-1):
         reference_x = ElementPos_X[x_ref]:
    
         for y_ref in range(len(ElementPos_Y)-1):
             reference_y = ElementPos_Y[y_ref] 
    
             # start with x_ref+1 and y_ref+1 and compare
             # to all remaining circles
             for x in range(x_ref+1, len(ElementPos_X)):
                 x_item = ElementPos_X[x]:
    
                 for y in range(y_ref+1, len(ElementPos_Y)):
                     y_item = ElementPos_Y[y] 
                     Val = in_radius(reference_x, reference_y,
                           radius1+radius2, x_item, y_item)
    Instead of storing all x values in one list and all y values in another, consider using a list of lists (or tuples), for the center point i.e. element_pos=[(x1, y1), (x2, y2)]. For future reference, when you post code, it is easier to understand if you follow the Python Style Guide, so "ElementPos _X" should be "element_po s_x" as the camel case implies that it is a class instead of a list.

    Comment

    • NunoMPG
      New Member
      • Jun 2012
      • 2

      #3
      Hi dwblas!

      I believe that the function i mentioned works even when x is greater than c_x because math.hypot does the the norm of the two values passed to the function like so: sqrt((c_x-x)**2+(c_y-y)**2).

      As for the piece of code you posted i will test it tomorrow and and see how it goes, but as far as i can tell it seems a good solution to compare all the circles.

      tuples and lists still makes me a bit confused when i have to use them in loops and other operations but i will dig a bit more into this!

      Thank you were very helpfull!

      Comment

      Working...