## Wednesday, June 8, 2011

### Plotting a Sine Curve

Reusing the Wheel
We are going to use the Visualizer in the previous article, Plotting Graph

All we have to do is to prepare a new data file named data.txt, which will contain the values of sin(x) for x=0,15,30,45,60... (degree).

data.txt
A few lines of the data file is shown, you may use a line editor to calculate the value of sin(x) up to 360 degree. Or you may use an excel spread sheet to generate the data.

```0
0.2588
0.5
0.7071
0.8660
...
```

Automatic data generation
If you don't like to use a spread sheet program, you may write a simple java program to generate the data. A simple example is shown below :

```/******************************************************************************
* File : DataWriter.java
* Author : http://java.macteki.com/
* Description :
*   Calculate y=sin(x) for x=0,15,30,..., 360
* Tested with : JDK 1.6
******************************************************************************/
class DataWriter
{
public static void main(String[] args) throws Exception
{
double pi_over_180 = 3.141592654/180;
for (double degree=0; degree<=360; degree+=15)
{
double radian = degree*pi_over_180;
double sine = Math.sin(radian);
System.out.println(sine);
}
}
}
```
Of course you would need to redirect the program output :
```javac DataWriter.java
java DataWriter > data.txt
```

Plotting the Curve

Once you have data.txt ready, you may invoke the Visualizer class to plot the curve. The Visualizer class is defined in the article Plotting Graph

```java Visualizer
```