Advertisement
here2share

# Tk_double_curve.py

Oct 8th, 2019
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.23 KB | None | 0 0
  1. # Tk_double_curve.py
  2.  
  3. from Tkinter import *
  4.  
  5. root = Tk()
  6. canvas = Canvas(root, height=600, width=600)
  7. canvas.pack()
  8.  
  9. def create_double_curve(canvas_root, x1, y1, x2, y2):
  10.     """Draw a curved line from A(x1, y1) to B(x2, y2)
  11.    :param canvas_root: the tkinter canvas on which you would like to draw a double-curve
  12.    :param x1: A's horizontal coordinate
  13.    :param y1: A's vertical coordinate
  14.    :param x2: B's horizontal coordinate
  15.    :param y2: B's vertical coordinate
  16.    """
  17.     if y1 == y2:
  18.         canvas_root.create_line(x1, y1, x2, y2)
  19.         return
  20.     if x1 < x2:
  21.         mid_x = (x1+x2)/2; mid_y = (y1+y2)/2
  22.         if y1 > mid_y:
  23.             if x1 > mid_x:
  24.                 bbox_x1 = mid_x; bbox_y1 = 2*mid_y - y1; bbox_x2 = 2*x1 - mid_x; bbox_y2 = y1; start_ang = 180
  25.             else:
  26.                 bbox_x1 = 2*x1 - mid_x; bbox_y1 = 2*mid_y - y1; bbox_x2 = mid_x; bbox_y2 = y1; start_ang = 270
  27.         else:
  28.             if x1 > mid_x:
  29.                 bbox_x1 = mid_x; bbox_y1 = y1; bbox_x2 = 2*x1 - mid_x; bbox_y2 = 2*mid_y - y1; start_ang = 90
  30.             else:
  31.                 bbox_x1 = 2*x1 - mid_x; bbox_y1 = y1; bbox_x2 = mid_x; bbox_y2 = 2*mid_y - y1; start_ang = 0
  32.         canvas_root.create_arc(bbox_x1, bbox_y1, bbox_x2, bbox_y2, start=start_ang, extent=90,
  33.                                                      style=ARC)
  34.         if y2 > mid_y:
  35.             if x2 > mid_x:
  36.                 bbox_x1 = mid_x; bbox_y1 = 2*mid_y - y2; bbox_x2 = 2*x2 - mid_x; bbox_y2 = y2; start_ang = 180
  37.             else:
  38.                 bbox_x1 = x2 - (mid_x-x1); bbox_y1 = 2*mid_y - y2; bbox_x2 = mid_x; bbox_y2 = y2; start_ang = 270
  39.         else:
  40.             if x2 > mid_x:
  41.                 bbox_x1 = mid_x; bbox_y1 = y2; bbox_x2 = 2*x2 - mid_x; bbox_y2 = 2*mid_y - y2; start_ang = 90
  42.             else:
  43.                 bbox_x1 = 2*x2 - mid_x; bbox_y1 = y2; bbox_x2 = mid_x; bbox_y2 = 2*mid_y - y2; start_ang = 0
  44.         canvas_root.create_arc(bbox_x1, bbox_y1, bbox_x2, bbox_y2, start=start_ang, extent=90,
  45.                                                      style=ARC)
  46.         return
  47.     else:
  48.         create_double_curve(canvas_root, x2, y2, x1, y1)
  49. 0
  50. create_double_curve(canvas, 100, 100, 500, 500)
  51.  
  52. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement