コード例 #1
0
def check_all(points, lines):
    crosses = []
    for i in xrange(len(lines)):
        s1 = points[lines[i,0]], points[lines[i,1]]
        p11 = lines[i,0]
        p12 = lines[i,1]
        for j in xrange(i+1, len(lines)):
            p21 = lines[j,0]
            p22 = lines[j,1]
            #print "segment 1:",p11, p12
            #print "segment 2:",p21, p22
            if (p11 == p21 and p12 == p22) or (p11 == p22 and p12 == p21): # they are the same two points:
                #print "segments are the same"
                crosses.append( (i, j) )
                break # no need to check further
            elif p11 == p21:
                #print "first point matches first point"
                break                    
            elif p11 == p22:
                #print "first point matches second point"
                break                    
            elif p12 == p21:
                #print "second point matches first point"
                break                    
            elif p12 == p22:
                #print "second point matches second point"
                break                    
            s2 = points[lines[j,0]], points[lines[j,1]]
            if segment_cross(s1, s2):
                crosses.append( (i,j) )
    return crosses
コード例 #2
0
def test_segment_cross_no():
    S1 = ((0.0, 0.0), (10.0, 10.0))

    for S2 in [ ((10.0, 0.0), (10.0, -10.0)),
                ((-1.0, 0.0), ( 0.0,  -1.0)),
                ((11.0, 9.0), ( 9.0, -11.0)),
                (( 5.0, 5.00000000000001), ( 0.0,  5.0)),
              ]:
        assert not segment_cross(S1, S2)
コード例 #3
0
def test_segment_cross_yes():
    S1 = ( (0.0, 0.0), (10.0, 10.0) )

    for S2 in [ ( (10.0,  0.0), ( 0.0, 10.0) ),
                ( ( 0.0,  0.0), ( 1.0,  0.0) ),
                ( (10.0, 10.0), (20.0, 10.0) ), # just touch the end
                ( (10.0, 10.0), (20.0, 10.0) ),
                (( 5.0, 5.0), ( 0.0,  5.0)),
              ]:
        
        print S1, S2
        assert segment_cross(S1, S2)