Python Programming: An Introduction to Computer Science

(Nora) #1
5.3.USINGGRAPHICALOBJECTS 67

Circle

y:

x:
100

100

center: Point
radius:

draw( )
.
.
.

GraphWin
.
.
.

Low-level drawing commands

win:

30

circ:

Figure5.5:Objectinteractionstodraw a circle.

Incorrect way to createtwo circles.


leftEye = Circle(Point(80,50), 5)
leftEye.setFill(’yellow’)
leftEye.setOutline(’red’)
rightEye = leftEye
rightEye.move(20,0)


Thebasicideais tocreatethelefteyeandthencopy thatintoa righteyewhichis thenmovedover 20units.
Thisdoesn’t work.Theproblemhereis thatonlyoneCircleobjectis created.Theassignment


rightEye = leftEye


simplymakesrightEyerefertotheverysamecircleasleftEye. Figure5.6showsthesituation.When
theCircleis movedinthelastlineofcode,bothrightEyeandleftEyerefertoit initsnew location
ontherightside.Thissituationwheretwo variablesrefertothesameobjectis calledaliasing, andit can
sometimesproduceratherunexpectedresults.


rightEye:

leftEye: Circle

y:

x:
50

80

center: Point
radius: 10

Figure5.6:VariablesleftEyeandrightEyearealiases.

Onesolutiontothisproblemwouldbetocreatea separatecircleforeacheye.

A correct way to createtwo circles.


leftEye = Circle(Point(80,50), 5)
leftEye.setFill(’yellow’)

Free download pdf