Bezier Curve

Материал из Поле цифровой дидактики

Шаблон:Stub A Bezier Curve is a smooth curve with many control points. It is infinitely scalable and often used in graphics and terrain generation. To make one in Scratch, one must use the following two methods:

Method 1

whenclickedhidepenupsetpensizeto2forevererasealldefp-linesetpencolortodrawp-linesetpencolorto255drawb-linesteps:90enddefinesetq-pointA:A.xA.yB:B.xB.yprogress:progresssetq.xtoA.x+progress*B.x-A.xsetq.ytoA.y+progress*B.y-A.ydefinedefp-linedeleteallofp.xdeleteallofp.yaddxpositionofP.00top.xTheseareallthecontrolspointsofthecurve,usingclonesisanothersolutiontothis.addypositionofP.00top.yaddxpositionofP.01top.xaddypositionofP.01top.yaddxpositionofP.02top.xaddypositionofP.02top.yaddxpositionofP.03top.xaddypositionofP.03top.yaddxpositionofP.04top.xaddypositionofP.04top.yaddxpositionofP.05top.xaddypositionofP.05top.yaddxpositionofP.06top.xaddypositionofP.06top.yaddxpositionofP.07top.xaddypositionofP.07top.yaddxpositionofP.08top.xaddypositionofP.08top.yaddxpositionofP.09top.xaddypositionofP.09top.yaddxpositionofP.10top.xaddypositionofP.10top.yaddxpositionofP.11top.xaddypositionofP.11top.yaddxpositionofP.12top.xaddypositionofP.12top.yaddxpositionofP.13top.xaddypositionofP.13top.ydefinesetb-pointprogress:progressdeleteallofb.xdeleteallofb.yrepeatlengthofp.xadditemlengthofb.x+1ofp.xtob.xadditemlengthofb.y+1ofp.ytob.yendsetindex.nto1repeatlengthofb.x-index.nrepeatlengthofb.x-index.nsetq-pointA:itemindex.nofb.xitemindex.nofb.yB:itemindex.n+1ofb.xitemindex.n+1ofb.yprogress:progressaddq.xtob.xaddq.ytob.ychangeindex.nby1endchangeindex.nby1enddefinedrawp-linesetloop.nto1gotox:itemloop.nofp.xy:itemloop.nofp.ypendownrepeatlengthofp.x-loop.nchangeloop.nby1gotox:itemloop.nofp.xy:itemloop.nofp.yendpenupdefinedrawb-linesteps:stepssetloop.nto0setb-pointprogress:loop.n/stepsgotox:q.xy:q.ypendownrepeatsteps-loop.nchangeloop.nby1setb-pointprogress:loop.n/stepsgotox:q.xy:q.yendpenup

Method 2

One sprite needs this:

whenclickedhidedeleteallofclonexdeleteallofcloneysetdragmodedraggablemakeclonesdefinemakeclonessetCloneIDto1localvariablerepeat10createcloneofmyselfaddtoclonexaddtocloneyendWhenIstartasacloneshowmakesureithasacostumegotorandompositionforeverreplaceitemcloneIDofclonexwithxpositionreplaceitemcloneIDofcloneywithypositionend

In another sprite, put this:

whenclickedforevererasealldrawlinebetweendotswithoutscreenrefreshFindcurvepointswithoutscreenrefreshenddefinecopymainpointsdeleteallofxdeleteallofysetcounterto1repeatlengthofclonexadditemcounterofclonextoxadditemcounterofcloneytoychangecounterby1enddefinefindcurvepointsdeleteallofcurvexdeleteallofcurveysettto1repeat100copymainpointsrepeatuntillengthofx=1findlinesendaddxtocurvexaddytocurveychangetby1endDrawcurvedefinemovet%tox:xy:ypointindirectionatanofxposition-x/yposition-y+180*yposition<ymovesqrtofxposition-x*xposition-x+yposition-y*yposition-y*t/100stepsdefinefindlinessetlinecounterto1repeatlengthofx-1gotox:itemlinecounterofxy:itemlinecounterofymovet%tox:itemlinecounter+1ofxy:itemlinecounter+1ofyaddxpositiontoxaddypositiontoychangelinecounterby1endrepeatlinecounter-1delete1ofxdelete1ofyenddefineDrawcurvegotox:item1ofcurvexy:item1ofcurveypendownsetlinecounterto1repeatlengthofcurvexgotox:itemlinecounterofcurvexy:itemlinecounterofcurveychangelinecounterby1endpenupdefinedrawlinebetweendotsgotox:item1ofclonexy:item1ofcloneypendownsetlinecounterto1repeatlengthofclonexgotox:itemlinecounterofclonexy:itemlinecounterofcloneychangelinecounterby1endpenup

Example located here

Uses of Bezier Curves

There are many uses of Bezier curves within Scratch. These can include:

  • Combining them with velocity to make things fall or bounce.
  • Rendering a terrain with hills so that when one zooms in, the curve still looks nice.
  • Making customisable graphs.

See Also