package com.logicbig.example.math;
public class CosExample {
public static void main(String... args) {
findCosine(180);
findCosine(90);
findCosine(60);
findCosine(30);
findCosine(0);
}
private static void findCosine(double angleInDegree) {
double angleInRadians = Math.toRadians(angleInDegree);
double cos = Math.cos(angleInRadians);
System.out.printf("Cos of angle %s = %1.3f%n", angleInDegree, cos);
}
}
Output
Cos of angle 180.0 = -1.000
Cos of angle 90.0 = 0.000
Cos of angle 60.0 = 0.500
Cos of angle 30.0 = 0.866
Cos of angle 0.0 = 1.000
package com.logicbig.example.math;
public class CosGraphExample {
public static void main(String... args) {
int scale = 10;
for (double i = 0; i <= Math.PI * 2; i += 0.3) {
double cos = Math.cos(i);
int cosValue = (int) Math.round(cos * scale);
if (cosValue < 0) {
int spaces = scale - Math.abs(cosValue);
System.out.print(" ".repeat(spaces) + "+");
System.out.println(" ".repeat(scale - spaces - 1) + "|");
} else {
System.out.print(" ".repeat(scale) + "|");
System.out.println(" ".repeat(cosValue) + "+");
}
}
}
}
Output
| +
| +
| +
| +
| +
| +
+ |
+ |
+ |
+ |
+ |
+ |
+ |
+ |
+ |
+ |
| +
| +
| +
| +
| +