12,401,324 members (33,751 online)
Rate this:
See more:
I have built a GUI on which I want to show sine and cosine graphs.

The code for drawing the graph is :
```while(x1 < getWidth()) {
x2 = x1 + 1;
y2 = (int)(340 - ((Math.sin(1.5*i*Math.PI/180) * 80)));
g2.drawLine(x1, y1, x2, y2);
x1 = x2;
y1 = y2;
i++;
}
x1 = 300;   // x co-ordinate of interpolated origin
y1 = 340;   // y co-ordinate of interpolated origin

while(x1 > 0) {
x2 = x1 - 1;
i = -1;
y2 = (int)(340 - ((Math.sin(1.5*i*Math.PI/180) * 80)));
g2.drawLine(x1, y1, x2, y2);
x1 = x2;
y1 = y2;
i--;
}```
1.5(degrees) is the scale on my x-axis(relative to the GUI co-ordinates). and 1/80 i.e, 0.0125 is the scale on y-axis(relative to the GUI co-ordinates). The code for first part (positive angles) is working correctly. But all I get is a straight line for the second part(negative angles). Can you please point out where am I getting wrong in my code for second part.
Thank you.
Posted 30-Aug-12 23:11pm
Updated 31-Aug-12 1:53am
v2

Rate this:

## Solution 2

Just adding phases solved my problem, it has been a long time since I used trigonometry or the"ALL SILVER TEA CUPS" idiom. For sine wave I have added a phase of PI/2 and for cosine wave I have added a phase of (1.5)*PI.

Top Experts
Last 24hrsThis month
 OriginalGriff 260 Karthik Bangalore 180 Richard Deeming 155 F-ES Sitecore 135 ppolymorphe 105
 OriginalGriff 7,023 ppolymorphe 2,950 Karthik Bangalore 2,837 Richard MacCutchan 2,125 F-ES Sitecore 2,122