473,504 Members | 13,601 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Detection of intersecting circles on python

2 New Member
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:


Expand|Select|Wrap|Line Numbers
  1.  
  2. # some parameters
  3.  
  4. # X and Y
  5.  
  6. PosMin_xy = radius + tolerance
  7.  
  8. PosMax_xy = size - (radius + tolerance)
  9.  
  10. step_xy = (PosMax_xy - PosMin_xy) / step_var
  11.  
  12. #Vector with possible x and y coordiantes for circles
  13.  
  14. XYpos = np.arange(PosMin_xy, PosMax_xy, step_xy)
  15.  
  16. # Extraction of random samples from previous vector to 
  17. # build the final position vectors for the circles
  18.  
  19. # would shuffling the XYpos vector be more efficient than extracting random samples?!
  20.  
  21. ElementPos_X = random.sample(XYpos, number_of_fibers)
  22.  
  23. ElementPos_Y = random.sample(XYpos, number_of_fibers)
  24.  
  25.  

The following code is a first attempt of a intersection detection algorithm:

Expand|Select|Wrap|Line Numbers
  1.  
  2. #This function is to check if given coordinates are inside of the reference circle (c_x,c_y)
  3.  
  4. def in_radius(c_x, c_y, r, x, y):
  5.  
  6.     return math.hypot(c_x - x, c_y - y) <= r
  7.  
  8. # This is for checking if all the circles created before intersects with reference circle
  9.  
  10. for item in ElementPos_X:
  11.  
  12.     for item2 in ElementPos_Y:
  13.  
  14.         Val = in_radius(c_x, c_y, radius, item, item2)
  15.  
  16.  
  17.         if Val == 1:          
  18.             print 'Circle with this coordinates: ({0},{1}),  intersects reference circle'.format(item, item2)
  19.  
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
Jun 12 '12 #1
2 5683
dwblas
626 Recognized Expert Contributor
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.
Expand|Select|Wrap|Line Numbers
  1. 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
Expand|Select|Wrap|Line Numbers
  1. for x_ref in range(len(ElementPos_X)-1):
  2.      reference_x = ElementPos_X[x_ref]:
  3.  
  4.      for y_ref in range(len(ElementPos_Y)-1):
  5.          reference_y = ElementPos_Y[y_ref] 
  6.  
  7.          # start with x_ref+1 and y_ref+1 and compare
  8.          # to all remaining circles
  9.          for x in range(x_ref+1, len(ElementPos_X)):
  10.              x_item = ElementPos_X[x]:
  11.  
  12.              for y in range(y_ref+1, len(ElementPos_Y)):
  13.                  y_item = ElementPos_Y[y] 
  14.                  Val = in_radius(reference_x, reference_y,
  15.                        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_pos_x" as the camel case implies that it is a class instead of a list.
Jun 12 '12 #2
NunoMPG
2 New Member
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!
Jun 12 '12 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

22
3374
by: Max M | last post by:
There is a story today on Slashdot Open Source Project Management Lessons ======================================...
9
12771
by: Dr. Colombes | last post by:
What is the easiest way to generate some plots and graphs in Python ? Specifically interested in simple histograms and scatter plots with circles and regression lines. Thanks for your...
0
1868
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 241 open ( -6) / 2622 closed (+26) / 2863 total (+20) Bugs : 764 open ( +6) / 4453 closed (+38) / 5217 total (+44) RFE : 150 open...
22
6276
by: Brad Tilley | last post by:
Is it possible to write a file open, then read program in C and then call the C program from a Python script like this: for root, files, dirs in os.walk(path) for f in files: try:...
17
3663
by: Tim Daneliuk | last post by:
Is anyone aware of freely available Python modules that can do any of the following tasks: 1) Given the latitude/longitude of two locations, compute the distance between them. "Distance" in...
17
2651
by: Mark | last post by:
uhhmmm... not really sure how to word this. i cant get get this to compile.. i'm not sure what the proper syntax to do this is.. hopefully it's self explanatory. here's my class: ------ class...
3
2813
by: Douglas Douglas | last post by:
Hi everybody. I have a paper form that I scan into an image. My user fills some circles in this paper form using black ink. Every form has ten rows with five circles each and the user fills only...
9
5280
by: Carl | last post by:
I am desperately looking for a way to call Python from Matlab. I have become used to Python's rich syntax and large number of libraries, and feel ridiculously clumsy being stuck with Matlab's...
2
2249
by: Chris | last post by:
I have a Bayesian simulation package that keeps running into memory allocation problems. I have a feeling this has something to do with Python (2.5.1.1) not freeing memory. The program essentially...
35
3469
by: eliben | last post by:
Python provides a quite good and feature-complete exception handling mechanism for its programmers. This is good. But exceptions, like any complex construct, are difficult to use correctly,...
0
7213
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7098
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7298
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7366
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5610
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4698
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3187
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
406
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.