Difference between revisions of "CSC111 Dot Notation and Objects"
(→Exercise 1) |
|||
(2 intermediate revisions by the same user not shown) | |||
Line 13: | Line 13: | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
<br /> | <br /> | ||
<br /> | <br /> | ||
− | <br /> | + | <br /> |
+ | ==Solutions== | ||
+ | <source lang="python"> | ||
+ | from graphics import * | ||
+ | |||
+ | |||
+ | def main(): | ||
+ | win = GraphWin( "DotNotation", 600, 400 ) | ||
+ | |||
+ | P1 = Point( 100, 100 ) | ||
+ | |||
+ | # Wheel 1 | ||
+ | h1 = Circle( P1, 30 ) | ||
+ | h1.setFill( "grey" ) | ||
+ | w1 = Circle( P1, 50 ) | ||
+ | w1.setFill( "black" ) | ||
+ | w1.draw( win ) | ||
+ | h1.draw( win ) | ||
+ | |||
+ | # Wheel 2 | ||
+ | h2 = Circle( Point( P1.getX()+300, P1.getY() ), h1.getRadius() ) | ||
+ | h2.setFill( "grey" ) | ||
+ | w2 = Circle( Point( h2.getCenter().getX(), h2.getCenter().getY() ), | ||
+ | w1.getRadius() ) | ||
+ | w2.setFill( "black" ) | ||
+ | w2.draw( win ) | ||
+ | h2.draw( win ) | ||
+ | |||
+ | # wait for click | ||
+ | #win.getMouse() | ||
+ | win.close() | ||
+ | main() | ||
+ | </source> | ||
<br /> | <br /> | ||
Latest revision as of 11:19, 13 October 2011
--D. Thiebaut 09:11, 13 October 2011 (EDT)
Exercise 1
For the 2 questions below, use as many variables as you need.
- Draw the wheel of a Taxi cab at a center point of coordinates ( 100, 100 ). The hubcap is 30 pixels wide, the tire 50.
- Draw the second wheel of the Taxi cab with a center point horizontally 300 pixels away from the center, to the right.
Exercise 2
Same two questions as in Exercise 1, but you can only use 5 variables: P1, the original point (100, 100), h1, w1, the first hubcap and wheel, and h2, w2, for the second wheel.
Solutions
from graphics import *
def main():
win = GraphWin( "DotNotation", 600, 400 )
P1 = Point( 100, 100 )
# Wheel 1
h1 = Circle( P1, 30 )
h1.setFill( "grey" )
w1 = Circle( P1, 50 )
w1.setFill( "black" )
w1.draw( win )
h1.draw( win )
# Wheel 2
h2 = Circle( Point( P1.getX()+300, P1.getY() ), h1.getRadius() )
h2.setFill( "grey" )
w2 = Circle( Point( h2.getCenter().getX(), h2.getCenter().getY() ),
w1.getRadius() )
w2.setFill( "black" )
w2.draw( win )
h2.draw( win )
# wait for click
#win.getMouse()
win.close()
main()