Script Lesson 9

download Script Lesson 9

of 9

Transcript of Script Lesson 9

  • 7/29/2019 Script Lesson 9

    1/9

    Home

    TI-Nspire Authoring

    TI-Nspire Scripting HQ

    Scripting Tutorial - Lesson 9

    Scripting Tutorial - Lesson 9: Graphical ShapeNumbers

    Download supporting files for this tutorial

    Texas Instruments TI-Nspire Scripting Support Page

    It is time to try and put together everythingthat we have learned so far. In this lesson andlesson 10 we will use Lua's graphic commandsalong with many of the enhancements wehave learned to create documents that areboth useful, clear and easy to use. We willbegin with the graphical display of shapenumbers - square, rectangular and triangularnumbers.

    Take amoment towatch theaccompanyingmovie andthen have aplay with thisdocumentusing the

    Player to get afeel for thepossibilitiesand for someof the designchoices thathave beenmade. Forexample,while it isgreat to usearrow keysand enter,escape andeven tab keys

  • 7/29/2019 Script Lesson 9

    2/9

    to makecontrol bybothcomputer andhandheldeasy, if wewant ourdocuments to

    be usable withthe Playerthen it isworth leavingin the sliders.

    In theexampleshown,pressing upand down

    arrowscontrols thevalue ofn,increasinganddecreasingthe numberbeing studied.Left and rightarrows show

    the threeoptions -square,rectangularand triangularnumbers.Pressing entershowsdifferentviews - the

    shape createdusing circles,with a border,and usingsquares.finally,pressing thetab key stepsthrough thebuilding up ofthat pattern,

    making it easyto see therelationshipsinvolved -

    Click anywhere on this image for a video demonstration

  • 7/29/2019 Script Lesson 9

    3/9

    squarenumbers arethe sums ofodd numbers,rectangularnumbers thesum of evennumbers, and

    because everyrectangle isdouble thecorrespondingtriangle, thetriangularnumbers arebuilt byadding thecountingnumbers.Pressingescape takesyou backthrough thisprogression.

    Lesson 9.1: Building a Grid Pattern

    We will beginby usingdrawLine tocreate asquare gridcorrespondingto the currentvalue ofn. But

    first, somepracticalconsiderations- plan yourdesign andlayout! Wewould like ourgrid to liecentered onthe page. We

    know how todo that. Butwe would alsolike it if the

  • 7/29/2019 Script Lesson 9

    4/9

    size of ourgrid remainedwithin thevisible page,with a bit ofwhite spacearound it. Sowe could

    divide thewidth andheight of thepage up bythe value ofn(and a bitmore forwhite space).Will also needa slider for n(and moresliders tofollow!). Soset up a splitpage with aGeometrywindow andslider on oneside and ourscript windowfor the main

    window.

    Look closelyat the codehere. Wedefine afunction(drawArray)that takes asits arguments

    the currentvalue ofn, thex and ycoordinates ofthe startingposition, andthe length andwidth of eachcell.

    You will see

    that thedrawLinefunction takesfour

    function drawArray(number, x, y, length,height, gc)

    for k = 0, number do

    for m = 0, number do

    gc:drawLine(

    x,

    y + height *m,

    x + length *number,

    y + height *m)

    gc:drawLine(

    x + length *k,

    y,

  • 7/29/2019 Script Lesson 9

    5/9

    arguments:the x and ycoordinates ofstart and endpoints. We willuse onevariable (m) todefine the

    horizontallines and theother (k) forthe verticallines. Again,study thecode providedto see oneway in whichto achievewhat wedesire.

    Once thefunction isdefined, weneed to paintthe screen toactually see it.here wechoose to

    define therequiredvariableswithin theon.paintfunction.Hence wedefine thewindow widthand height,

    recall thecurrent valueofn and use itto calculatethe x and ycoordinatesfor thestarting point,and the widthand length ofthe cells. Forthe lattervalues, wedivide thewindow

    x + length *k,

    y + height *number)

    end

    end

    end

    function on.paint(gc)

    w = platform.window:width()

    h = platform.window:height()

    num = var.recall("n") or 1

    xval = math.floor(w/(num+4))

    yval = math.floor(h/(num+4))

    x = w/2 - num * xval / 2

    y = h/2 - num * yval / 2

    gc:setPen("thin", "smooth")

    gc:setColorRGB(165,42,42)

    drawArray(num, x, y, xval, yval,gc)

    end

  • 7/29/2019 Script Lesson 9

    6/9

    dimensions bythe value of n(and a littlemore), thentake thelowest integervalue. Thiswill ensure

    that ourmodel alwaysremainswithin thescreendimensions.Finally, we setthe color andthe pen valuesand call ourfunction,drawArray.

    This producesa dynamicsquare grid,as required,controlled bythe currentvalue of avariable n

    within TI-Nspire.

    Lesson 9.2: Varying our Grid Pattern

    So we havea squaregrid - wewould liketo have twovariationson this

    theme: arectangulargrid (withone side

  • 7/29/2019 Script Lesson 9

    7/9

    than theother) anda triangulargrid. Wewill controlthese witha slidervariable we

    will calltype.

    First, therectangularnumbers:essentiallythe samescript asfor thesquarenumbers,but withonevariableone morethan theother.

    Recall thevalue of

    variabletype at thestart of ourdrawArrayfunction,then use itas the basisof a test: iftype == 1then (the

    squareroutine)elseif type== 2 then(rectangles)else(triangles)end.

    Thetriangle

    elseif types == 2 then

    for k = 0, number + 1 do

    for m = 0, number do

    gc:drawLine(

    x,

    y + height *m,

    x + length *(number + 1),

    y + height *m)

    gc:drawLine(

    x + length *

  • 7/29/2019 Script Lesson 9

    8/9

    follows -the mainvariationfrom thetwopreviousforms isthat,

    instead ofthe secondvariablerunningfrom 0 or1, it beginsat thecurrentvalue for k,thus

    indentingeach line toform thetriangle. Inorder toclose thefigure, itwasnecessaryto add anadditionalline at topandanother atthe rightside -hence thefour linesin thisscriptwhere the

    others hadonly two.

    Add theline whichdefines thevariabletypes fromtype andthe firstcondition

    ,

    y,

    x + length *k,

    y + height *number)

    endend

    else

    gc:drawLine(x, y, x + length * (number+1) -length, y )

    gc:drawLine(x + length * number, y, x +length * number, y + height * number)

    for k = 1, number do

    for m = k, number do

    gc:drawLine(x+ length*(m-1), y + height* m, x +length *number, y +

    height * m)

    gc:drawLine(x+ length * (k-1), y, x +length * (k-1), y + height* k)

    end

    end

    end

  • 7/29/2019 Script Lesson 9

    9/9

    (if types== 1 then)at the startof thefunctiondefinition,and thisscript will

    deliver thethreeshapesdrawnusing agrid.

    In the finaltutorial inthis series

    this scriptis furtherenhancedusingcircles todynamicallybuild eachof thepatterns,and ourusual arrow

    keycontrolswill beadded.

    Home TI-Nspire Authoring TI-Nspire Scripting HQScripting Tutorial -

    Lesson 9