# import necessary modules
from tkinter import *
import sys
xtab=[]
ytab=[]
# title and description of the program
title = "\
Wykresy v. 0.01, 10/7/2018. Copyright (C) by Pawel Pilarczyk.\n\
This is free software. No warranty. See GNU GPL for details.\n";
helpinfo = "\
This is a program that plots a graph of a polynomial with varying parameters.\n\
Call with: n a_n^min a_n^max ... a_0^min a_0^max x_min x_max y_min y_max\n\
where n is the degree of the polynomial,\n\
a_n ... a_0 are the coefficients,\n\
";
# show the title of the program
print(title)
# =====================================================
# interpret the command line arguments
# =====================================================
# show help info if the program is called without any arguments
# or if it is called with the "--help" argument
if (len(sys.argv) <= 1) or (sys.argv[1] == "--help"):
print(helpinfo)
exit(1)
# show the command-line arguments
print("There are %d command-line arguments:" % len(sys.argv), end=" ")
print(sys.argv);
# determine the degree of the polynomial
degree = int(sys.argv[1])
print("The degree of the polynomial is", degree);
ile=len(sys.argv)-1
print("ile"+str(ile))
# make sure that the degree is in the reasonable scope
if (degree < 0) or (degree > 10):
print("ERROR: Degree n=%d out of acceptable range [0,10].\n" % int(degree))
exit(1)
# make sure that there are enough command line arguments:
# script name, degree, n+1 coefficients (min and max), x_min, x_max, y_min, y_max
if (len(sys.argv) < 2 + 2 * (degree + 1) + 4):
print("ERROR: Not enough numbers provided in the command line.\n");
exit(1);
# create arrays for the ranges of coefficients
a_min = []
a_max = []
# for i in range(0, degree + 1):
# offset = 2 + 2 * (degree - i)
#
# #a_min.append(float(sys.argv[offset]))
# #a_max.append(float(sys.argv[offset + 1]))
# print("offset="+str(sys.argv[offset]))
# print("offset="+str(sys.argv[offset + 1]))
# xtab.append(float(sys.argv[offset]))
# ytab.append(float(sys.argv[offset + 1]))
#for i in range(1, ile + 1):
#print(i)
# print(offset)
#a_min.append(float(sys.argv[offset]))
#a_max.append(float(sys.argv[offset + 1]))
# print("offset="+str(sys.argv[offset]))
# print("offset="+str(sys.argv[offset + 1]))
i=1
while(i<(ile-4)):
print(str(sys.argv[i])+"="+str(sys.argv[i + 1]))
xtab.append(float(sys.argv[i]))
ytab.append(float(sys.argv[i + 1]))
i=i+2
# xtab = [1, 4, 9]
# ytab = [1, 2, 3]
print("Minimum coefficients:", xtab)
print("Maximum coefficients:", ytab)
# determine the ranges of coordinates
#range_offset = 2 + 2 * (degree + 1)
x_min = float(sys.argv[ile-3])
x_max = float(sys.argv[ile-2])
y_min = float(sys.argv[ile-1])
y_max = float(sys.argv[ile])
print("X coordinates range: [" + str(x_min) + ", " + str(x_max) + "]")
print("Y coordinates range: [%.3f, %.3f]" % (y_min, y_max))
if (x_min >= x_max):
print("ERROR: Wrong X coordinates range.")
exit(1)
if (y_min >= y_max):
print("ERROR: Wrong Y coordinates range.")
exit(1)
x_width = x_max - x_min
y_width = y_max - y_min
# =====================================================
# prepare canvas size and plotting functions
# =====================================================
# set the width and size of the canvas (in pixels)
canvas_width = 800
canvas_height = 600
# define functions that convert from real to screen coordinates
def x_to_scr(x):
result = ((x - x_min) / x_width) * canvas_width
return int(round(result))
def y_to_scr(y):
result = ((y - y_min) / y_width) * canvas_height
return canvas_height - 1 - int(round(result))
# plots coordinate axes
def plot_axes(canvas):
y = y_to_scr(0)
color = "#000000"
canvas.create_line(0, y, canvas_width, y, fill=color, width=1)
canvas.create_line(canvas_width - 20, y - 10, canvas_width, y, fill=color, width=1)
canvas.create_line(canvas_width - 20, y + 10, canvas_width, y, fill=color, width=1)
canvas.create_text(canvas_width - 10, y + 20, text="X")
x = x_to_scr(0)
canvas.create_line(x, 0, x, canvas_height, fill=color, width=1)
canvas.create_line(x - 10, 20, x, 0, fill=color, width=1)
canvas.create_line(x + 10, 20, x, 0, fill=color, width=1)
canvas.create_text(x - 15, 12, text="Y")
# plots a line with the given coordinates;
# returns a reference to the object
def plot_line(canvas, x0, y0, x1, y1, color, width, tag):
x0scr = x_to_scr(x0)
y0scr = y_to_scr(y0)
x1scr = x_to_scr(x1)
y1scr = y_to_scr(y1)
return canvas.create_line(x0scr, y0scr, x1scr, y1scr,
fill=color, width=width, tag=tag)
# =====================================================
# plotting the animation
# =====================================================
# prepare the current values of the parameters
a_current = a_min[:]
# define the number of intervals from minimum to maximum
number_of_intervals = 10
# define the current position: from 0 (min) to the number of intervals (max)
current_position = 0
# plots the graph at the current values of the parameters;
# adds all the plotted objects to the list named "plotted" above;
# here plotting of a polynomial or another function should be included,
# now the program plots "anything" that changes when the parameters change
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
# tutaj jest dodana nowa funkcja bierze te wartosci z te
def hmm(listaX,listaY,wartosc):
wyniki=[]
for i in range(0, len(listaX)):
#wyniki[i] = ytab[i]
wyniki.append(ytab[i])
for k in range(0, len(listaX)):
if xtab[i] != xtab[k]:
wyniki[i] *= (1.000 * wartosc - xtab[k])
for k in range(0, 3):
if xtab[i] != xtab[k]:
wyniki[i] /= (1.000 * xtab[i] - xtab[k])
suma = float(0)
for i in range(0, 3):
suma += (wyniki[i] * 1.000)
return suma
def plot_graph(canvas):
# line_object = plot_line(canvas,
# 1, 1,
# 7, 7,
# "#FF0000", 2, tag='graph')
# line_object = plot_line(canvas,
# 0.7 * x_min + 0.3 * x_max, a_current[0],
# 0.3 * x_min + 0.7 * x_max, a_current[1],
# color="#007F00", width=2, tag='graph')
# line_object = plot_line(canvas,
# 0.3 * x_min + 0.7 * x_max, a_current[1],
# x_max, y_max,
# width=2, color="#0000FF", tag='graph')
# for i in range(x_min, x):
# print(str(xtab[i])+"="+str(ytab[i]))
#
# if i+1 != 4:
# line_object = plot_line(canvas,
# xtab[i], ytab[i],
# xtab[i+1], ytab[i+1],
# "#FF0000", 2, tag='graph')
xtest=[]
ytest=[]
for i in range(int(x_min),int(x_max)+1):
#print(i)
xtest.append(i)
ytest.append(hmm(xtab,ytab,i))
#print(hmm(xtab,ytab,i))
max = len(xtest)
for i in range(max):
#print(xtest[i])
if (i+1)!=max:
print(str(xtest[i])+"="+str(ytest[i]))
line_object = plot_line(canvas,
xtest[i], ytest[i],
xtest[i+1], ytest[i+1],
"#FF0000", 2, tag='graph')
print(max)
print(x_max)
print(hmm(xtab,ytab,x_max))
# advances the animation to the next/previous position and updates the canvas
def anim_advance(canvas, direction):
# update the current position
global current_position
current_position += direction
if (current_position > number_of_intervals):
current_position = number_of_intervals
elif (current_position < 0):
current_position = 0
# update the current value of the parameters
global a_current
if (current_position <= 0):
a_current = a_min[:]
elif (current_position >= number_of_intervals):
a_current = a_max[:]
else:
w_min = float(number_of_intervals - current_position) / float(number_of_intervals)
w_max = float(current_position) / float(number_of_intervals)
for i in range(0, len(a_current)):
a_current[i] = w_min * a_min[i] + w_max * a_max[i]
# delete the previously drawn objects
canvas.delete('graph')
# plot the new graph
plot_graph(canvas)
# =====================================================
# create the initial plotting
# =====================================================
# open a window
window = Tk()
# create a canvas for plotting and adjust the size of the window to fit the canvas
canvas = Canvas(window, width=canvas_width, height=canvas_height)
canvas.pack()
# plot the picture
plot_axes(canvas)
plot_graph(canvas)
# show the title
canvas.create_text(1, 1, font="Times 12 italic", anchor="nw",
text="Przyklad programu do rysowania wykresow.\n" +
"Copyright (C) 2018 by Pawel Pilarczyk.")
# =====================================================
# serve events and update the graphics animation
# =====================================================
# shows the symbol of a key that was pressed
def key_pressed(event):
print("Key pressed: " + repr(event.char))
# advances the animation to the next step
def right_pressed(event):
print("Right pressed.")
anim_advance(canvas, 1)
# switches the animation to the previous step
def left_pressed(event):
print("Left pressed.")
anim_advance(canvas, -1)
# rewinds the animation to the beginning
def home_pressed(event):
print("Home pressed.")
anim_advance(canvas, -number_of_intervals)
# forwards the animation to the end
def end_pressed(event):
print("End pressed.")
anim_advance(canvas, number_of_intervals)
# leaves the program
def esc_pressed(event):
print("ESC pressed. Exiting.")
exit(0)
# define which events should be served by which functions
canvas.bind("<KeyPress>", key_pressed)
canvas.bind("<Escape>", esc_pressed)
canvas.bind("<Right>", right_pressed)
canvas.bind("<Left>", left_pressed)
canvas.bind("<Home>", home_pressed)
canvas.bind("<End>", end_pressed)
canvas.focus_set()
# process the events in a loop
mainloop()