#!/usr/bin/python from Tkinter import * import math import random # Draw a globe... def AltAzToXYZ(alt, az): altrad= (3.14159/180.0)* -alt azrad= (3.14159/180.0)* -az x= math.sin(azrad)* abs(math.cos(altrad)) y= math.sin(altrad) z= math.cos(azrad)* abs(math.cos(altrad)) #print '(%i, %i)-> (%i, %i, %i)' % (alt, az, x, y, z) return [x,y,z] def MultByRad(xyz, rad): xyz[0]= xyz[0]* rad xyz[1]= xyz[1]* rad xyz[2]= xyz[2]* rad rotbyd= 15 rotbyr= rotbyd* 3.14159/180 cosine= math.cos(rotbyr); sine= math.sin(rotbyr) def Rotate1(pt): x= pt[0]; y= pt[1] x2= (x*cosine) + (y*sine) y2= -(x*sine) + (y*cosine) pt[0]= x2; pt[1]= y2 def Rotate2(pt): x= pt[1]; y= pt[2] x2= (x*cosine) + (y*sine) y2= -(x*sine) + (y*cosine) pt[1]= x2; pt[2]= y2 rad= 150.0 #----------------------------------------------------------------------------- class App: def __init__(self, master): frame = Frame(master); frame.pack() self.canvas = Canvas(frame, width=400, height=400) self.canvas.pack(side=TOP) fr2= Frame(frame); fr2.pack(side=BOTTOM) bm = Button(fr2, text="Rotate1", command=self.rotate1); bm.pack(side=LEFT) bm2 = Button(fr2, text="Rotate2", command=self.rotate2); bm2.pack(side=RIGHT) self.lines= [] self.draw() def add_parallel(self, alt, color): az= 0 p0= AltAzToXYZ(alt, az) MultByRad(p0, rad) while az< 360: az= az+ 10 p1= AltAzToXYZ(alt, az) MultByRad(p1, rad) self.add_line([p0, p1, color]) p0= p1[:] def add_meridian(self, az, color): alt= -90 p0= AltAzToXYZ(alt, az) MultByRad(p0, rad) while alt<= 90: alt= alt+ 10 p1= AltAzToXYZ(alt, az) MultByRad(p1, rad) self.add_line([p0, p1, color]) p0= p1[:] def draw(self): alt= -80 while alt< 70: self.add_parallel(alt, "blue") alt= alt+ 10 self.add_parallel(70, "red") self.add_parallel(80, "red") az= 20 while az < 360: self.add_meridian(az, "blue") az= az+ 10 self.add_meridian(0, "red") self.add_meridian(10, "green") def rotate1(self): for lineentry in self.lines: Rotate1(lineentry[1][0]) Rotate1(lineentry[1][1]) self.canvas.delete(lineentry[0]) lineentry[0]= self.add_line1(lineentry[1]) def rotate2(self): for lineentry in self.lines: Rotate2(lineentry[1][0]) Rotate2(lineentry[1][1]) self.canvas.delete(lineentry[0]) lineentry[0]= self.add_line1(lineentry[1]) def add_line1(self, line): rad= 150 ls= line[0]; le=line[1]; color= line[2] #print '((%i, %i) (%i, %i))' % (ls[0], ls[1], le[0],le[0]) lineobj= self.canvas.create_line(ls[0]+200, ls[1]+200, le[0]+200, le[1]+200, fill=color) return lineobj def add_line(self, line): lineobj= self.add_line1(line) lineentry= [lineobj, line] self.lines.append(lineentry) #--------------------------------------------------------------------------- root = Tk() app = App(root) root.mainloop()