def test_equals(): # test for equals assert (equals(0.1, 0.1)) assert (equals(1.0, 1.0-1e-15)) assert (equals(1.0, 1.0-1e-5) is False) assert (equals(-2.0, -2.0+1e-15)) assert (equals(-2.0, -2.0+1e-5) is False)
def test_phase_diff_complex(): # test for phase_diff_complex assert (equals(phase_diff_complex(1.0+1.0j, 1.0+0.0j), 45.0)) assert (equals(phase_diff_complex(1.0+0.0j, 1.0+1.0j), 315.0)) assert (equals(phase_diff_complex(1.0+1.0j, -2.0+2.0j), 270.0)) assert (equals(phase_diff_complex(1.0+1.0j, 2.0-2.0j), 90.0)) assert (equals(phase_diff_complex(3.0+4.0j, 3.0+4.0j), 0.0))
def test_norm_complex(): # test for norm_complex assert (equals(norm_complex(3.0+4.0j), 25.0)) assert (equals(norm_complex(3.0-4.0j), 25.0)) assert (equals(norm_complex(1.0+2.0j), 5.0)) assert (equals(norm_complex(-1.0+2.0j), 5.0)) assert (equals(norm_complex(-2.0-2.0j), 8.0))
def test_commonpoint_of_circles_complex(): # test for commonpoint_of_circles_complex c1, c2 = commonpoint_of_circles_complex(0 + 1j, 5, 4 + 3j, math.sqrt( 5)) # x^2 + (y-1)^2 = 25, (x-4)^2 + (y-3)^2 = 5 の交点 (3,5), (5,1) assert (equals(c1.real, 3)) assert (equals(c1.imag, 5)) assert (equals(c2.real, 5)) assert (equals(c2.imag, 1))
def test_commonpoint_of_line_circle_complex(): # test for commonpoint_of_line_circle_complex c1, c2 = commonpoint_of_line_circle_complex( 0 + 1j, 1 + 2j, 0 + 0j, 1) # x^2+y^2=1 と y=x+1 の交点 (0,1), (-1,0) assert (equals(c1.real, 0)) assert (equals(c1.imag, 1)) assert (equals(c2.real, -1)) assert (equals(c2.imag, 0))
def test_cross_point_of_segments_complex(): # test for crosspoint_of_segments_complex assert (equals( crosspoint_of_segments_complex(-1 - 1j, 1 + 3j, -1 + 1j, 1 - 1j).real, -1 / 3)) # y = 2x + 1 と y = -x の交点 (-1/3, 1/3) assert (equals( crosspoint_of_segments_complex(-1 - 1j, 1 + 3j, -1 + 1j, 1 - 1j).imag, 1 / 3))
def test_dist_between_segments_complex(): # test for dist_between_segments_complex assert (equals( dist_between_segments_complex(0 + 0j, 2 + 2j, 0 + 2j, 2 + 0j), 0)) # 線分が交わる assert (equals( dist_between_segments_complex(0 + 0j, 10 + 0j, 5 + 5j, 5 + 6j), 5)) assert (equals( dist_between_segments_complex(0 + 0j, 0 + 10j, 5 + 5j, 5 + 6j), 5))
def test_dist_between_point_line_complex(): # test for dist_between_point_line_complex assert (equals( dist_between_point_line_complex(0 + 0j, complex(1, math.sqrt(3)), 2 + 0j), math.sqrt(3))) # y = √3x と (2, 0) の距離 √3 assert (equals(dist_between_point_line_complex(0 + 0j, 0 + 1j, 2 + 0j), 2)) # y 軸と (2, 0) の距離 2 assert (equals(dist_between_point_line_complex(0 + 0j, 0 + 1j, 0 + 3j), 0)) # on line
def test_dist_between_point_segment_complex(): # test for dist_between_point_segment_complex assert (equals( dist_between_point_segment_complex(0 + 0j, 10 + 20j, 3 + 1j), math.sqrt(5))) # 直線 y=2x と (3,1) の距離は √5 だが assert (equals(dist_between_point_segment_complex(0 + 0j, -1 - 2j, 3 + 1j), math.sqrt(10))) # このような線分になると (0, 0) との距離が最短となる assert (equals( dist_between_point_segment_complex(5 + 10j, 10 + 20j, 3 + 1j), math.sqrt(85))) # このような線分になると (5, 10) との距離が最短となる assert (equals(dist_between_point_segment_complex(0 + 0j, 0 + 10j, 0 + 5j), 0)) # on segment
def test_rotate_complex(): # test for rotate_complex assert (equals(math.degrees(cmath.phase(rotate_complex(1.0+1.0j, 45))), 90)) assert (equals(math.degrees(cmath.phase(rotate_complex(1.0+1.0j, -45))), 0)) assert (equals(rotate_complex(3.0+4.0j, 180).real, -3.0)) assert (equals(rotate_complex(3.0+4.0j, 180).real, -3.0)) assert (equals(rotate_complex(1.0+0.0j, 90).real, 0.0)) assert (equals(rotate_complex(1.0+0.0j, 90).imag, 1.0)) assert (equals(rotate_complex(3.0+5.0j, 360).real, 3.0)) assert (equals(rotate_complex(3.0+5.0j, 360).imag, 5.0)) assert (rotate_complex(3.0+5.0j, 0) == 3.0+5.0j) assert (rotate_complex(3.0+5.0j, 0) == 3.0+5.0j) assert (equals(abs(rotate_complex(cmath.rect(1.3, math.radians(35)), 52)), 1.3)) assert (equals(math.degrees(cmath.phase(rotate_complex(cmath.rect(1.3, math.radians(35)), 52))), 87))
def test_ccw(): # test for ccw assert (equals(ccw(0 + 0j, 1 + 1j, -1 + 1j), 2.0)) assert (equals(ccw(0 + 0j, -1 + 1j, 1 + 1j), -2.0)) assert (equals(ccw(1 + 1j, 2 + 2j, 3 + 3j), 0))
def test_reflection_complex(): # test for reflection_complex assert (equals(reflection_complex(2.0+5.5j, 1.0+3.5j, -2.0+4.0j).real, 16/5)) # (-2, 4) からの 4x-2y+3=0 に対する反射 assert (equals(reflection_complex(2.0+5.5j, 1.0+3.5j, -2.0+4.0j).imag, 7/5)) assert (equals(reflection_complex(1.0+1.0j, 3.0+3.0j, 2.0+2.0j).real, 2.0)) # そもそも直線上 assert (equals(reflection_complex(1.0+1.0j, 3.0+3.0j, 2.0+2.0j).imag, 2.0))
def test_projection_complex(): # test for projection_complex assert (equals(projection_complex(2.0-1.0j, 5.0-3.0j, 4.0+5.0j).real, 8/13)) # (4,5) から 2x+3y-1=0 への射影 assert (equals(projection_complex(2.0-1.0j, 5.0-3.0j, 4.0+5.0j).imag, -1/13)) assert (equals(projection_complex(1.0+1.0j, 3.0+3.0j, 2.0+2.0j).real, 2.0)) # そもそも直線上 assert (equals(projection_complex(1.0+1.0j, 3.0+3.0j, 2.0+2.0j).imag, 2.0))
def test_area_of_parallelogram_complex(): # test for area_of_parallelogram_complex assert (equals(area_of_parallelogram_complex(3.0+4.0j, 5.0+6.0j), 2.0)) assert (equals(area_of_parallelogram_complex(1.0+3.0j, 5.0+7.0j), 8.0)) assert (equals(area_of_parallelogram_complex(-1.0-2.0j, -2.0-4.0j), 0)) # 直線 assert (equals(area_of_parallelogram_complex(2.0+3.0j, -3.0+2.0j), 13.0)) # 長方形
def test_dot_product_complex(): # test for dot_product_complex assert (equals(dot_product_complex(2.0+3.0j, 4.0+5.0j), 23.0)) assert (equals(dot_product_complex(0+0j, 100+100j), 0)) assert (equals(dot_product_complex(1.0+1.0j, 2.0+2.0j), 4.0)) # 平行 assert (equals(dot_product_complex(1000+0j, 0+100j), 0)) # 直交