Hi,
I am looking for a way to calculate the intersection point for two lines,I have a javascript method which works fine with sample x and 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
``
this is it…But When I add a sample point from cesium it gives wrong answers.
I think it is because of radiance and geographic convetations…Does any one has any idea how to fix it?
this is two point of my sample line
var start=new Cesium.Cartesian3(0.7007890932991216, -1.3246378751475405, 0)
var end=new Cesium.Cartesian3(0.7007917595851312, -1.3246388181033268, 0)
``
and this is another line
var st=new Cesium.Cartesian3(0.7007909619371219, -1.3246425889783457, 0)
var en=new Cesium.Cartesian3(0.7007881826635102, -1.3246415011312433, 0)
``
this is how I am using it
var e = linear.getInt(start.x,start.y,end.x,end.y,st.x,st.y,en.x, en.y)||["n/a","n/a"];
``
It gives me wrong point…I think I must perform some convtration because points are in radiant…right?can some one help me with it?