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

Draw Sine/Cosine wave

Post by xfratboy » Thu Dec 22, 2011 8:49 pm

I'm needing some help figuring out how to plot a freehand graphic in the shape of a sin/cos wave pattern. I'd need the graphic to run the width and height of the stack. Any suggestions? Thanks

edit: or maybe someone would know of an 8th grade math class that might be open for an old dumb fart like myself to audit.

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

Re: Draw Sine/Cosine wave

Post by dunbarx » Thu Dec 22, 2011 9:59 pm

Real fast, and I am sure that there are prettier and cleverer tools out there, but you did ask for the 8th grade version.

Create a curve graphic. In a button put

Code: Select all

on mouseUp
   put "150,150" into startPoint
   put 10 into amplitude --changeable
   put 2 into hScale --changeable
   repeat with y = 1 to 100 
       put hScale * (item 1 of startpoint + y) & "," & trunc(sin(y) * amplitude) + item 2 of startPoint & return after sinewave
   end repeat
   set the points of grc 1 to  sinewave
end mouseUp
You can now modify and scale as you need to.

Craig Newman

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

Re: Draw Sine/Cosine wave

Post by xfratboy » Wed Dec 28, 2011 11:56 pm

Well, this is a good start. Thanks. Here's my dilemma:
- I need the wave to repeat 8~10 times
- I need the wave pattern to take up just the middle third of the stack. So, the amplitude I'm guess would be .25*(the height of this stack)
- I need the curve to be smooth.
- I need to win the lottery

Here's what I've got now:

Code: Select all

on mouseUp
   put "0,"& (.5* the height of this stack) into startPoint
   put .25*the height of this stack into amplitude --changeable
   put 5 into hScale --changeable
   repeat with y = 0 to 400
      put hscale *(item 1 of startpoint + y) & "," & trunc(sin(y) * amplitude) + item 2 of startPoint & return after sinewave
   end repeat
   set the points of grc 1 to  sinewave
end mouseUp

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

Re: Draw Sine/Cosine wave

Post by dunbarx » Thu Dec 29, 2011 10:24 pm

OK. We move to the 10th grade. The sine function in LC assumes radians. This is a bit coarser than we want, so we will convert the argument to degrees with a simple function. This will smooth out the curve. Another factor, the angular frequency "angFreq" must now be included so you have control over the number of cycles displayed:

Code: Select all

on mouseUp
      put "0,"& (.5* the height of this stack) into startPoint
      put .25*the height of this stack into amplitude --changeable
   put 3 into angFreq --changeable
      put 1 into hScale --changeable
      repeat with y = 0 to 400
            put hscale *(item 1 of startpoint + y) & "," & trunc(sin(toDegrees(angFreq * y)) * amplitude) + item 2 of startPoint & return after sinewave
      end repeat
      set the points of grc 1 to  sinewave
end mouseUp

function toDegrees radians
    return sin(radians * pi / 180)
  end toDegrees
Change the value of "angFreq" as needed and you should be good to go.

Craig Newman

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

Re: Draw Sine/Cosine wave

Post by xfratboy » Fri Dec 30, 2011 4:30 am

Thanks Craig. I think I'm missing something? When I build this it just has a blank stack (no graphic is drawn). When I check the points of the grc it shows:

5,1


5,3


5,5


5,7

etc...
What I've figured out, is that If I set the width and height of the stack to 430 & 367 respectively it seems to reproduce the problem. However, if I randomly resize the stack a few times and try again, it will eventually draw the graphic. Yet, if I set it back to width = 430 height = 367 then it stops working again.
see attached..
sine.zip
(1.21 KiB) Downloaded 325 times

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

Re: Draw Sine/Cosine wave

Post by dunbarx » Fri Dec 30, 2011 8:01 pm

Just lack of thorough testing. The "points" have to be integers. When you set the value of your "startPoint", you sometimes get a value with a decimal component.

Change line 2 to:

put "0,"& trunc((.5* the height of this stack)) into startPoint

That is why some stack heights work and some do not. The halfway point is an integer only now and then.

Craig Newman

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

Re: Draw Sine/Cosine wave

Post by xfratboy » Mon Jan 02, 2012 8:03 pm

Awesome! Thanks! That works. Can we move to 11th grade now? How would I determine the center point of the "peaks" and "valleys" of the curve. If I could determine those coordinates, I'd like to then put a line or dot at those points like in this picture (attached). When I loop through the points of the curve I can get the highs and lows but there may be 2 or 3 points on the same y-axis. Can you help me think of a way to pinpoint the specific coordinate for each of the peaks and valleys? Hope this makes sense.
curve.jpg
curve.jpg (10.54 KiB) Viewed 13234 times

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

Re: Draw Sine/Cosine wave

Post by dunbarx » Mon Jan 02, 2012 11:32 pm

Hi.

Read up on the "average" function, the "max" function and the "min" function. You already have your data points. Time to think like a ninth grader.

Craig Newman

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

Re: Draw Sine/Cosine wave

Post by xfratboy » Tue Jan 03, 2012 5:36 am

I'm pretty sure I understand the min,max, and avg functions. However, these only give me the min, max, and avg for the the entire curve graphic. I'm perplexed.

Code: Select all

repeat with i = 1 to the number of lines of points in grc Curve1
put item 2 of line i of the points of grc Curve1 & Comma after templist

end repeat

answer "Minimum:"min(templist) & "Maximum:"& max(templist)

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

Re: Draw Sine/Cosine wave

Post by dunbarx » Tue Jan 03, 2012 3:18 pm

Is this not what you need? The min and max give you the peaks, and the average would give you the baseline. No?

Craig Newman

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

Re: Draw Sine/Cosine wave

Post by xfratboy » Tue Jan 03, 2012 10:08 pm

I don't think this would give me what I want. I guess I'm not making sense. I'm trying to get the coordinates for each peak and each valley in the curve.

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

Re: Draw Sine/Cosine wave

Post by dunbarx » Tue Jan 03, 2012 11:35 pm

But in a simple sine wave, all the peaks and all the valleys have the same value:

f(x) = A sin(kx-c)

A is the amplitude, proportional to the trigonometric, and has a fixed value. You can sort of see this with the curves you are getting, no? You would have to be dealing with much more complex waves, or mixtures of waves, to see different peak values.

Still not what you had in mind?

Craig Newman

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

Re: Draw Sine/Cosine wave

Post by dunbarx » Wed Jan 04, 2012 10:04 pm

Oh.

Did you mean the x,y coordinates of EACH peak and valley? Right, that is exactly what you said.

If you place all the points, which are comma separated pairs of numbers, you can see all the min and max values as they relate to the x values. You can also get the average, and find all those values as well in the list (or at least the value that comes closest). Are you able to write a script that extracts these? I can think of several ways to do it. Check out the "filter" command. Can you sort with aplomb? Write back if you are stumped.

Craig Newman

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

Re: Draw Sine/Cosine wave

Post by xfratboy » Thu Jan 05, 2012 6:14 am

Yes, that's right. The x,y for each peak and valley. I'm sure there's some way to do this but I'm not coming up with an easy solution. I'm not sure I understand your question: "Can you sort with aplomb?"

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

Re: Draw Sine/Cosine wave

Post by dunbarx » Thu Jan 05, 2012 2:33 pm

You have all your data in the variable "sineWave" as a list of ordered pairs "x,y". Sort this list by item 2 of each. Looking at the y values, this will give you all your minimum ordered pairs in the beginning of the list, which are your valleys, and all your maximum ordered pairs at the end, which are your peaks. You already have the y value of the baseLine, which is item 2 of startPoint.

Can you script this with aplomb?

Craig Newman

Post Reply