By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,639 Members | 1,125 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,639 IT Pros & Developers. It's quick & easy.

Sunrise, Motion & Blending Colors

P: 1
Using the code below I need to make a movie of a new function Sunrise it should: (A) Cause the sun to come up from behind the horizon up into the sky. The horizon line is a black line at the common edge of the sky and the ground.
(B) As the sun comes up, the sky must change from black to cyan and the ground from dark gray to orange. It should be 320x240 but should be written so the size of the canvas is a variable.

I can only make the Canvas, other wise I'm lost. Any contributions, ideas would be helpful. Thank You!

Expand|Select|Wrap|Line Numbers
  1. import time
  2.  
  3. def INT(N):
  4.     return int(round(N))
  5.  
  6. def Interpolate (N0,N1,T=0.5):
  7.     return (N1 - N0) * float(T) + N0
  8.  
  9. def InterpInt (N0,N1,T=0.5):
  10.     return INT(Interpolate(N0,N1,T))
  11.  
  12.  
  13. def Parabola (N0,N1,N2,T=0.5):
  14.     A = +2.0*N0 - 4.0*N1 + 2.0*N2
  15.     B = -3.0*N0 + 4.0*N1 - N2
  16.     C = N0
  17.     return (A * T * T) + (B * T) + C
  18.  
  19. def ParabolaInt (N0,N1,N2,T=0.5):
  20.     return INT(Parabola(N0,N1,N2,T))
  21.  
  22.  
  23. def BlendPoints2D(P0,P1,T=0.5):
  24.     X = InterpInt(P0[0],P1[0],T)
  25.     Y = InterpInt(P0[1],P1[1],T)
  26.     return [X,Y]
  27.  
  28.  
  29. def BlendPoints3D(P0,P1,T=0.5):
  30.     X = InterpInt(P0[0],P1[0],T)
  31.     Y = InterpInt(P0[1],P1[1],T)
  32.     Z = InterpInt(P0[2],P1[2],T)
  33.     return [X,Y,Z]
  34.  
  35.  
  36. def BlendColor(C0,C1,T=0.5):
  37.     R = InterpInt(C0.getRed(),   C1.getRed(),   T)
  38.     G = InterpInt(C0.getGreen(), C1.getGreen(), T)
  39.     B = InterpInt(C0.getBlue(),  C1.getBlue(),  T)
  40.     return makeColor(R,G,B)
  41.  
  42. #--------------------------------------------------
  43.  
  44. def BlendParabola2D(P0,P1,P2,T=0.5):
  45.     X = ParabolaInt(P0[0],P1[0],P2[0],T)
  46.     Y = ParabolaInt(P0[1],P1[1],P2[1],T)
  47.     return [X,Y]
  48.  
  49.  
  50. def BlendParabola3D(P0,P1,P2,T=0.5):
  51.     X = ParabolaInt(P0[0],P1[0],P2[0],T)
  52.     Y = ParabolaInt(P0[1],P1[1],P2[1],T)
  53.     Z = ParabolaInt(P0[2],P1[2],P2[2],T)
  54.     return [X,Y,Z]
  55.  
  56. #--------------------------------------------------
  57.  
  58. def BlendParabolaColor(C0,C1,C2,T=0.5):
  59.     R = ParabolaInt(C0.getRed(),   C1.getRed(),   C2.getRed(),   T)
  60.     G = ParabolaInt(C0.getGreen(), C1.getGreen(), C2.getGreen(), T)
  61.     B = ParabolaInt(C0.getBlue(),  C1.getBlue(),  C2.getBlue(),  T)
  62.     return makeColor(R,G,B)
  63.  
  64.  
  65. def addCircleFilled (Canvas, Xc,Yc,R,NewColor=black):
  66.     addOvalFilled(Canvas,INT(Xc-R),INT(Yc-R),INT(2*R+1),INT(2*R+1),NewColor)
  67.     return
  68.  
  69.  
  70. def addEllipseFilled (Canvas, Xc,Yc,Rx,Ry,NewColor=black):
  71.     addOvalFilled(Canvas,INT(Xc-Rx),INT(Yc-Ry),INT(2*Rx+1),INT(2*Ry+1),NewColor)
  72.     return
  73.  
  74. #--------------------------------------------------
  75. # These routines all use 2D points as arguments
  76. # instead of explicit X and Y values, while again
  77. # keeping in the same style as the built-in JES
  78. # routines.  All routines in this category start
  79. # with the prefix "plot".  Some directly map onto
  80. # JES routines, others implement new funtionality
  81. # (such as triangles and polygons).
  82. #--------------------------------------------------
  83.  
  84. def plotLine (Canvas,P,Q,NewColor=black):
  85.     addLine(Canvas,INT(P[0]),INT(P[1]),INT(Q[0]),INT(Q[1]),NewColor)
  86.     return
  87.  
  88.  
  89. def plotRect (Canvas,P,Q,NewColor=black):
  90.     addRect(Canvas,INT(P[0]),INT(P[1]),INT(Q[0]-P[0]+1),INT(Q[1]-P[1]+1),NewColor)
  91.     return
  92.  
  93.  
  94. def plotRectFilled (Canvas,P,Q,NewColor=black):
  95.     addRectFilled(Canvas,INT(P[0]),INT(P[1]),INT(Q[0]-P[0]+1),INT(Q[1]-P[1]+1),NewColor)
  96.     return
  97.  
  98.  
  99. def plotOval (Canvas,P,Q,NewColor=black):
  100.     addOval(Canvas,INT(P[0]),INT(P[1]),INT(Q[0]-P[0]+1),INT(Q[1]-P[1]+1),NewColor)
  101.     return
  102.  
  103.  
  104. def plotOvalFilled (Canvas,P,Q,NewColor=black):
  105.     addOvalFilled(Canvas,INT(P[0]),INT(P[1]),INT(Q[0]-P[0]+1),INT(Q[1]-P[1]+1),NewColor)
  106.     return
  107.  
  108.  
  109. def plotCircleFilled (Canvas,Center,Radius,NewColor=black):
  110.     addCircleFilled(Canvas,Center[0],Center[1],Radius,NewColor)
  111.     return
  112.  
  113.  
  114. def plotTriangle(Canvas,P,Q,R,NewColor=black):
  115.     plotLine(Canvas,P,Q,NewColor)
  116.     plotLine(Canvas,Q,R,NewColor)
  117.     plotLine(Canvas,R,P,NewColor)
  118.     return
  119.  
  120.  
  121. def plotCenteredTriangle(Canvas,P,Radius,NewColor=black):                       
  122.     Offset = sqrt(3)/2.0 * Radius
  123.     P0 = [P[0], P[1]-Offset]
  124.     P1 = [P[0]+Offset,P[1]+Radius/2.0]
  125.     P2 = [P[0]-Offset,P[1]+Radius/2.0]
  126.     plotTriangle(Canvas,P0,P1,P2,NewColor)
  127.     return
  128.  
  129.  
  130. def plotCenteredPolygon(Canvas,P,Radius,Sides,NewColor=black):
  131.     Step = 2 * pi / Sides
  132.     Angle = 0.0
  133.     Last = [P[0] + Radius, P[1]]
  134.     for I in range(Sides):
  135.         Angle = Angle + Step
  136.         Next  = [P[0] + Radius * cos(Angle), P[1] - Radius * sin(Angle)]
  137.         plotLine(Canvas, Last, Next, NewColor)
  138.         Last  = Next
  139.     return
  140.  
  141.  
  142. Origin = [0,0]    # Global, 2D position on screen
  143.  
  144.  
  145. def Ortho(P3D):
  146. #    Angle = 30.0 * pi / 180.0
  147.     X = P3D[0]
  148.     Y = P3D[1]
  149.     Z = P3D[2]
  150.     NewX = X + Z * 0.8660254
  151.     NewY = -(Y + Z * 0.5)
  152.     return [NewX,NewY]
  153.  
  154.  
  155. def add3D (P3D,Q3D):
  156.     return [P3D[0]+Q3D[0],P3D[1]+Q3D[1],P3D[2]+Q3D[2]]
  157.  
  158.  
  159.  
  160. def add2D (P2D,Q2D):
  161.     return [P2D[0]+Q2D[0],P2D[1]+Q2D[1]]
  162.  
  163. #--------------------------------------------------
  164.  
  165. def SetOrigin (X,Y):
  166.     global Origin
  167.     Origin = [X,Y]
  168.     return
  169.  
  170.  
  171. def Line3D (Canvas,P3D,Q3D,NewColor=black):
  172.     global Origin
  173.     plotLine(Canvas,add2D(Origin,Ortho(P3D)), add2D(Origin,Ortho(Q3D)), NewColor)
  174.     return
  175.  
  176.  
  177. def Axes (Canvas,Size,Separation,Tic=5):
  178.     Line3D(Canvas,[-Size,0,0],[+Size,0,0])
  179.     Line3D(Canvas,[0,-Size,0],[0,+Size,0])
  180.     Line3D(Canvas,[0,0,-Size],[0,0,+Size])
  181.     N = 0
  182.     while (N <= Size):
  183.         Line3D(Canvas,[+N,0,-Tic],[+N,0,+Tic])
  184.         Line3D(Canvas,[-N,0,-Tic],[-N,0,+Tic])
  185.         Line3D(Canvas,[0,+N,-Tic],[0,+N,+Tic])
  186.         Line3D(Canvas,[0,-N,-Tic],[0,-N,+Tic])
  187.         Line3D(Canvas,[-Tic,0,+N],[+Tic,0,+N])
  188.         Line3D(Canvas,[-Tic,0,-N],[+Tic,0,-N])
  189.         N = N + Separation
  190.     return
  191.  
  192.  
  193. def addCircleXY (Canvas, Center, Radius, NewColor=black):
  194.     Steps = 60
  195.     StepAngle = 2.0 * pi / Steps
  196.     Last = [Center[0]+Radius, Center[1], Center[2]]
  197.     for I in range(Steps):
  198.         Angle = (I+1) * StepAngle
  199.         X = Radius * cos(Angle)
  200.         Y = Radius * sin(Angle)
  201.         Current = [Center[0]+X,Center[1]+Y,Center[2]]
  202.         Line3D(Canvas, Last, Current, NewColor)
  203.         Last = Current
  204.     return
  205.  
  206.  
  207. def addCircleXZ (Canvas, Center, Radius, NewColor=black):
  208.     Steps = 60
  209.     StepAngle = 2.0 * pi / Steps
  210.     Last = [Center[0]+Radius, Center[1], Center[2]]
  211.     for I in range(Steps):
  212.         Angle = (I+1) * StepAngle
  213.         X = Radius * cos(Angle)
  214.         Y = Radius * sin(Angle)
  215.         Current = [Center[0]+X,Center[1],Center[2]+Y]
  216.         Line3D(Canvas, Last, Current, NewColor)
  217.         Last = Current
  218.     return
  219.  
  220.  
  221.  
  222. def addCircleYZ (Canvas, Center, Radius, NewColor=black):
  223.     Steps = 60
  224.     StepAngle = 2.0 * pi / Steps
  225.     Last = [Center[0], Center[1]+Radius, Center[2]]
  226.     for I in range(Steps):
  227.         Angle = (I+1) * StepAngle
  228.         X = Radius * cos(Angle)
  229.         Y = Radius * sin(Angle)
  230.         Current = [Center[0],Center[1]+X,Center[2]+Y]
  231.         Line3D(Canvas, Last, Current, NewColor)
  232.         Last = Current
  233.     return
  234.  
  235.  
  236. def addSimpleSphere (Canvas, Center, Radius, NewColor=black):
  237.     addCircleXY(Canvas, Center, Radius, NewColor)
  238.     addCircleXZ(Canvas, Center, Radius, NewColor)
  239.     addCircleYZ(Canvas, Center, Radius, NewColor)        
  240.     return
  241.  
  242.  
  243. def addSphere (Canvas, Center, Radius, NewColor=black):
  244.     Angle = -90.0
  245.     while (Angle <= 90.0):
  246.         Radians = Angle / 180.0 * pi
  247.         NewRadius    = Radius * cos(Radians)
  248.         NewOffset    = Radius * sin(Radians)   
  249.         NewCenter    = add3D(Center, [NewOffset,0,0])
  250.         addCircleYZ(Canvas, NewCenter, NewRadius, NewColor)
  251.         NewCenter    = add3D(Center, [0,NewOffset,0])
  252.         addCircleXZ(Canvas, NewCenter, NewRadius, NewColor)
  253.         NewCenter    = add3D(Center, [0,0,NewOffset])
  254.         addCircleXY(Canvas, NewCenter, NewRadius, NewColor)
  255.         Angle = Angle + 15.0
  256.     return
  257.  
  258.  
  259. def BounceSpheres():
  260.     Canvas = makeEmptyPicture(640,480)
  261.     SetOrigin(320,240)
  262.  
  263.     Radius = 30         # Radius of sphere
  264.     Size   = 100        # Size of cube
  265.     Center = [5,7,-3]   # Original center of sphere
  266.     DeltaX = 3          # Change in X per step
  267.     DeltaY = 7          # Change in X per step
  268.     DeltaZ = 5          # Change in X per step
  269.     Trails = []         # List of previous centers
  270.  
  271.     while (True):
  272.         # Clear previous image
  273.         setAllPixelsToAColor(Canvas,white)
  274.  
  275.         # Paint back "wall" of the cube
  276.         Line3D(Canvas,[-Size,+Size,+Size],[+Size,+Size,+Size],gray)
  277.         Line3D(Canvas,[-Size,-Size,+Size],[+Size,-Size,+Size],gray)
  278.         Line3D(Canvas,[-Size,+Size,+Size],[-Size,-Size,+Size],gray)
  279.         Line3D(Canvas,[+Size,+Size,+Size],[+Size,-Size,+Size],gray)
  280.  
  281.         # Plot the center of the cube and the current
  282.         # position of the sphere.
  283.         Axes(Canvas,10,10,2)
  284.         addSphere(Canvas, Center, Radius, blue)
  285.  
  286.         # Plot the remaining edges of the cube.
  287.         Line3D(Canvas,[-Size,+Size,-Size],[+Size,+Size,-Size],gray)
  288.         Line3D(Canvas,[-Size,-Size,-Size],[+Size,-Size,-Size],gray)
  289.         Line3D(Canvas,[-Size,+Size,-Size],[-Size,-Size,-Size],gray)
  290.         Line3D(Canvas,[+Size,+Size,-Size],[+Size,-Size,-Size],gray)
  291.  
  292.         Line3D(Canvas,[-Size,-Size,-Size],[-Size,-Size,+Size],gray)
  293.         Line3D(Canvas,[-Size,+Size,-Size],[-Size,+Size,+Size],gray)
  294.         Line3D(Canvas,[+Size,-Size,-Size],[+Size,-Size,+Size],gray)
  295.         Line3D(Canvas,[+Size,+Size,-Size],[+Size,+Size,+Size],gray)
  296.  
  297.         # Keep a list of upto the last 50 positions
  298.         # of the sphere, then plot those positions
  299.         # as a color spot, <R,G,B> color depending on
  300.         # the <X,Y,Z> position.
  301.         Trails = Trails + [center]
  302.         if (len(Trails) > 50): Trails = Trails[1:len(Trails)]
  303.         for P in Trails:
  304.             Q = add2D(Origin,Ortho(P))
  305.             RR = int(float(P[0]+Size)/float(2*Size)*255.0)
  306.             GG = int(float(P[1]+Size)/float(2*Size)*255.0)
  307.             BB = int(float(P[2]+Size)/float(2*Size)*255.0)
  308.             plotCircleFilled(Canvas, Q, 3, makeColor(RR,GG,BB))
  309.  
  310.         # Update the position of the sphere,
  311.         # bouncing off a wall as needed.
  312.         Center = add3D(Center, [DeltaX,DeltaY,DeltaZ])
  313.         if (abs(Center[0])+Radius > Size): DeltaX = -DeltaX
  314.         if (abs(Center[1])+Radius > Size): DeltaY = -DeltaY
  315.         if (abs(Center[2])+Radius > Size): DeltaZ = -DeltaZ
  316.  
  317.         # Display updated screen and hold.
  318.         repaint(Canvas)
  319.         time.sleep(0.01)
  320.     return
  321.  
  322.  
  323. def Exercise2():
  324.     Canvas = makeEmptyPicture(640,480)
  325.     SetOrigin(320,240)
  326.     addSimpleSphere(Canvas, [+100,10,0], 100, blue)
  327.     show(Canvas)
  328.     time.sleep(0.1)
  329.     addSimpleSphere(Canvas, [+100,10,0], 50, blue)
  330.     repaint(Canvas)
  331.     time.sleep(0.1)
  332.     addSimpleSphere(Canvas, [+100,10,0], 40, red)
  333.     repaint(Canvas)
  334.     addSimpleSphere(Canvas, [+100,10,0], 30, green)
  335.     addSimpleSphere(Canvas, [+100,10,0], 20, orange)
  336.     addSimpleSphere(Canvas, [-100,0,0], 50, blue)
  337.     repaint(Canvas)
  338.     return
  339.  
  340.  
  341. def TraceParabola():
  342.     Canvas = makeEmptyPicture(640,480)
  343.     SetOrigin(getWidth(Canvas)/2,getHeight(Canvas)/2)
  344.     Center = [0,0,0]
  345.     P0 = [-90,40,80]
  346.     P1 = [10,-50,10]
  347.     P2 = [90,70,60]
  348.     Steps = 100
  349.     Points = [P0]
  350.     MyStyle = makeStyle(serif, bold+italic, 24)
  351.     for I in range(Steps):
  352.         setAllPixelsToAColor(Canvas,white)
  353.         T = float(I)/(Steps-1)
  354.         P = BlendParabola3D(P0,P1,P2,T)
  355.         X = P[0]
  356.         Y = P[1]
  357.         Z = P[2]
  358.         Points = Points + [P]
  359.         Line3D(Canvas, [X,0,0], [X,0,Z], red)
  360.         Line3D(Canvas, [X,Y,Z], [X,0,Z], red)
  361.         Line3D(Canvas, [0,0,Z], [X,0,Z], red)
  362.         Last = Points[0]
  363.         for P in Points:
  364.             Line3D(Canvas, Last, P, blue)
  365.             Last = P
  366.         Axes(Canvas,200,200,2)
  367.         plotCircleFilled(Canvas, add2D(Origin,Ortho(P0)), 3, green)
  368.         plotCircleFilled(Canvas, add2D(Origin,Ortho(P1)), 3, green)
  369.         plotCircleFilled(Canvas, add2D(Origin,Ortho(P2)), 3, green)
  370.         addTextWithStyle(Canvas, 10, getHeight(Canvas)-10, "P = <" + str(P[0]) + "," + str(P[1]) + "," + str(P[2]) + ">", MyStyle)
  371.         repaint(Canvas)
  372.         time.sleep(0.3)
  373.     return
Nov 26 '12 #1
Share this Question
Share on Google+
1 Reply

Expert 100+
P: 626
I see no point to posting a 373 line program for us to wade through. I am not, so this is a simplified example of what one would do without any reference to any functions in the code you posted.
Expand|Select|Wrap|Line Numbers
  1. try:
  2.     import Tkinter as tk     ## Python 2.x
  3. except ImportError:
  4.     import tkinter as tk     ## Python 3.x
  5.  
  6. class Sunrise(object):
  7.     def __init__(self):
  8.         self.window = tk.Tk()
  9.         self.canvas = tk.Canvas(self.window, width = 300, height = 300)
  10.         self.canvas.pack()
  11.  
  12.         self.create_objects()
  13.         deltax = 1
  14.         deltay = -2
  15.         ctr = 0
  16.         self.colors_list = ["orange", "blue", "black"]
  17.         while True:
  18.             self.canvas.move('Sun', deltax, deltay)
  19.  
  20.             ## test for off the edge
  21.             x1, y1, x2, y2 = self.canvas.coords(self.circle)
  22.             print x1, y2
  23.             if x1 > 300 or y2 < 1:
  24.                 self.exit()
  25.                 break
  26.  
  27.             ## change background color
  28.             ctr += 1
  29.             ctr=self.change_bg(ctr)
  30.             self.canvas.after(30)
  31.             self.canvas.update()
  32.  
  33.         self.window.mainloop()
  34.  
  35.  
  36.     def create_objects(self):
  37.         ## horizon
  38.         line_x1=10
  39.         line_y1=200
  40.         line_x2=290
  41.         self.canvas.create_line(line_x1, line_y1, line_x2, line_y1, width=3)
  42.  
  43.         ## background sky
  44.         self.sky=self.canvas.create_rectangle(0, 0, 300, line_y1, outline='white', 
  45.                            fill='gray50')
  46.  
  47.         ## sun
  48.         x1=75
  49.         y1=100
  50.         self.circle=self.canvas.create_oval(x1, y1, x1+100, y1+100, fill="yellow", 
  51.                                        tag='Sun')
  52.  
  53.         b1 = tk.Button(text="Exit", bg='lightblue', command=self.exit)
  54.         b1.pack()
  55.  
  56.     def change_bg(self, ctr):
  57.         if ctr > 25:
  58.             ctr = 0
  59.             if len(self.colors_list):
  60.                 self.canvas.itemconfigure(self.sky, fill=self.colors_list[0])
  61.                 del self.colors_list[0]
  62.         return ctr
  63.  
  64.     def exit(self):
  65.         self.window.after(100)  ## allow other updates to finish
  66.         self.window.destroy()
  67.         self.window.quit()
  68.  
  69. SR=Sunrise() 
Nov 27 '12 #2

Post your reply

Sign in to post your reply or Sign up for a free account.