#---------------------------------------------------------------------- # Introduction to simple graphics # # Copyright (C) February 13, 2017 -- Dr. William T. Verts #---------------------------------------------------------------------- #---------------------------------------------------------------------- # While the makePicture function requires a filename of a graphics # file (.GIF, .JPG, etc.), makeEmptyPicture creates a blank canvas. # The following program creates and shows a white 800x600 canvas. #---------------------------------------------------------------------- def Main1(): Canvas = makeEmptyPicture(800,600) show(Canvas) return #---------------------------------------------------------------------- # The following program creates and shows a yellow 800x600 canvas. # The color is optional, and may be black, red, green, blue, magenta, # cyan, yellow, white, or gray. If omitted, white is assumed. # The color may also be synthesized from RGB components (see Main3). #---------------------------------------------------------------------- def Main2(): Canvas = makeEmptyPicture(800,600,yellow) show(Canvas) return #---------------------------------------------------------------------- # The following program creates and shows an orange 800x600 canvas, # where the color is mixed from R=255, G=128, and B=0 (the limits on # all three values are between 0...255). #---------------------------------------------------------------------- def Main3(): Canvas = makeEmptyPicture(800,600,makeColor(255,128,0)) show(Canvas) return #---------------------------------------------------------------------- # The synthesized color may be assigned to a variable, and then used # anywhere colors are expected. #---------------------------------------------------------------------- def Main4(): MyColor = makeColor(255,128,0) Canvas = makeEmptyPicture(800,600,MyColor) show(Canvas) return #---------------------------------------------------------------------- # Let's draw a blue line between <100,50> and <400,200> #---------------------------------------------------------------------- def Main5(): MyColor = makeColor(255,128,0) Canvas = makeEmptyPicture(800,600,MyColor) show(Canvas) addLine(Canvas, 100, 50, 400, 200, blue) return #---------------------------------------------------------------------- # Let's draw a black line between <100,50> and <400,200> #---------------------------------------------------------------------- def Main6(): MyColor = makeColor(255,128,0) Canvas = makeEmptyPicture(800,600,MyColor) show(Canvas) addLine(Canvas, 100, 50, 400, 200) # If color omitted, assume black return #---------------------------------------------------------------------- # How do we draw rectangles? Write a function! Notice that the # LineColor has a default value of black - if the 6th parameter is # included it must be a color passed into LineColor, but if omitted # LineColor is assumed to be black. #---------------------------------------------------------------------- def Rectangle (Canvas, X, Y, Width, Height, LineColor=black): addLine(Canvas, X, Y, X, Y+Height, LineColor) # Left Line addLine(Canvas, X+Width, Y, X+Width, Y+Height, LineColor) # Right Line addLine(Canvas, X, Y, X+Width, Y, LineColor) # Top Line addLine(Canvas, X, Y+Height, X+Width, Y+Height, LineColor) # Bottom Line return def Main7(): MyColor = makeColor(255,128,0) Canvas = makeEmptyPicture(800,600,MyColor) show(Canvas) Rectangle(Canvas, 200,100,300,200) repaint(Canvas) return #---------------------------------------------------------------------- # However, JES has an addRect function with the same parameters. #---------------------------------------------------------------------- def Main8(): MyColor = makeColor(255,128,0) Canvas = makeEmptyPicture(800,600,MyColor) show(Canvas) addRect(Canvas, 200,100,300,200) repaint(Canvas) return #---------------------------------------------------------------------- # The lesson is that you can use the tools you have to build the tools # you need, but sometimes there is already a tool that does what you # want. That is the case with Rectangle/addRect. We don't need the # Rectangle function. There is also an addOval function, with the same # parameters as addRect, BUT THERE IS NO CIRCLE function. #---------------------------------------------------------------------- def Main9(): MyColor = makeColor(255,128,0) Canvas = makeEmptyPicture(800,600,MyColor) show(Canvas) addOval(Canvas, 200,100,300,200) addRect(Canvas, 200,100,300,200) # Rect drawn on top of the oval repaint(Canvas) return #---------------------------------------------------------------------- # So, if we want to have a circle function that takes in center X, # center Y, and radius, then we have to write one. But, all we can # use is the addOval function, which expects its reference point at # the upper left corner, not the center. We need to have our circle # function figure out from center and radius where the upper left # corner is located. #---------------------------------------------------------------------- def Circle (Canvas, CenterX, CenterY, Radius, LineColor=black): addOval(Canvas, CenterX-Radius, CenterY-Radius, 2*Radius, 2*Radius, LineColor) return def Main10(): MyColor = makeColor(255,128,0) Canvas = makeEmptyPicture(800,600,MyColor) show(Canvas) addOval(Canvas, 200,100,300,200) addRect(Canvas, 200,100,300,200) Circle (Canvas, 200,100,100,red) repaint(Canvas) return