Example #1
0
def test_intersection():    
    def r():
        return random.randint(-50,50)
    random.seed(0)
    for x in xrange(1000):
        la=Line(Vertex(r(),r()),Vertex(r(),r()))
        lb=Line(Vertex(r(),r()),Vertex(r(),r()))
        if not points(la).intersection(points(lb)):
            continue
        try:
            ls=list(la.intersect(lb))
            have_split=False
            assert len(ls)>0    
            middle=ls[0]
            a,b,c,d=ls[1:]
            have_split=True
            sqs=[]
            sm=points(middle)
            sa=points(a)
            sb=points(b)
            sc=points(c)
            sd=points(d)
            sla=points(la)
            slb=points(lb)
            aplusb=points(la).union(points(lb))
            assert sm.intersection(aplusb)==sm
            assert sa.intersection(sla)==sa
            assert sb.intersection(slb)==sb
            assert sc.intersection(sla)==sc
            assert sd.intersection(slb)==sd
            splitted_union=sa.union(sc).union(sm).union(sb).union(sd)
            superf=splitted_union.difference(sla.union(slb))
            assert len(superf)==0
            #Note that not all points in la and lb are in m+a+b+c+d.            
        except Exception,cause:
            print "Oops:",cause
            if have_split:
                visualize_diff([
                                (la,(255,0,0)),
                                (lb,(0,255,0)),
                                ],[])
                visualize_diff([
                                (la,(255,0,0)),
                                (lb,(0,255,0)),
                                (middle,(0,0,255))
                                ],sqs)
                
                visualize_diff([
                                (a,(255,0,0)),
                                (b,(128,0,0)),
                                (c,(0,0,255)),
                                (d,(0,0,128)),
                                (middle,(0,255,0))
                                ],[])
            else:
                print "Visualize two lines"
                #raise
                visualize_diff([
                                (la,(255,0,0)),
                                (lb,(0,255,0))
                                ])
            raise