我正在寻找一种计算两条线的交点的方法,我有一个javascript方法可以正常使用样本x和y
//Start of linear part
window.linear = {
getSlope: function(x1, y1, x2, y2) {
if (x1 == x2) return false;
return (y1 - y2) / (x1 - x2);
},
getYInt: function(x1, y1, x2, y2) {
if (x1 === x2) return y1 === 0 ? 0 : false;
if (y1 === y2) return y1;
return y1 - this.getSlope(x1, y1, x2, y2) * x1;
},
getXInt: function(x1, y1, x2, y2) {
var slope;
if (y1 === y2) return x1 == 0 ? 0 : false;
if (x1 === x2) return x1;
return (-1 * ((slope = this.getSlope(x1, y1, x2, y2)) * x1 - y1)) / slope;
},
getInt: function(x11, y11, x12, y12, x21, y21, x22, y22) {
var slope1, slope2, yint1, yint2, intx, inty;
// check if either of the points are the same. if so that's our intersection
if (x11 == x21 && y11 == y21) return [x11, y11];
if (x12 == x22 && y12 == y22) return [x12, y22];
// get slope: (y1 - y1) / (x1 - x2)
slope1 = this.getSlope(x11, y11, x12, y12);
slope2 = this.getSlope(x21, y21, x22, y22);
// if both lines have the same slope, they are paralell; never touch.
if (slope1 === slope2) return false;
// get y-intersection: y - slope * x
yint1 = this.getYInt(x11, y11, x12, y12);
yint2 = this.getYInt(x21, y21, x22, y22);
// check to see if both lines have the same yintcept, and if so, return the point
if (yint1 === yint2) return yint1 === false ? false : [0, yint1];
// if one of the lines doesn't have a slope:
if (slope1 === false) return [y21, slope2 * y21 + yint2];
if (slope2 === false) return [y11, slope1 * y11 + yint1];
//if both lines have a slop and y intercept, calulate the x-intercept:
// (slope1 * x1 + yint1 - yint2) / slope2;
intx = (slope1 * x11 + yint1 - yint2) / slope2;
// calculate the y-intercept, and return an array:
return [intx, slope1 * intx + yint1];
}
}
// END OF linear
这就是它。但是当我从铯中添加一个样本点时,它给出了错误的答案。 我认为这是因为光辉和地理对话。任何人都知道如何解决它?
这是我的样本行的两点
var start=new Cesium.Cartesian3(0.7007890932991216, -1.3246378751475405, 0)
var end=new Cesium.Cartesian3(0.7007917595851312, -1.3246388181033268, 0)
这是另一条线
var st=new Cesium.Cartesian3(0.7007909619371219, -1.3246425889783457, 0)
var en=new Cesium.Cartesian3(0.7007881826635102, -1.3246415011312433, 0)
这就是我使用它的方式
var e = linear.getInt(start.x,start.y,end.x,end.y,st.x,st.y,en.x, en.y)||["n/a","n/a"];
它给了我错误的观点...我认为我必须进行一些审判,因为积分是光芒四射的......是否可以帮助我?