python: Drawing Canvas

 

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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# encoding: utf-8
# 版权所有 2023 涂聚文有限公司
# 许可信息查看:
# 描述:
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : PyCharm 2023.1 python 311
# Datetime  : 2023/9/21 21:28
# User      : geovindu
# Product   : PyCharm
# Project   : EssentialAlgorithms
# File      : DrawingTwo.py
# explain   : 学习
 
import tkinter as tk
import tkinter.font as tk_font
import math
 
class DrawingCanvasTwo(object):
    """
    A canvas drawing manager.
 
    """
    def __init__(self, canvas, wxmin, wymin, wxmax, wymax, dmargin, y_is_flipped):
        """
 
        :param canvas:
        :param wxmin:
        :param wymin:
        :param wxmax:
        :param wymax:
        :param dmargin:
        :param y_is_flipped:
        """
        self.canvas = canvas
        self.wxmin = wxmin
        self.wymin = wymin
        self.wxmax = wxmax
        self.wymax = wymax
        self.dmargin = dmargin
        self.y_is_flipped = y_is_flipped
 
        self.set_scales()
 
    def set_scales(self):
        """
        Calculate scale parameters for the canvas's current size.
        :return:
        """
        self.canvas.update()
        self.dxmin = self.dmargin
        self.dymin = self.dmargin
        self.dxmax = self.canvas.winfo_width() - self.dmargin - 1
        self.dymax = self.canvas.winfo_height() - self.dmargin - 1
 
        # Flip the Y coordinates to invert the result.
        if self.y_is_flipped:
            self.dymin, self.dymax = self.dymax, self.dymin
 
        self.xscale = (self.dxmax - self.dxmin) / (self.wxmax - self.wxmin)
        self.yscale = (self.dymax - self.dymin) / (self.wymax - self.wymin)
 
        # Calculate 1 pixel in world coordinates.
        self.xpix = 1 / self.xscale
        self.ypix = 1 / self.yscale
 
    def w_to_d(self, wx, wy):
        """
        Map a point from world to device coordinates.
        :param wx:
        :param wy:
        :return:
        """
        dx = (wx - self.wxmin) * self.xscale + self.dxmin
        dy = (wy - self.wymin) * self.yscale + self.dymin
        return dx, dy
 
    def clear(self):
        """
 
        :return:
        """
 
        self.canvas.delete(tk.ALL)
 
    def wdraw_line(self, wx0, wy0, wx1, wy1, color, arrow):
        """
        Draw a line in world coordinates.
        :param wx0:
        :param wy0:
        :param wx1:
        :param wy1:
        :param color:
        :param arrow:
        :return:
        """
        dx0, dy0 = self.w_to_d(wx0, wy0)
        dx1, dy1 = self.w_to_d(wx1, wy1)
        self.canvas.create_line(dx0, dy0, dx1, dy1, fill=color, arrow=arrow)
 
    def wdraw_axes(self, xtic_spacing, ytic_spacing, tic_hgt, tic_wid, do_draw_text, color):
        """
        Draw coordinate axes.
        :param xtic_spacing:
        :param ytic_spacing:
        :param tic_hgt:
        :param tic_wid:
        :param do_draw_text:
        :param color:
        :return:
        """
        self.wdraw_line(self.wxmin, 0, self.wxmax, 0, color, arrow=tk.BOTH)
        self.wdraw_line(0, self.wymin, 0, self.wymax, color, arrow=tk.BOTH)
 
        startx = xtic_spacing * int((self.wxmin + xtic_spacing) / xtic_spacing)
        x = startx
        while x < self.wxmax:
            if (abs(x) > 0.01):
                dx0, dy0 = self.w_to_d(x, tic_hgt)
                dx1, dy1 = self.w_to_d(x, -tic_hgt)
                self.canvas.create_line(dx0, dy0, dx1, dy1, fill=color)
                if do_draw_text:
                    self.canvas.create_text(dx1, dy1, text=str(x), fill=color, anchor=tk.N)
            x += xtic_spacing
 
        starty = ytic_spacing * int((self.wymin + ytic_spacing) / ytic_spacing)
        y = starty
        while y < self.wymax:
            if (abs(y) > 0.01):
                dx0, dy0 = self.w_to_d(tic_wid, y)
                dx1, dy1 = self.w_to_d(-tic_wid, y)
                self.canvas.create_line(dx0, dy0, dx1, dy1, fill=color)
                if do_draw_text:
                    self.canvas.create_text(dx1, dy1, text=str(y), fill=color, anchor=tk.E)
            y += ytic_spacing
 
    def wdraw_polyline(self, wcoords, color):
        """
        Draw a connected series of points in world coordinates.
        :param wcoords:
        :param color:
        :return:
        """
 
        dpoints = []
        for i in range(0, len(wcoords), 2):
            dpoints += self.w_to_d(wcoords[i], wcoords[i+1])
        self.canvas.create_line(dpoints, fill=color)
 
    def wdraw_rotated_text(self, wx, wy, text, angle, color, font):
        """
        Draw a rotated text at the indicated position in world coordinates.
        :param wx:
        :param wy:
        :param text:
        :param angle:
        :param color:
        :param font:
        :return:
        """
        dx, dy = self.w_to_d(wx, wy)
        self.canvas.create_text(dx, dy, text=text, angle=angle, fill=color, font=font)
 
    def wdraw_function(self, func, color, wxmin, wxmax, step_x):
        """
        Draw a function.
        :param func:
        :param color:
        :param wxmin:
        :param wxmax:
        :param step_x:
        :return:
        """
        points = []
        x = wxmin
        while x <= wxmax:
            points.append(x)
            points.append(func(x))
            x += step_x
        self.wdraw_polyline(points, color)
 
 
def log_x(x):
    """
 
    :param x:
    :return:
    """
    return math.log(x, 2)
def sqrt_x(x):
    """
 
    :param x:
    :return:
    """
    return 1.5 * math.sqrt(x)
def identity_x(x):
    """
 
    :param x:
    :return:
    """
    return x
def x2(x):
    """
 
    :param x:
    :return:
    """
    return x * x / 5
def two_to_the_x(x):
    """
 
    :param x:
    :return:
    """
    return math.pow(2, x) / 10
def factorial_n(n):
    """
 
    :param n:
    :return:
    """
    result = 1
    for i in range(2, n + 1):
        result *= i
    return result / 100
def fibonacci_n(n):
    """
 
    :param n:
    :return:
    """
    if n == 0:
        return 0
    fib_minus2 = 0
    fib_minus1 = 1
    fib = 1
    for i in range(2, n + 1):
        fib = fib_minus1 + fib_minus2
        fib_minus2 = fib_minus1
        fib_minus1 = fib
    return fib / 10

  

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
# encoding: utf-8
# 版权所有 2023 涂聚文有限公司
# 许可信息查看:
# 描述:
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : PyCharm 2023.1 python 311
# Datetime  : 2023/9/21 21:29
# User      : geovindu
# Product   : PyCharm
# Project   : EssentialAlgorithms
# File      : Chapter03.py
# explain   : 学习
 
import tkinter as tk
import tkinter.font as tk_font
import math
import ChapterOne.DrawingTwo
 
class Ch03App(object):
    """
 
    """
    def __init__(self):
        """
 
        """
        self.window = tk.Tk()
        self.window.title("runtime_functions")
        self.window.protocol("WM_DELETE_WINDOW", self.kill_callback)
        self.window.geometry("570x570")
 
        # Make a slightly bigger label font.
        self.label_font = tk_font.Font(family="Times New Roman", size=14)
 
        # Canvas.
        self.canvas = tk.Canvas(self.window, width=550, height=550,
            relief=tk.RIDGE, bd=5, highlightthickness=0, bg="white")
        self.canvas.xview("moveto", 5)   # Move out from the border.
        self.canvas.yview("moveto", 5)
        self.canvas.grid(row=1, column=0, columnspan=4, padx=5, pady=5)
 
        # Make the DrawingCanvas.
        self.drawing_canvas = ChapterOne.DrawingTwo.DrawingCanvasTwo(self.canvas, -1, -1, 21, 21, 20, True)
 
        # Draw the scene.
        self.draw_scene()
 
        # Force focus so Alt+F4 closes this window and not the Python shell.
        self.window.focus_force()
        self.window.mainloop()
 
    def kill_callback(self):
        """
 
        :return:
        """
 
        self.window.destroy()
 
    def draw_scene(self):
        """
        Draw the scene."
        :return:
        """
        self.drawing_canvas.clear()
 
        # Draw the curves.
        wxmin = self.drawing_canvas.wxmin
        wxmax = self.drawing_canvas.wxmax
        xpix = self.drawing_canvas.xpix
        self.drawing_canvas.wdraw_function(ChapterOne.DrawingTwo.log_x, "blue", 0.5, wxmax, xpix)
        self.drawing_canvas.wdraw_function(ChapterOne.DrawingTwo.sqrt_x, "green", 0, wxmax, xpix)
        self.drawing_canvas.wdraw_function(ChapterOne.DrawingTwo.identity_x, "black", 0, wxmax, xpix)
        self.drawing_canvas.wdraw_function(ChapterOne.DrawingTwo.x2, "orange", 0, wxmax, xpix)
        self.drawing_canvas.wdraw_function(ChapterOne.DrawingTwo.two_to_the_x, "magenta", 0, wxmax, xpix)
        self.drawing_canvas.wdraw_function(ChapterOne.DrawingTwo.fibonacci_n, "blue", 0, 20, 1)
        self.drawing_canvas.wdraw_function(ChapterOne.DrawingTwo.factorial_n, "red", 0, 10, 1)
 
        self.drawing_canvas.wdraw_rotated_text(15, 4.5, "y = Log(x)", 6, "blue", self.label_font)
        self.drawing_canvas.wdraw_rotated_text(15, 6.5, "y = 1.5 * Sqrt(x)", 11, "green", self.label_font)
        self.drawing_canvas.wdraw_rotated_text(13, 14, "y = x", 45, "black", self.label_font)
        self.drawing_canvas.wdraw_rotated_text(8.75, 17, "y = x^2 / 5", 75, "orange", self.label_font)
        self.drawing_canvas.wdraw_rotated_text(7, 17.5, "y = 2x / 10", 85, "magenta", self.label_font)
        self.drawing_canvas.wdraw_rotated_text(5.5, 18, "y = x! / 100", 88, "red", self.label_font)
        self.drawing_canvas.wdraw_rotated_text(11.5, 16, "y = Fibonacci(x) / 10", 83, "blue", self.label_font)
 
        # Draw the axes.
        self.drawing_canvas.wdraw_axes(5, 5, 0.2, 0.2, True, "gray")

  

调用:

1
BLL.ChapterIExample.ChapExample.CharIII()

  

输出:

 

posted @   ®Geovin Du Dream Park™  阅读(9)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2022-09-29 CSharp: Iterator Pattern
2011-09-29 csharp datagridview to a datatable,a dataset
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
点击右上角即可分享
微信分享提示