![]() |
|||
| HSG |
|
Ein Grundproblem der Grafik-Programmierung ist die Transformation sogenannter Welt-Koordinaten (x/y) auf Bildschirm-Koordinaten (u/v) . Im Folgenden soll anhand eines Beispiels die Entwicklung der Transformation gezeigt werden:
u = a*x + b
v = c*y + d
gesucht: Koeffizienten a, b, c, d
|
|
III - I:
umax - 0 = a(xmax - xmin) => a = umax/(xmax-xmin)
a in I:
b = - a*xmin
IV - II:
vmax - 0 = c(ymin - ymax) => c = vmax/(ymin-ymax)
c in II:
d = - c*ymax
def berechneKoeffizienten(xmin,xmax,ymin,ymax,umax,vmax):
global a_g,b_g,c_g,d_g
a_g = umax/float(xmax-xmin)
b_g = -a_g*xmin
c_g = vmax/float(ymin-ymax)
d_g = -c_g*ymax
def ut(x):
return a_g*x+b_g
def vt(y):
return c_g*y+d_g
def punkt(x,y):
c.create_rectangle(ut(x),vt(y),ut(x),vt(y))
def f(x):
return 0.2*x*x-0.5
def zeichneAchsen(event):
xAchse = c.create_line(ut(-2),vt(0),ut(4),vt(0))
yAchse = c.create_line(ut(0),vt(-1),ut(0),vt(3))
def zeichneGraph(event):
x=-1
while x < 3 :
y = f(x)
punkt(x,y)
x=x+0.01
from Tkinter import *
root = Tk()
root.title('Welt-Bild-Demo 1')
root.geometry('800x600')
c = Canvas(master=root,width=600,height=400,bg='white')
c.place(x=10,y=10)
bAchsen = Button(master=root,text='zeichne Achsen',width=15)
bAchsen.place(x=10,y=430)
bAchsen.bind('<Button-1>',zeichneAchsen)
bGraph = Button(master=root,text='zeichne Graph',width=15)
bGraph.place(x=10,y=470)
bGraph.bind('<Button-1>',zeichneGraph)
berechneKoeffizienten(-2,4,-1,3,600,400)
root.mainloop()

Erweitere das Programm um