What horizontal coordinate of the panel will correspond to x=0? 0
What horizontal coordinate of the panel will correspond to x=2*Math.PI? 599
To use the drawLine()
method, the endpoints of
the line need to be positive integers,
and we want the graph to fill the drawing area.
But what we want to graph consists floating point values of x from zero to two pi,
and floating point values of sin(x) from minus one to plus one.
To deal with this problem you need to scale the values
into JPanel
coordinates.
Say that the drawing area is 600 pixels wide,
and that will represent the range of 0.0 to 2*PI.
So we have:
actual x value | integer x' to fit panel width |
---|---|
0.0 | 0 |
2*PI | 599 |
If the applet width is 600, then the largest x value is 599 (because x coordinates start at zero).
The floating point number x
can be changed into the
correct integer x'
by using a linear equation:
x' = x * 599/(2*PI)
This may look awful, but it is just algebra.
Err... just algebra.... Maybe we better check if it works.
Fill in the following table by computing x'
by using the formula.
actual x value | x' = x * 599/(2*PI) |
---|---|
0.0 | |
2*PI |