小弟水平有限,项目中需要计算角度,就随便瞎写了个方法,希望对新人有用,不足之处也希望各路大神多多指点。共同进步!!!

不废话,直接上代码:

/**

 * JiSuan.java 是一个点(与原点方向)
 * JiSuan2.Java 是两个点(有矢量方向,AB与BA与正方向角度不同)
 * JiSuanJiaoDu.java 是从屏幕输入坐标
 */
 
/****************第一种方法******************/
public class JiSuan {
public static void main(String[] args) {
double x = -1.0;
double y = -1.73;
double m = compute(x, y);
System.out.println("m的值为" + m);
}
public static double compute(double x, double y) {
double d = 0.0;
double tan = y / x;
// System.out.println("res = " + res);
if (x == 0.0 && y == 0.0) {
d = 0.0;
} else if (x >= 0.0 && y == 0.0) {
d = 90.0;
} else if (x < 0 && y == 0) {
d = 180.0;
} else if (x == 0.0 && y < 0.0) {
d = 270.0;
} else {
d = Math.atan(tan);
}
if (x > 0.0 && y > 0.0) {
d = d * 180 / Math.PI;
} else if (x < 0.0 && y != 0.0) {
d = 180 + d * 180 / Math.PI;
} else if (x > 0.0 && y < 0.0) {
d = 360 + d * 180 / Math.PI;
}
return d;
}
}
 
/****************第二种方法******************/
 
import java.util.Scanner;
import java.lang.Math;
 
public class JiSuan2 {
public static void main(String[] args) {
double x1 = 1.0;
double y1 = 2.0;
double x2 = -1.0;
double y2 = -90;
double m = compute2(x1, y1, x2, y2);
System.out.println("这个向量与坐标正方向所成角度为:" + m + "°");
}
public static double compute2(double x1, double y1, double x2, double y2) {
double d = 0.0;
double tan = (y2 - y1) / (x2 - x1);
if (x1 == 0 && y1 == 0) {
d = compute(x2, y2);
} else if (x1 == x2 && y1 > y2) {
d = 270.0;
} else if (x1 == x2 && y1 < y2) {
d = 90.0;
} else if (x1 > x2 && y1 == y2) {
d = 180.0;
} else if (x1 < x2 && y1 == y2) {
d = 0.0;
} else if (x1 < x2 && y1 < y2) {
d = Math.atan(tan);
d = d * 180 / Math.PI;// 第一象限
} else if (x1 > x2 && y1 != y2) {
d = Math.atan(tan);
d = 180 + d * 180 / Math.PI;// 第二第三象限
} else if (x1 < x2 && y1 > y2) {
d = Math.atan(tan);
d = 360 + d * 180 / Math.PI;// 第四象限
}
return d;
}
// compute方法提供的是当其中一个点在原点时,更简便的算法
public static double compute(double x, double y) {
double dd = 0.0;
double tan = y / x;
if (x == 0.0 && y == 0.0) {
dd = 0.0;
} else if (x >= 0.0 && y == 0.0) {
dd = 90.0;
} else if (x < 0 && y == 0) {
dd = 180.0;
} else if (x == 0.0 && y < 0.0) {
dd = 270.0;
} else {
dd = Math.atan(tan);
}
if (x > 0.0 && y > 0.0) {
dd = dd * 180 / Math.PI;// 第一象限
} else if (x < 0.0 && y != 0.0) {
dd = 180 + dd * 180 / Math.PI;// 二三象限
} else if (x > 0.0 && y < 0.0) {
dd = 360 + dd * 180 / Math.PI;// 第四象限
}
return dd;
}
}
 
/****************第一种方法******************/
 
import java.util.Scanner;
import java.lang.Math;
 
public class JiSuanJiaoDu {
public static void main(String[] args) {
System.out.println("请以此输入第一个数的x、y坐标和第二个数的x、y坐标");
Scanner in = new Scanner(System.in);// 获取屏幕输入
double[] dou = new double[4];// 保存屏幕输入的两个坐标系数
for (int i = 0; i < 4; i++) {
System.out.println("请输入第" + (i + 1) + "个数:");
dou[i] = in.nextDouble();
}
System.out.println("这两个坐标是:");
System.out.println("(" + dou[0] + "," + dou[1] + ")" + "; " + "("
+ dou[2] + "," + dou[3] + ")" + "; ");
double x1 = dou[0];
double y1 = dou[1];
double x2 = dou[2];
double y2 = dou[3];
double m = compute2(x1, y1, x2, y2);
System.out.println("这个向量与坐标正方向所成角度为:" + m + "°");
}
 
public static double compute2(double x1, double y1, double x2, double y2) {
double d = 0.0;
double tan = (y2 - y1) / (x2 - x1);
if (x1 == 0 && y1 == 0) {
d = compute(x2, y2);
} else if (x1 == x2 && y1 > y2) {
d = 270.0;
} else if (x1 == x2 && y1 < y2) {
d = 90.0;
} else if (x1 > x2 && y1 == y2) {
d = 180.0;
} else if (x1 < x2 && y1 == y2) {
d = 0.0;
} else if (x1 < x2 && y1 < y2) {
d = Math.atan(tan);
d = d * 180 / Math.PI;// 第一象限
} else if (x1 > x2 && y1 != y2) {
d = Math.atan(tan);
d = 180 + d * 180 / Math.PI;// 第二第三象限
} else if (x1 < x2 && y1 > y2) {
d = Math.atan(tan);
d = 360 + d * 180 / Math.PI;// 第四象限
}
return d;
}
 
// compute方法提供的是当其中一个点在原点时,更简便的算法
public static double compute(double x, double y) {
double dd = 0.0;
double tan = y / x;
if (x == 0.0 && y == 0.0) {
dd = 0.0;
} else if (x >= 0.0 && y == 0.0) {
dd = 90.0;
} else if (x < 0 && y == 0) {
dd = 180.0;
} else if (x == 0.0 && y < 0.0) {
dd = 270.0;
} else {
dd = Math.atan(tan);
}
if (x > 0.0 && y > 0.0) {
dd = dd * 180 / Math.PI;// 第一象限
} else if (x < 0.0 && y != 0.0) {
dd = 180 + dd * 180 / Math.PI;// 二三象限
} else if (x > 0.0 && y < 0.0) {
dd = 360 + dd * 180 / Math.PI;// 第四象限
}
return dd;
}
}