JAVA学习笔记 || 使用Point 类的注意事项
- 在定义类型的实例变量过程中,若声明Point 类的属性,则需要在声明的同时分配空间(new Point());
public class Myline {
Point X = new Point();//属性:线段的两个端点X,Y
Point Y = new Point();
}
2.写一个求点到线段的距离的方法用到余弦定理
void distance(Point k,Myline j){//计算线段外一点到线段的距离
double S; //定义一个局部变量来表示面积
double temp;//定义一个局部临时变量来返回最终值
double c = Math.sqrt(Math.pow(j.X.x - j.Y.x,2) + Math.pow(j.X.y - j.Y.y,2));
double a = Math.sqrt(Math.pow(k.x - j.Y.x,2) + Math.pow(k.y - j.Y.y,2));
double b = Math.sqrt(Math.pow(j.X.x - k.x,2) + Math.pow(j.X.y - k.y,2));
double cos =(Math.pow(b,2) + Math.pow(c,2) - Math.pow(a,2)) / (2 * b *c); //此处记得加括号!
double sin = Math.sqrt(1- Math.pow(cos,2));
S = 0.5 * b * c * sin; //问题:此处0.5用 1/2 为何不行?
temp = 2 * S / c;
if(S == 0) System.out.println("k在线段或线段所在的直线上");
else System.out.println("点到线段的距离为" + temp);
}
设计过程中遇到返回值为NaN的问题
原因是:数值越界异常
btw:第一次写博客,很多功能还不会用,有点难看嘤嘤嘤,见谅。