Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java Swing Eclipse
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 31-Aug-12 0:11am
Edited 31-Aug-12 2:53am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

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.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 195
1 Afzaal Ahmad Zeeshan 152
2 ProgramFOX 130
3 Maciej Los 130
4 den2k88 80
0 OriginalGriff 6,564
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,228
3 Manas Bhardwaj 4,657
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 31 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100