On 02/16/2015 11:22 PM, Levi Adissi wrote:

Thank you for using text email, rather than the html mail that so many newcomers use.

So I'm kind of stuck trying to program a function that returns a list of
tuples. 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

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

That's silly. You don't want to compare the two circles to see if they're equal. Remove this line.

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

Why don't you tell us the exception this line causes? lst1 is subscripted by integers, not by circle objects.

What you really want in this line is something like:
               if circles_overlap(h, x) ! = 1:
                   newlst.append(h, x)


                newlst.append(lst1[h],lst2[x])

             elif circles_overlap(lst1[h],lst2[x])== 0:
                newlst.append(lst1[h],lst2[x])

    print newlst

Don't print it, return it.  Otherwise, you're returning None.



TEST CASE:

     def test_circles_olap1(self):
         list1=[data_2.Circle(data_2.Point(2,3),
2),data_2.Circle(data_2.Point(2,3), 2), data_2.Circle(data_2.Point(2,3), 2)
                ]
         list2=[data_2.Circle(data_2.Point(6,3),
2),data_2.Circle(data_2.Point(10,3), 2), data_2.Circle(data_2.Point(5,3), 2)
                ]
         testor=functions_2.circles_only(list1,list2)
         newlist=[(data_2.Circle(data_2.Point(2,3),
2),data_2.Circle(data_2.Point(6,3), 2)),(data_2.Circle(data_2.Point(2,3),
2),data_2.Circle(data_2.Point(10,3), 2))]
         self.assertEqual(testor, newlist)


The test code makes no sense to me at all. it's a method of some unspecified class, and it uses some namespaces called data_2 and functions_2 for an unknown purpose.



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

Reply via email to