Page 2 of 2

Re: Draw Sine/Cosine wave

Posted: Thu Jan 05, 2012 4:23 pm
by xfratboy
what the heck is aplomb?

here's the code I have thus far but I'm guessing I'd need to add your sorting method somewhere in this.

Code: Select all

On MouseUp
   -- draws a line across the middle of the stack
   put "0" &","&trunc(.5 * the height of stack SineWave) & CR into tmp
   put the width of this stack &","&trunc(.5 * the height of stack SineWave) after tmp
   set the points of grc line1 to tmp
   -- end draw line
   put empty into plotpoints
   put "0" & "," & trunc(.5 * the height of stack SineWave) into startPoint
   put trunc(.25 * the height of stack SineWave) into amplitude --changeable
   put 1 into hScale --changeable
   put 2 into angFreq
   put trunc(the width of stack SineWave) into theWidth
   put trunc(the height of stack SineWave /2) into theHeight
   repeat with y = 0 to trunc(the width of stack SineWave)
      put hScale * (y) into xPoint 
      put trunc(sin(toDegrees(angFreq * y)) * amplitude) + item 2 of startPoint into yPoint
      put xPoint & "," & ypoint & CR after plotPoints	
      if xPoint > theWidth and yPoint > theHeight then exit repeat
      
   end repeat
   
   -- Begin hack to trim line at begining and end
   put trunc(.7 * the height of stack SineWave) into tmpVar
   put the number of lines of plotPoints into plotNumber
   repeat with i = plotNumber down to 0
      if (item 2 of line i of plotPoints) < tmpVar then
         put i into tmpLine
      else
         put i into tmpLine
         exit repeat
      end if
   end repeat
   delete line tmpLine to plotNumber of plotPoints
   put trunc(.7 * the height of stack SineWave) into tmpVar
   repeat with i = 0 to the number of lines of plotPoints
      if (item 2 of line i of plotPoints )>=tmpVar then
         put i into tmpLine
         exit repeat
      end if
      
   end repeat
   delete line 0 to tmpLine of plotPoints
   -- end trim line process
   
   set the points of grc curve1 to plotPoints
   set the loc of grc curve1 to .5 * theWidth,theHeight
   
   
end MouseUp

   function toDegrees radians
        return sin(radians * pi / 180)
      end toDegrees

Re: Draw Sine/Cosine wave

Posted: Thu Jan 05, 2012 7:26 pm
by dunbarx
Here is a code fragment with aplomb:

Code: Select all

  sort sinewave  by item 2 of each
   put item 2 of line 1 of sineWave into valley
   put item 2 of last line of sineWave into peak
   repeat for each line tline in sineWave
      if item 2 of tLine contains peak or item 2 of tLine contains valley then put tLine & return after accum
   end repeat
 
   answer accum
accum contains the various points for all your max and min ordered pairs.

Craig

Re: Draw Sine/Cosine wave

Posted: Thu Jan 05, 2012 8:57 pm
by mwieder
I couldn't resist joining in the fun here. Here's your test code with some maxima and minima plotting. Your point-generating routine seems to be generating more points than necessary, but otherwise this seems workable. Nice job on scaling the graph.