#---------------------------------------------------------------------- # Program to illustrate hierarchical image decomposition. # # Copyright (C) October 30, 2017 -- Dr. William T. Verts #---------------------------------------------------------------------- #---------------------------------------------------------------------- # Functions to plot centered circles and rectangles with both interior # and exterior colors. #---------------------------------------------------------------------- def addCircle (Canvas,Xc,Yc,R,InColor=white,OutColor=black): addOvalFilled(Canvas,Xc-R,Yc-R,2*R,2*R,InColor) addOval (Canvas,Xc-R,Yc-R,2*R,2*R,OutColor) return def addRectangle (Canvas,Xleft,Ytop,Width,Height,InColor=white,OutColor=black): addRectFilled(Canvas,Xleft,Ytop,Width,Height,InColor) addRect (Canvas,Xleft,Ytop,Width,Height,OutColor) return #---------------------------------------------------------------------- # Drawing a car the "hard way" by explicitly calling the rectangle or # circle functions for every piece of geometry in the image. What # is necessary when moving a wheel or a window, or changing the color # or size of a wheel or window? (Short answer: you have to chase down # every related function call and adjust the numbers appropriately. # Possible, but very labor intensive, and easy to make errors.) #---------------------------------------------------------------------- def addCar1 (Canvas,X,Y,NewColor=green): addRectangle(Canvas,X-20,Y-40,100,30,NewColor) # Body addRectangle(Canvas,X+10,Y-70, 60,30,NewColor) # Cab addRectangle(Canvas,X+20,Y-65, 20,20,blue) # Left Window addRectangle(Canvas,X+23,Y-62, 14,14,cyan) # Left Pane addRectangle(Canvas,X+45,Y-65, 20,20,blue) # Right Window addRectangle(Canvas,X+48,Y-62, 14,14,cyan) # Right Pane addCircle (Canvas,X ,Y-10, 10,black) # Left Tire addCircle (Canvas,X ,Y-10, 7,gray) # Left Hubcap addCircle (Canvas,X+60,Y-10, 10,black) # Right Tire addCircle (Canvas,X+60,Y-10, 7,gray) # Right Hubcap return #---------------------------------------------------------------------- # Draw a car the sophisticated way. In this case, functions Wheel and # Window are ***local*** to the addCar2 function (not visible outside). # In both Wheel and Window, their own X and Y are local, but Canvas is # global to the inner functions (but local to addCar2). # # Changing a wheel's size or color is easy, as well as its position. # This approach is much more complicated in terms of the code that must # be written, but the complexity buys a certain simplicity in editing # the graphical objects. #---------------------------------------------------------------------- def addCar2 (Canvas,X,Y,NewColor=green): def Wheel(X,Y): addCircle(Canvas,X,Y,10,black) # Tire addCircle(Canvas,X,Y, 7,gray) # Hubcap return def Window(X,Y): addRectangle(Canvas,X,Y,20,20,blue) # Outer Frame addRectangle(Canvas,X+3,Y+3,14,14,cyan) # Inner Pane return addRectangle(Canvas,X-20,Y-40,100,30,NewColor) # Body addRectangle(Canvas,X+10,Y-70, 60,30,NewColor) # Cab Window(X+20,Y-65) # Left Window Window(X+45,Y-65) # Right Window Wheel (X ,Y-10) # Left Wheel Wheel (X+60,Y-10) # Right Wheel return #---------------------------------------------------------------------- # Plot the two cars #---------------------------------------------------------------------- def Main(): Canvas = makeEmptyPicture(600,400) addCar1 (Canvas,100,200) addCar2 (Canvas,300,200) show(Canvas) return