Python Programming: An Introduction to Computer Science

(Nora) #1
70 CHAPTER5. OBJECTSANDGRAPHICS

objects.WhencreatingText, wespecifytheanchorpoint(thepointthetextis centeredon)andthestringto
useasthelabel.
Thelabelstringsareeasy. Ourlongestlabelis five characters,andthelabelsshouldalllineuponthe
rightsideofa column,sotheshorterstringswillbepaddedontheleftwithspaces.Theplacementofthe
labelsis chosenwitha bitofcalculationandsometrialanderror. Playingwithsomeinteractive examples,
it seemsthata stringoflengthfive looksnicelypositionedinthehorizontaldirectionplacingthecenter 20
pixelsinfromtheleftedge.Thisleavesjusta bitofwhitespaceat themargin.
Intheverticaldirection,wehave justover 200 pixelstoworkwith.Asimplescalingwouldbetohave
100 pixelsrepresent$5,000.Thatmeansourfive labelsshouldbespaced 50 pixelsapart.Using 200 pixels
fortherange0–10,000leaves 240  200 40 pixelstosplitbetweenthetopandbottommargins.We might
wanttoleave a littlemoremarginatthetoptoaccommodatevaluesthatgrowbeyond$10,000. Alittle
experimentationsuggeststhatputtingthe“ 0.0K”label 10 pixelsfromthebottom(position230)seemsto
looknice.
Elaboratingouralgorithmtoincludethesedetails,thesinglestep


Draw scale labels on leftside of window


becomesa sequenceofsteps


Draw label " 0.0K"at (20, 230)
Draw label " 2.5K"at (20, 180)
Draw label " 5.0K"at (20, 130)
Draw label " 7.5K"at (20, 80)
Draw label "10.0K"at (20, 30)


Thenextstepintheoriginaldesigncallsfordrawingthebarthatcorrespondstotheinitialamountofthe
principal.It is easytoseewherethelowerleftcornerofthisbarshouldbe.Thevalueof$0.0is located
verticallyat pixel 230,andthelabelsarecentered 20 pixelsinfromtheleftedge.Addinganother 20 pixels
getsusto therightedgeofthelabels.Thusthelowerleftcornerofthe0thbarshouldbeat location


40  230 .
Nowwejustneedtofigureoutwheretheopposite(upperright)cornerofthebarshouldbesothatwe
candraw anappropriaterectangle.Intheverticaldirection,theheightofthebaris determinedbythevalue
ofprincipal. Indrawingthescale,wedeterminedthat 100 pixelsis equalto$5,000.Thismeansthat
wehave 100



5000 0 02 pixelstothedollar. Thistellsus,forexample,thata principalof$2,000should
producea barofheight 2000


02  40 pixels.Ingeneral,theypositionoftheupper-rightcornerwillbe
givenby 230 



principal

0 02 . (Rememberthat 230 is the0 point,andtheycoordinatesdecreasegoing
up).
How wideshouldthebarbe?Thewindow is 320pixelswide,but 40 pixelsareeatenupbythelabelson
theleft.Thatleavesuswith 280 pixelsfor 11 bars 280



11 25 4545.Let’s justmake eachbar 25 pixels;that
willgive usa bitofmarginontherightside.So,therightedgeofourfirstbarwillbeat position 40  25 65.
We cannow fillthedetailsfordrawingthefirstbarintoouralgorithm.


Draw a rectangle from(40, 230) to (65, 230 - principal * 0.02)


Atthispoint,wehave madeallthemajordecisionsandcalculationsrequiredtofinishouttheproblem.All
thatremainsis topercolatethesedetailsintotherestofthealgorithm.Figure5.8showsthegenerallayoutof
thewindow withsomeofthedimensionswehave chosen.
Let’s figureoutwherethelower-leftcornerofeachbarisgoingtobelocated. We chosea barwidth
of25,sothebarforeachsuccessive yearwillstart 25 pixelsfartherrightthanthepreviousyear. We can
use a variableyeartorepresenttheyearnumberandcalculatethexcoordinateofthelowerleftcorneras
year


25   40.(The 40 leavesspaceontheleftedgeforthelabels.)Ofcourse,theycoordinateofthis
pointis still 230 (thebottomofthegraph).
To findtheupper-rightcornerofa bar, weadd 25 (thewidthofthebar)tothexvalueofthelower-left
corner. Theyvalueoftheupperrightcorneris determinedfromthe(updated)valueofprincipalexactly
aswedeterminedit forthefirstbar. Hereis therefinedalgorithm.


for year running froma value of 1 up through 10:
Calculate principal= principal * (1 + apr)

Free download pdf