On 17/02/15 04:22, Levi Adissi wrote:

Hello. Thanks for your post.
However could you please use a subject that rflects the content. Otherwise we wind up with an archive full of "Hey Guys", "Hello chaps", "Gday mate", Bonjour" etc Not very useful for searching.

So I'm kind of stuck trying to program a function that returns a list of
tuples.

Note that your function prints the list it does not return the list.
The two are very different.

The function takes 2 lists containing circles of which it should
compare list1[0] to list2[0] to see if they intersect. If they intersect or
touch then I should return them on a list of tuples(in the tuple would be
both intersecting circles).

I can't get circles_only to work the way I see it I'm comparing h to x only
if they're both in the same place on the list (hence my "h==x") I know it
doesn't work because the test returns None so I would really appreciate an
alternative method if you guys see one.

Here are my functions:


def circles_overlap(c1, c2):
    x=(c2.center.y-c1.center.y)**2
    y=(c2.center.x-c1.center.x)**2
    distancemid=math.sqrt(x+y)
    distancerad=(c1.radius+c2.radius)
    if distancemid > distancerad:
        return 1
    elif distancemid < distancerad:
        return -1
    elif distancemid == distancerad:
        return 0


Since this is a test you should probably just return True or False.
Either they overlap or they don't. If you want to use touching as a third result you can still give a boolean for the general case:

return
1 for overlap
-1 for touching
0 for not touching

1 and -1 both evaluate to True in a boolean sense
0 evaluates to False

This makes the test code in your second function look like:

        if circles_overlap(c1,c2):
             newlst.append((c1,c2))

and you don't need the else clause

def circles_only(lst1, lst2):
    newlst=[]
    for h in lst1:
       for x in lst2:
          if h==x:

Why do you have this test? You only check if they overlap
when they are equal? That seems odd to me.
Also h and x seem an odd choice of name for two circles.
Why not

for c1 in lst1:
  for c2 in lst2:

             if circles_overlap(lst1[h],lst2[x])== -1:
                newlst.append(lst1[h],lst2[x])

Now you are trying to use indexing to extract the circles
but you are (quite rightly) iterating over the lists directly
not using indexes. So h and x are circles, you don't need
to  get them out of the lists.

Also you are trying to store two items in your newlist rather
than a tuple. (See my suggested test above.)

HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to