explaing calculation in sine graph

14 views
Skip to first unread message

Paul Cowan

unread,
May 29, 2016, 2:32:58 AM5/29/16
to d3-js
Hi,

If you look at this nicely executed sinewave, there contains the following code that determines some values for the x values on each tick of the animation:

  for (i = 0; i < 44; i++) {
    data.push(i * 10 / 84);
  }

I think 44 is really just the available width but is there any reasoning behind this code:

data.push(i * 10 / 84);

I am confused as to the meaning of this calculation. 

Tito

unread,
May 29, 2016, 1:09:50 PM5/29/16
to d3-js
for (i = 0; i < 44; i++) {
    data.push(i * 10 / 84);
  }

The above is a for loop used to populate the array data[] which in javascript is a zero based array. Think of data.push as data.insertdata
i=0 
  data.push(0*10/84) = data[0] = 0
i=1
  data.push(1*10/84) = data[1] = 0.119047619047619
i=2
   data.push(2*10/84) = data[2] = 0.2380952380952381
i=3
  data.push(3*10/84) = data[3] =0.3571428571428571
if you change 44 to 22 you will notice the sinewave length changes

Paul Cowan

unread,
May 29, 2016, 2:18:16 PM5/29/16
to d3...@googlegroups.com
I know what a javascript array is and I know what push etc. means.

I also stated that I thought it was the available width.

Maybe I was not clear in my question.

I meant why is it pushing (i * 10 / 84).

What does this calculation mean?

Why is it multiplying i by 10 divided by 84.

This seems unusually precise for some reason.

Cheers

Paul Cowan

Cutting-Edge Solutions (Scotland)


--
You received this message because you are subscribed to a topic in the Google Groups "d3-js" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/d3-js/qpkhay5jY2E/unsubscribe.
To unsubscribe from this group and all its topics, send an email to d3-js+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tito

unread,
May 30, 2016, 11:28:32 AM5/30/16
to d3-js
ah sorry Paul.

I think the reason for that division is so that you do not get jagged sinewave

var data = [];

for (i = 0; i < 44; i++) {
    data.push(i * 10 / 10);
  }

  console.log(data); gives you integers whereas 10/84 consistently (mostly) floats which makes for smoother sinewaves

check this out

Reply all
Reply to author
Forward
0 new messages