Python Programming: An Introduction to Computer Science

(Nora) #1
5.5.CHOOSINGCOORDINATES 73

for year in range(1,11):


Theexpressionrange(1,11)producesa sequenceofints1–10.Theloopindex variableyearmarches
throughthissequenceonsuccessive iterationsoftheloop.So,thefirsttimethroughyearis 1,then2,then
3,etc.,upto10.Thevalueofyearis thenusedtocomputetheproperpositionofthelowerleftcornerof
eachbar.


xll = year * 25 + 40


I hopeyouarestartingtogetthehangofgraphicsprogramming.It’s a bitstrenuous,butveryaddictive.

5.5 ChoosingCoordinates.


Thelion’s shareoftheworkindesigningthefutvalgraphprogramwasindeterminingtheprecisecoor-
dinateswherethingswouldbeplacedonthescreen.Mostgraphicsprogrammingproblemsrequiresomesort
ofacoordinatetransformationtochangevaluesfroma real-worldproblemintothewindow coordinatesthat
getmappedontothecomputerscreen.Inourexample,theproblemdomaincalledforxvaluesrepresenting
theyear(0–10)andyvaluesrepresentingmonetaryamounts($0–$10,000).We hadtotransformtheseval-
uestoberepresentedina 320x 240window. It’s nicetoworkthroughanexampleortwo toseehowthis
transformationhappens,butit makesfortediousprogramming.
Coordinatetransformationis anintegralandwell-studiedcomponentofcomputergraphics. It doesn’t
take toomuchmathematicalsavvytoseethatthetransformationprocessalwaysfollowsthesamegeneral
pattern.Anythingthatfollowsa patterncanbedoneautomatically. Inordertosave youthetroubleofhaving
toexplicitlyconvertbackandforthbetweencoordinatesystems,thegraphicsmoduleprovidesa simple
mechanismtodoit foryou. WhenyoucreateaGraphWinyoucanspecifya coordinatesystemforthe
windowusingthesetCoords method.Themethodrequiresfourparametersspecifyingthecoordinates
ofthelower-leftandupper-rightcorners,respectively. Youcanthenusethiscoordinatesystemtoplace
graphicalobjectsinthewindow.
To take a simpleexample,supposewejustwanttodividethewindow intonineequalsquares,Tic-Tac-
Toefashion.Thiscouldbedonewithouttoomuchtroubleusingthedefault 200 x 200window, butit would
requirea bitofarithmetic.Theproblembecomestrivialif wefirstchangethecoordinatesofthewindow to
runfrom0 to3 inbothdimensions.


create a default200x200 window


win = GraphWin("Tic-Tac-Toe")


set coordinates to go from(0,0) in the lower left


to (3,3) in the upperright.


win.setCoords(0.0,0.0, 3.0, 3.0)


Draw vertical lines


Line(Point(1,0), Point(1,3)).draw(win)
Line(Point(2,0), Point(2,3)).draw(win)


Draw horizontal lines


Line(Point(0,1), Point(3,1)).draw(win)
Line(Point(0,2), Point(3,2)).draw(win)


Anotherbenefitofthisapproachis thatthesizeofthewindowcanbechangedbysimplychangingthedi-
mensionsusedwhenthewindow is created(e.g.win = GraphWin("Tic-Tac-Toe", 300, 300)).
Becausethesamecoordinatesspanthewindow (duetosetCoords) theobjectswillscaleappropriatelyto
thenew window size.Using“raw”window coordinateswouldrequirechangesinthedefinitionsofthelines.
We canapplythisideatosimplifyourgraphingfuturevalueprogram.Basically, wewantourgraphics
window togofrom0 through 10 (representingyears)inthexdimensionandfrom0 to10,000(representing
dollars)intheydimension.We couldcreatejustsucha window like this.

Free download pdf