Draw Sine/Cosine wave

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

xfratboy
Posts: 97
Joined: Fri Mar 05, 2010 9:09 pm

Re: Draw Sine/Cosine wave

Post by xfratboy » Thu Jan 05, 2012 4:23 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10330
Joined: Wed May 06, 2009 2:28 pm

Re: Draw Sine/Cosine wave

Post by dunbarx » Thu Jan 05, 2012 7:26 pm

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

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Draw Sine/Cosine wave

Post by mwieder » Thu Jan 05, 2012 8:57 pm

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.
Attachments
plot.zip
sinewave plotting
(5.52 KiB) Downloaded 282 times

Post Reply