def test_between(): assert not Heading(0).between(10, 30) assert not Heading(10).between(10, 30) assert Heading(20).between(10, 30) assert not Heading(30).between(10, 30) assert not Heading(40).between(10, 30) assert not Heading(-20).between(-10, 10) assert not Heading(-10).between(-10, 10) assert Heading(0).between(-10, 10) assert not Heading(10).between(-10, 10) assert not Heading(20).between(-10, 10)
def test_angle_to(): assert Heading(0).angle_to(170) == Angle(170) assert Heading(170).angle_to(0) == Angle(-170) assert Heading(0).angle_to(190) == Angle(-170) assert Heading(190).angle_to(0) == Angle(170) assert Heading(45).angle_to(-45) == Angle(-90) assert Heading(-45).angle_to(45) == Angle(90) assert Heading(90).angle_to(-90) == Angle(180) assert Heading(-90).angle_to(90) == Angle(180) assert_raises( TypeError, lambda: Heading(0).angle_to(Angle(90)) )
def test_init_error(): # Make sure that we catch errors in algorithms early. # Don't allow None to be treated as an Angle. assert_raises( ValueError, lambda: Angle(None) ) assert_raises( ValueError, lambda: Heading(None) ) # Don't allow Headings and Angles to be conflated. They are conceptually different. assert_raises( TypeError, lambda: Angle(Heading(45)), ) assert_raises( TypeError, lambda: Heading(Angle(45)), )
def f(n): a = 12 b = 0.03 c = 0.2 d = 1.5 e = 0.5 wobble = a * math.exp(-b * n) * math.sin(c * n + d * n**e) return ( Angle(-24 + wobble), Angle(24 + wobble), ) center_heading = Heading(90) center = p.position p.turn_to(center_heading) num_layers = 26 for layer in range(num_layers): lo, hi = f(layer) lo = center_heading + lo hi = center_heading + hi p.arc_right((p.heading - lo) + 90, center=center) p.arc_left(180, 1) p.arc_left((hi - p.heading) + 90, center=center) if layer < (num_layers - 1):
def test_radians(): assert Heading(90).rad == math.pi / 2 assert Angle(90).rad == math.pi / 2 assert Heading.from_rad(math.pi / 2) == 90 assert Angle.from_rad(math.pi / 2) == 90
def test_subtract_heading(): assert Heading(30) - Heading(10) == Angle(20) assert Heading(10) - Heading(30) == Angle(340) assert Heading(10) - Heading(-10) == Angle(20) assert Heading(-10) - Heading(10) == Angle(340)
def test_compare_heading(): assert Heading(45) > Heading(0) assert Heading(45) >= Heading(0) assert Heading(0) < Heading(45) assert Heading(0) <= Heading(45) assert Heading(-135) > Heading(135) assert Heading(-135) >= Heading(135) assert Heading(135) < Heading(-135) assert Heading(135) <= Heading(-135) assert Heading(10) > Heading(-10) assert Heading(10) >= Heading(-10) assert Heading(-10) < Heading(10) assert Heading(-10) <= Heading(10) assert not Heading(42) > Heading(42) assert not Heading(42) < Heading(42) assert Heading(42) >= Heading(42) assert Heading(42) <= Heading(42) # Opposing headings count as greater than each other. assert Heading(180) > Heading(0) assert Heading(0) > Heading(180) assert Heading(90) > Heading(270) assert Heading(270) > Heading(90)
def test_eq(): assert Heading(10) == Heading(10) assert not Heading(10) != Heading(10) assert Heading(10) != Heading(20) assert not Heading(10) == Heading(20)