def test_distance_vectors_holding_other_than_numbers(self):
     u = ('a', 'b')
     v = ('c', 'd')
     with self.assertRaises(
             TypeError,
             msg=
             'should raise TypeError when passing vectors holding no numeric values'
     ):
         distance(u, v)
 def test_distance_vectors_different_sizes(self):
     u = (1, 2)
     v = (1, 2, 3)
     with self.assertRaises(
             ValueError,
             msg=
             'should raise ValueError when passing two vectors of different sizes'
     ):
         distance(u, v)
 def test_distance_happy_path(self):
     u = (1, 2, 3)
     v = (4, 5, 6)
     distance_actual_result = distance(u, v)
     distance_expected_result = sqrt((u[0] - v[0])**2 + (u[1] - v[1])**2 +
                                     (u[2] - v[2])**2)
     self.assertEqual(distance_actual_result, distance_expected_result,
                      'distance vectors of same size should match')
Beispiel #4
0
def do_segments_intersect(s1, s2):
    """Return True if the two segments defined by s1 and s2 intersect,
    or false otherwise.
    s1 and s2 are two sets of points.
    """
    u1, u2 = s1
    v1, v2 = s2
    d1 = distance(u1, u2)
    d2 = distance(v1, v2)
    try:
        x, y = intersection(u1, u2, v1, v2)
        return (distance(u1, (x, y)) <= d1 and distance(u2, (x, y)) <= d1
                and distance(v1, (x, y)) <= d2 and distance(v2, (x, y)) <= d2)

    except np.linalg.LinAlgError:
        return False