-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mapping_for_Tkinter
89 lines (65 loc) · 2.68 KB
/
Mapping_for_Tkinter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# Justin Diep
# 4-9-19
from tkinter import *
class Mapping_for_Tkinter:
#Class methods
def __init__(self,xmin,xmax,ymin,ymax,width):
self.__xmin = xmin
self.__xmax = xmax
self.__ymin = ymin
self.__ymax = ymax
self.__width = width
self.__height = int(self.__width * ((self.__ymax - self.__ymin) / (self.__xmax - self.__xmin)))
#Setter methods
def set_xmin(self, xmin):
self.__xmin = xmin
def set_xmax(self, xmax):
self.__xmax = xmax
def set_ymin(self, ymin):
self.__ymin = ymin
def set_ymax(self, ymax):
self.__ymax = ymax
#Getter methods
def get_xmax(self):
return self.__xmax
def get_xmin(self):
return self.__xmin
def get_ymin(self):
return self.__ymin
def get_ymax(self):
return self.__ymax
def get_width(self):
return self.__width
def get_height(self):
return self.__height
def get_x(self,i):
return self.get_xmin()+(i*(self.get_xmax()-self.get_xmin())/self.get_width())
def get_y(self,j):
return self.get_ymax()-(j*(self.get_ymax-self.get_ymin())/self.get_height)
def get_i(self, x):
return (self.get_width())/(self.get_xmax()-self.get_xmin())*(x-self.get_xmin())
def get_j(self, y):
return (self.get_height())/(self.get_ymax()-self.get_ymin())*(self.get_ymax()-y)
#String
def __str__(self):
return "Mapping created between x=[" + str(self.__xmin) + "," + str(self.__xmax) + "] y=[" + str(self.__ymin) + "," + str(self.__ymax) + "] math => (" + str(self.__width) + "," + str(int(self.__height)) + ") tkinter"
def main():
m=Mapping_for_Tkinter(-5.0,5.0,-5.0,5.0,500) # instantiate mapping
print(m) # print info about object
window = Tk() # instantiate a tkinter window
canvas = Canvas(window, width=m.get_width(),height=m.get_height(),bg="white") # create a canvas width*height
canvas.pack()
# create rectangle the Tkinter way
print("Draw rectangle using tkinter coordinates at (100,400) (400,100)")
canvas.create_rectangle(100,400,400,100,outline="black")
# create circle using the mapping
print("Draw circle using math coordinates at center (0,0) with radius 3")
canvas.create_oval(m.get_i(-3.0),m.get_j(-3.0),m.get_i(3.0),m.get_j(3.0),outline="blue")
# create y=x line pixel by pixel using the mapping
print("Draw line math equation y=x pixel by pixel using the mapping")
for i in range(m.get_width()):
x=m.get_x(i) # obtain the x coordinate
y=x
canvas.create_rectangle((m.get_i(x),m.get_j(y))*2,outline="green")
window.mainloop() # wait until the window is closed
#main()