我正在研究计算几何项目,其中包含Point,Line,Triangle等类。我将线条存储为yint和slope,但这给我带来垂直线问题,如下面的问题所示。我必须保持斜坡的使用,但是如果你认为这适合于这种情况,我可以转换到度数。
下面是一个获取三角形的所有高度的数组的方法,在三角形类中实现。这是行不通的。我很确定这是由于无限的斜坡,但并非完全。有什么想法吗?
//sides is an array of LineSegs {AB, BC, CA}
public Line[] getLineAltitudes() {
Line[] returner = new Line[3];
for (int i=0; i<3; i++) {
//decides which point is opposite to lineseg
int pointgot = 0;
if (i == 0) pointgot = 2;
else if (i == 1) pointgot = 0;
else pointgot = 1;
//gets the perpendicular to a side
Double newSlope = (double)1/sides[i].getSlope();
System.out.println(newSlope);
if (newSlope.isInfinite()) newSlope = (double) 0;
//constructs a line using the (working) constructor Line(point on line, slope)
returner[i] = new Line(getPoints()[pointgot],newSlope);
}
return returner;
}
答案 0 :(得分:0)
要处理垂直斜坡,使用角度测量而不是斜率将允许您在没有此问题的情况下执行此操作。当x值发生零变化时,请考虑使用java.lang.Math.atan2(double y, double x)来获得垂直角度。