#! /usr/bin/python3 # =================================================================== # =================================================================== from numpy import * import user_interface as ui import draw_xy_axes as ax import coordinate_conversion as cc from graphics import * from parallel_lines import parallel_lines from line_intersection import intersection_point # ------------------------------------------------------------------ # ---- test data (an array of arrays which are tests) # ---- each test is an array contains two tuples and a # ---- test description string # ---- each tuple is a line segment used for the test # ---- each line segment contains a start X,Y coordinate and # ---- an end X,Y coordinate # ---- # ---- test line segment #1 # ---- [0] line segment #1 start X coord # ---- [1] line segment #1 start Y coord # ---- [2] line segment #1 end X coord # ---- [3] line segment #1 end Y coord # ---- # ---- test line segment #2 # ---- [0] line segment #2 start X coord # ---- [1] line segment #2 start Y coord # ---- [2] line segment #2 end X coord # ---- [3] line segment #2 end Y coord # ---- # ---- test description (a string) # ------------------------------------------------------------------ accuracy = 0.0001 tests = [ [ (-100.0,200.0,100.0,200.0), (-50.0,-50.0,250.0,350.0), 'test #1' ], [ (50.0,300.0,350.0,300.0), (0.0,-50.0,300.0,350.0), 'test #2' ], [ (-200.0,-200.0,-200.0,100.0), (-40.0,-100.0,200.0,-200.0), 'test #3' ], [ (-325.0,300.0,-325.0,200.0), (-350.0,300.0,-350.0,200.0), 'test #4 - vertical parallel lines' ], [ (-200.0,325.0,0.0,325.0), (-200.0,350.0,0.0,350.0), 'test #5 - horizontal parallel lines' ], [ (-200.0,150.0,-150.0,250.0), (-225.0,150.0,-175.0,250.0), 'test 6 - same slope parallel lines' ], [ (-300.0,-300.0,-300.0,100.0), (-350.0,-200.0,-50.0,-300.0), 'test #7' ], [ (250.0,0.0,350.0,0.0), (300.0,-100.0,300,100.0), 'test #8' ], ] # ------------------------------------------------------------------- # ---- draw line segment # ------------------------------------------------------------------- def draw_line_segment(win,x1,y1,x2,y2,color='black'): draw_line(win,x1,y1,x2,y2,color) draw_point(win,x1,y1,'black') draw_point(win,x2,y2,'yellow') # ------------------------------------------------------------------- # ---- draw line # ------------------------------------------------------------------- def draw_line(win,x1,y1,x2,y2,color='black'): wx1,wy1 = cc.center_to_win_coords(x1,y1,win.width,win.height) wx2,wy2 = cc.center_to_win_coords(x2,y2,win.width,win.height) l = Line(Point(wx1,wy1),Point(wx2,wy2)) l.setWidth(2) l.setFill(color) l.draw(win) # ------------------------------------------------------------------- # ---- draw point (small circle) # ------------------------------------------------------------------- def draw_point(win,x,y,color='red'): wx,wy = cc.center_to_win_coords(x,y,win.width,win.height) c = Circle(Point(wx,wy),4) c.setFill(color) c.draw(win) # ------------------------------------------------------------------- # ---- basic drawing test # ------------------------------------------------------------------- def basic_drawing_test(win): # ---- draw point draw_point(win,100,100,'red') # ---- draw line draw_line_segment(win,-100,-100,-45,50) # ------------------------------------------------------------------- # ---- data type and value # ------------------------------------------------------------------- def point_data_type(p): print('----POINT----------------------------------') print(type(p)) print(p) print(f'p[0] = {type(p[0])} {p[0]}') print(f'p[1] = {type(p[1])} {p[1]}') # ------------------------------------------------------------------- # ---- main # ------------------------------------------------------------------- if __name__ == '__main__': win_width = 801 win_height = 801 # ---- create window win = GraphWin("Intersection Point", win_width, win_height) win.setBackground("white") # ---- draw axes ax.draw_xy_axes(win,True) # ---- basic drawing test ##basic_drawing_test(win) # ---- example tests for t in tests: print() print(f'---- {t[2]} ---------------') draw_line_segment(win,t[0][0],t[0][1], t[0][2],t[0][3]) draw_line_segment(win,t[1][0],t[1][1], t[1][2],t[1][3]) if parallel_lines(t[0],t[1]): print('parallel lines') continue p1 = array([t[0][0],t[0][1]]) p2 = array([t[0][2],t[0][3]]) p3 = array([t[1][0],t[1][1]]) p4 = array([t[1][2],t[1][3]]) xy = intersection_point(p1,p2,p3,p4) print(f'intersection = {xy[0]},{xy[1]}') draw_point(win,xy[0],xy[1],'green') # ---- end program ui.pause() win.close() print()