# ulvis.paste.net

Paste Search Dynamic
Recent pastes
Wykresy
1. # import necessary modules
2. from tkinter import *
3. import sys
4.
5.
6.
7.
8.
9.
10. xtab=[]
11. ytab=[]
12.
13.
14. # title and description of the program
15. title = "\
16. Wykresy v. 0.01, 10/7/2018. Copyright (C) by Pawel Pilarczyk.\n\
17. This is free software. No warranty. See GNU GPL for details.\n";
18. helpinfo = "\
19. This is a program that plots a graph of a polynomial with varying parameters.\n\
20. Call with: n a_n^min a_n^max ... a_0^min a_0^max x_min x_max y_min y_max\n\
21. where n is the degree of the polynomial,\n\
22. a_n ... a_0 are the coefficients,\n\
23. ";
24.
25. # show the title of the program
26. print(title)
27.
28. # =====================================================
29. # interpret the command line arguments
30. # =====================================================
31.
32. # show help info if the program is called without any arguments
33. # or if it is called with the "--help" argument
34. if (len(sys.argv) <= 1) or (sys.argv[1] == "--help"):
35.     print(helpinfo)
36.     exit(1)
37.
38. # show the command-line arguments
39. print("There are %d command-line arguments:" % len(sys.argv), end=" ")
40. print(sys.argv);
41.
42. # determine the degree of the polynomial
43. degree = int(sys.argv[1])
44. print("The degree of the polynomial is", degree);
45.
46. ile=len(sys.argv)-1
47. print("ile"+str(ile))
48. # make sure that the degree is in the reasonable scope
49. if (degree < 0) or (degree > 10):
50.     print("ERROR: Degree n=%d out of acceptable range [0,10].\n" % int(degree))
51.     exit(1)
52.
53. # make sure that there are enough command line arguments:
54. # script name, degree, n+1 coefficients (min and max), x_min, x_max, y_min, y_max
55. if (len(sys.argv) < 2 + 2 * (degree + 1) + 4):
56.     print("ERROR: Not enough numbers provided in the command line.\n");
57.     exit(1);
58.
59. # create arrays for the ranges of coefficients
60. a_min = []
61. a_max = []
62. # for i in range(0, degree + 1):
63. #     offset = 2 + 2 * (degree - i)
64. #
65. #     #a_min.append(float(sys.argv[offset]))
66. #     #a_max.append(float(sys.argv[offset + 1]))
67. #     print("offset="+str(sys.argv[offset]))
68. #     print("offset="+str(sys.argv[offset + 1]))
69. #     xtab.append(float(sys.argv[offset]))
70. #     ytab.append(float(sys.argv[offset + 1]))
71. #for i in range(1, ile + 1):
72.     #print(i)
73.     # print(offset)
74.     #a_min.append(float(sys.argv[offset]))
75.     #a_max.append(float(sys.argv[offset + 1]))
76.     # print("offset="+str(sys.argv[offset]))
77.     # print("offset="+str(sys.argv[offset + 1]))
78. i=1
79. while(i<(ile-4)):
80.     print(str(sys.argv[i])+"="+str(sys.argv[i + 1]))
81.     xtab.append(float(sys.argv[i]))
82.     ytab.append(float(sys.argv[i + 1]))
83.     i=i+2
84.
85. # xtab = [1, 4, 9]
86. # ytab = [1, 2, 3]
87. print("Minimum coefficients:", xtab)
88. print("Maximum coefficients:", ytab)
89.
90.
91.
92.
93.
94. # determine the ranges of coordinates
95. #range_offset = 2 + 2 * (degree + 1)
96. x_min = float(sys.argv[ile-3])
97. x_max = float(sys.argv[ile-2])
98. y_min = float(sys.argv[ile-1])
99. y_max = float(sys.argv[ile])
100. print("X coordinates range: [" + str(x_min) + ", " + str(x_max) + "]")
101. print("Y coordinates range: [%.3f, %.3f]" % (y_min, y_max))
102. if (x_min >= x_max):
103.     print("ERROR: Wrong X coordinates range.")
104.     exit(1)
105. if (y_min >= y_max):
106.     print("ERROR: Wrong Y coordinates range.")
107.     exit(1)
108. x_width = x_max - x_min
109. y_width = y_max - y_min
110.
111. # =====================================================
112. # prepare canvas size and plotting functions
113. # =====================================================
114.
115. # set the width and size of the canvas (in pixels)
116. canvas_width = 800
117. canvas_height = 600
118.
119.
120. # define functions that convert from real to screen coordinates
121. def x_to_scr(x):
122.     result = ((x - x_min) / x_width) * canvas_width
123.     return int(round(result))
124.
125.
126. def y_to_scr(y):
127.     result = ((y - y_min) / y_width) * canvas_height
128.     return canvas_height - 1 - int(round(result))
129.
130.
131. # plots coordinate axes
132. def plot_axes(canvas):
133.     y = y_to_scr(0)
134.     color = "#000000"
135.     canvas.create_line(0, y, canvas_width, y, fill=color, width=1)
136.     canvas.create_line(canvas_width - 20, y - 10, canvas_width, y, fill=color, width=1)
137.     canvas.create_line(canvas_width - 20, y + 10, canvas_width, y, fill=color, width=1)
138.     canvas.create_text(canvas_width - 10, y + 20, text="X")
139.     x = x_to_scr(0)
140.     canvas.create_line(x, 0, x, canvas_height, fill=color, width=1)
141.     canvas.create_line(x - 10, 20, x, 0, fill=color, width=1)
142.     canvas.create_line(x + 10, 20, x, 0, fill=color, width=1)
143.     canvas.create_text(x - 15, 12, text="Y")
144.
145.
146. # plots a line with the given coordinates;
147. # returns a reference to the object
148. def plot_line(canvas, x0, y0, x1, y1, color, width, tag):
149.     x0scr = x_to_scr(x0)
150.     y0scr = y_to_scr(y0)
151.     x1scr = x_to_scr(x1)
152.     y1scr = y_to_scr(y1)
153.
154.     return canvas.create_line(x0scr, y0scr, x1scr, y1scr,
155.                              fill=color, width=width, tag=tag)
156.
157.
158.
159. # =====================================================
160. # plotting the animation
161. # =====================================================
162.
163. # prepare the current values of the parameters
164. a_current = a_min[:]
165.
166. # define the number of intervals from minimum to maximum
167. number_of_intervals = 10
168.
169. # define the current position: from 0 (min) to the number of intervals (max)
170. current_position = 0
171.
172. # plots the graph at the current values of the parameters;
173. # adds all the plotted objects to the list named "plotted" above;
174. # here plotting of a polynomial or another function should be included,
175. # now the program plots "anything" that changes when the parameters change
176.
177.
178.
179.
180.
181.
182. #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
183. # tutaj jest dodana nowa funkcja bierze te wartosci z te
184. def hmm(listaX,listaY,wartosc):
185.     wyniki=[]
186.     for i in range(0, len(listaX)):
187.         #wyniki[i] = ytab[i]
188.         wyniki.append(ytab[i])
189.         for k in range(0, len(listaX)):
190.             if xtab[i] != xtab[k]:
191.                 wyniki[i] *= (1.000 * wartosc - xtab[k])
192.         for k in range(0, 3):
193.             if xtab[i] != xtab[k]:
194.                 wyniki[i] /= (1.000 * xtab[i] - xtab[k])
195.     suma = float(0)
196.     for i in range(0, 3):
197.         suma += (wyniki[i] * 1.000)
198.     return suma
199.
200.
201.
202.
203.
204. def plot_graph(canvas):
205.     # line_object = plot_line(canvas,
206.     #                          1, 1,
207.     #                         7, 7,
208.     #                         "#FF0000", 2, tag='graph')
209.     # line_object = plot_line(canvas,
210.     #                             0.7 * x_min + 0.3 * x_max, a_current[0],
211.     #                             0.3 * x_min + 0.7 * x_max, a_current[1],
212.     #                             color="#007F00", width=2, tag='graph')
213.     # line_object = plot_line(canvas,
214.     #                             0.3 * x_min + 0.7 * x_max, a_current[1],
215.     #                             x_max, y_max,
216.     #                             width=2, color="#0000FF", tag='graph')
217.
218.     # for i in range(x_min, x):
219.     #     print(str(xtab[i])+"="+str(ytab[i]))
220.     #
221.     #     if i+1 != 4:
222.     #         line_object = plot_line(canvas,
223.     #                             xtab[i], ytab[i],
224.                 #                                                       xtab[i+1], ytab[i+1],
225.     #                             "#FF0000", 2, tag='graph')
226.     xtest=[]
227.     ytest=[]
228.     for i in range(int(x_min),int(x_max)+1):
229.         #print(i)
230.         xtest.append(i)
231.         ytest.append(hmm(xtab,ytab,i))
232.         #print(hmm(xtab,ytab,i))
233.     max = len(xtest)
234.
235.     for i in range(max):
236.         #print(xtest[i])
237.
238.         if (i+1)!=max:
239.             print(str(xtest[i])+"="+str(ytest[i]))
240.             line_object = plot_line(canvas,
241.                                     xtest[i], ytest[i],
242.                                                                 xtest[i+1], ytest[i+1],
243.                                     "#FF0000", 2, tag='graph')
244.     print(max)
245.     print(x_max)
246.     print(hmm(xtab,ytab,x_max))
247.
248.
249.
250. # advances the animation to the next/previous position and updates the canvas
252.     # update the current position
253.     global current_position
254.     current_position += direction
255.     if (current_position > number_of_intervals):
256.         current_position = number_of_intervals
257.     elif (current_position < 0):
258.         current_position = 0
259.
260.     # update the current value of the parameters
261.     global a_current
262.     if (current_position <= 0):
263.         a_current = a_min[:]
264.     elif (current_position >= number_of_intervals):
265.         a_current = a_max[:]
266.     else:
267.         w_min = float(number_of_intervals - current_position) / float(number_of_intervals)
268.         w_max = float(current_position) / float(number_of_intervals)
269.         for i in range(0, len(a_current)):
270.             a_current[i] = w_min * a_min[i] + w_max * a_max[i]
271.
272.     # delete the previously drawn objects
273.     canvas.delete('graph')
274.
275.     # plot the new graph
276.     plot_graph(canvas)
277.
278.
279. # =====================================================
280. # create the initial plotting
281. # =====================================================
282.
283. # open a window
284. window = Tk()
285.
286. # create a canvas for plotting and adjust the size of the window to fit the canvas
287. canvas = Canvas(window, width=canvas_width, height=canvas_height)
288. canvas.pack()
289.
290. # plot the picture
291. plot_axes(canvas)
292. plot_graph(canvas)
293.
294. # show the title
295. canvas.create_text(1, 1, font="Times 12 italic", anchor="nw",
296.                    text="Przyklad programu do rysowania wykresow.\n" +
297.                         "Copyright (C) 2018 by Pawel Pilarczyk.")
298.
299.
300. # =====================================================
301. # serve events and update the graphics animation
302. # =====================================================
303.
304. # shows the symbol of a key that was pressed
305. def key_pressed(event):
306.     print("Key pressed: " + repr(event.char))
307.
308.
309. # advances the animation to the next step
310. def right_pressed(event):
311.     print("Right pressed.")
313.
314.
315. # switches the animation to the previous step
316. def left_pressed(event):
317.     print("Left pressed.")
319.
320.
321. # rewinds the animation to the beginning
322. def home_pressed(event):
323.     print("Home pressed.")
325.
326.
327. # forwards the animation to the end
328. def end_pressed(event):
329.     print("End pressed.")
331.
332.
333. # leaves the program
334. def esc_pressed(event):
335.     print("ESC pressed. Exiting.")
336.     exit(0)
337.
338.
339. # define which events should be served by which functions
340. canvas.bind("<KeyPress>", key_pressed)
341. canvas.bind("<Escape>", esc_pressed)
342. canvas.bind("<Right>", right_pressed)
343. canvas.bind("<Left>", left_pressed)
344. canvas.bind("<Home>", home_pressed)
345. canvas.bind("<End>", end_pressed)
346. canvas.focus_set()
347.
348. # process the events in a loop
349. mainloop()
Parsed in 0.069 seconds