how do i implement the cosine trig function in java?
Printable View
how do i implement the cosine trig function in java?
Do you need to make an implementation, or do you just need a cosine function?
If you just need cosine for somthing else, then check the Math class. It contains a cos method.
Unless you intend to draw the graph using Graphics2D, the math class contains the sine and cosine functions. They do however only work with radians, so you'll need a converted variable if you intend to have degrees as input.
Example of the cosine:
edit: changed from int to doubleCode :
double x = Math.cos(Math.PI/6);
API:
Math (Java 2 Platform SE v1.4.2)
Hope that helps.
casting to integers for cosine/sine is a bad idea. You'll almost always get zero because of truncation.
Ah, didn't know that. Noted to self!
Edit: Come to think of it, I knew it but didn't consider the fact that int doesn't allow decimals which is crucial in most sine/cosine functions.