def test_ray2d_intersect_with_vertical(self): ray1 = Ray2D((10, 1), (10, -7)) ray2 = Ray2D((-10, 3), (17, -7)) point = ray1.intersect(ray2) self.assertAlmostEqual(point[0], 10., 3) self.assertAlmostEqual(point[1], -4.4074, 3) self.assertRaises(ArithmeticError, ray1.get_y, 1.)
def test_ray2d_angle(self): ray = Ray2D((10, 10), angle=HALF_PI) self.assertTrue(ray.is_vertical) ray = Ray2D((10, 10), angle=0) self.assertTrue(ray.is_horizontal) ray = Ray2D((10, 10), angle=math.pi / 4) self.assertAlmostEqual(ray.slope, 1., 6)
def test_ray2d_intersect(self): ray1 = Ray2D((10, 1), (20, 10)) ray2 = Ray2D((17, -7), (-10, 3)) point = ray1.intersect(ray2) self.assertAlmostEqual(point[0], 5.7434, 3) self.assertAlmostEqual(point[1], -2.8309, 3) self.assertFalse(ray1.is_parallel(ray2))
def test_ray2d_parallel(self): ray1 = Ray2D((17, -8), (-10, 2)) ray2 = Ray2D((-10, 3), (17, -7)) ray3 = Ray2D((-10, 4), (17, -6)) self.assertTrue(ray2.is_parallel(ray3)) self.assertTrue(ray1.is_parallel(ray3)) self.assertRaises(ParallelRaysError, ray2.intersect, ray3)
def test_init_with_angle(self): point = (10, 10) ray = Ray2D(point, angle=0) ray_normal = ray.normal_through(point) self.assertTrue(ray_normal.is_vertical) ray = Ray2D(point, angle=-HALF_PI) self.assertFalse(ray.is_horizontal) self.assertTrue(ray.is_vertical)
def test_bisectrix(self): ray1 = Ray2D((10, 10), angle=math.pi / 3) ray2 = Ray2D((3, -5), angle=math.pi / 2) ray3 = Ray2D((1, 1), angle=math.pi / 3) a = ray1.bisectrix(ray2) self.assertAlmostEqual(a.angle, 1.309, 4) self.assertAlmostEqual(a.get_y(7), 12.80385, 4) self.assertRaises(ParallelRaysError, ray1.bisectrix, ray3)
def center_of_3points_arc(point1, point2, point3): """ Calc center point of 3 point arc. Circle is defined by 3 points on the circle: point1, point2 and point3. """ ray1 = Ray2D(point1, point2) ray2 = Ray2D(point1, point3) midpoint1 = lerp(point1, point2) midpoint2 = lerp(point1, point3) center_ray1 = ray1.normal_through(midpoint1) center_ray2 = ray2.normal_through(midpoint2) return center_ray1.intersect(center_ray2)
def test_intersect_ray_pass(self): circle = Circle((10., 10.), 3) ray1_hor = Ray2D((10., 15.), angle=0) ray2_hor = Ray2D((10., 5.), angle=0) ray1_vert = Ray2D((5., 10.), angle=HALF_PI) ray2_vert = Ray2D((15., 10.), angle=-HALF_PI) ray3 = Ray2D((13.24, 14.95), angle=0.3992) self.assertFalse(circle.intersect_ray(ray1_hor)) self.assertFalse(circle.intersect_ray(ray2_hor)) self.assertFalse(circle.intersect_ray(ray1_vert)) self.assertFalse(circle.intersect_ray(ray2_vert)) self.assertFalse(circle.intersect_ray(ray3))
def test_ray2d_parallel_vertical(self): ray1 = Ray2D((10, 1), (10, -7)) ray2 = Ray2D((11, 0), angle=HALF_PI) ray3 = Ray2D((12, -10), (12, 7)) ray4 = Ray2D((0, 0), slope=1.0) ray5 = Ray2D((0, 0), slope=0) self.assertRaises(ParallelRaysError, ray1.intersect, ray3) self.assertTrue(ray1.is_parallel(ray3)) self.assertTrue(ray1.is_parallel(ray2)) self.assertTrue(ray2.is_parallel(ray2)) self.assertFalse(ray1.is_parallel(ray4)) self.assertFalse(ray2.is_parallel(ray4)) self.assertFalse(ray3.is_parallel(ray4)) self.assertFalse(ray1.is_parallel(ray5)) self.assertFalse(ray2.is_parallel(ray5)) self.assertFalse(ray3.is_parallel(ray5)) # vertical rays can't calc a y-value self.assertRaises(ArithmeticError, ray1.get_y, -1.)
def _setup(self): """ Calc setup values and determines the point order of the dimension line points. """ self.measure_points = [Vector(point) for point in self.measure_points] dimlineray = Ray2D(self.dimlinepos, angle=radians(self.angle)) self.dimline_points = [ self._get_point_on_dimline(point, dimlineray) for point in self.measure_points ] self.point_order = self._indices_of_sorted_points(self.dimline_points) self._build_vectors()
def test_touch(testnum, x, y, _angle, places=7): result = True ray = Ray2D((x, y), angle=_angle) points = circle.intersect_ray(ray, places) if len(points) != 1: result = False else: point = points[0] # print ("{0}: x= {1:.{places}f} y= {2:.{places}f} : x'= {3:.{places}f} y' = {4:.{places}f}".format(testnum, x, y, point[0], point[1], places=places)) if not equals_almost(point[0], x, 4): result = False if not equals_almost(point[1], y, 4): result = False return result
def test_intersect_ray_intersect(self): circle = Circle((10., 10.), 3) ray_vert = Ray2D((8.5, 10.), angle=HALF_PI) cross_points = circle.intersect_ray(ray_vert) self.assertEqual(len(cross_points), 2) p1, p2 = cross_points if p1[1] > p2[1]: p1, p2 = p2, p1 self.assertTrue(equal_points_almost(p1, (8.5, 7.4019), places=4)) self.assertTrue(equal_points_almost(p2, (8.5, 12.5981), places=4)) ray_hor = Ray2D((10, 8.5), angle=0.) cross_points = circle.intersect_ray(ray_hor) self.assertEqual(len(cross_points), 2) p1, p2 = cross_points if p1[0] > p2[0]: p1, p2 = p2, p1 self.assertTrue(equal_points_almost(p1, (7.4019, 8.5), places=4)) self.assertTrue(equal_points_almost(p2, (12.5981, 8.5), places=4)) ray_slope = Ray2D((5, 5), (16, 12)) cross_points = circle.intersect_ray(ray_slope) self.assertEqual(len(cross_points), 2) p1, p2 = cross_points if p1[0] > p2[0]: p1, p2 = p2, p1 self.assertTrue(equal_points_almost(p1, (8.64840, 7.3217), places=4)) self.assertTrue(equal_points_almost(p2, (12.9986, 10.0900), places=4)) # ray with slope through midpoint ray_slope = Ray2D((10, 10), angle=HALF_PI / 2) cross_points = circle.intersect_ray(ray_slope) self.assertEqual(len(cross_points), 2) p1, p2 = cross_points if p1[0] > p2[0]: p1, p2 = p2, p1 # print (p1[0], p1[1], p2[0], p2[1]) self.assertTrue(equal_points_almost(p1, (7.8787, 7.8787), places=4)) self.assertTrue(equal_points_almost(p2, (12.1213, 12.1213), places=4)) # horizontal ray through midpoint ray_hor = Ray2D((10, 10), angle=0) cross_points = circle.intersect_ray(ray_hor) self.assertEqual(len(cross_points), 2) p1, p2 = cross_points if p1[0] > p2[0]: p1, p2 = p2, p1 # print (p1[0], p1[1], p2[0], p2[1]) self.assertTrue(equal_points_almost(p1, (7, 10), places=4)) self.assertTrue(equal_points_almost(p2, (13, 10), places=4)) # vertical ray through midpoint ray_vert = Ray2D((10, 10), angle=HALF_PI) cross_points = circle.intersect_ray(ray_vert) self.assertEqual(len(cross_points), 2) p1, p2 = cross_points if p1[1] > p2[1]: p1, p2 = p2, p1 # print (p1[0], p1[1], p2[0], p2[1]) self.assertTrue(equal_points_almost(p1, (10, 7), places=4)) self.assertTrue(equal_points_almost(p2, (10, 13), places=4))
def test_ray2d_normal_horizontal(self): ray = Ray2D((10, 10), (20, 10)) # horizontal line norm = ray.normal_through((3, 3)) point = ray.intersect(norm) self.assertAlmostEqual(point[0], 3.0, 3) self.assertAlmostEqual(point[1], 10.0, 3)
def test_ray2d_normal(self): ray = Ray2D((-10, 3), (17, -7)) norm = ray.normal_through((3, 3)) point = ray.intersect(norm) self.assertAlmostEqual(point[0], 1.4318, 3) self.assertAlmostEqual(point[1], -1.234, 3)
def test_ray2d_normal_vertical(self): ray = Ray2D((10, 1), (10, -7)) # vertical line norm = ray.normal_through((3, 3)) point = ray.intersect(norm) self.assertAlmostEqual(point[0], 10.0, 3) self.assertAlmostEqual(point[1], 3.0, 3)
def test_init_with_slope(self): ray1 = Ray2D((10, 10), slope=1) ray2 = Ray2D((-3, -3), slope=-1) self.assertAlmostEqual(ray1.get_y(15), 15, 3) self.assertAlmostEqual(ray2.get_y(-2), -4, 3)
def test_Ray2D_get_x_y(self): ray1 = Ray2D((10, 1), (20, 10)) y = ray1.get_y(15) self.assertAlmostEqual(y, 5.5, 3) x = ray1.get_x(y) self.assertAlmostEqual(x, 15., 3)