I am trying to find out the line position using the coordinates. I have two lines connected with each other using StartX,StartY, EndX & EndY.
Next, I have to figure it out direction of the second line (Right, Left, Up, Down). I tried this script but, getting some unexpected results.
Here x1 is startX, y1 is startY
x2 is endX, y2 is endY for the line 1
x3 is startX, y3 is startY
x4 is endX, y4 is endY for the line 2
export const getPositionOfPipe = ( x1: number, y1: number, x2: number, y2: number, x3: number, y3: number, x4: number, y4: number ): string => {
if( y4 - y2 > 0 ){
return "Up"
}
if( y4 - y2 < 0 ){
return "Down"
}
if( x4 - x2 >= 0 ){
return "Right"
}
if( x4 - x2 <= 0 ){
return "Left"
}
return "Parallel"
}
export const getPositionOfPipe = (
x1: number, y1: number,
x2: number, y2: number,
x3: number, y3: number,
x4: number, y4: number
): string => {
const dx = x4 - x2;
const dy = y4 - y2;
const horizontal = dx > 0 ? "Right" : (dx < 0 ? "Left" : "");
const vertical = dy > 0 ? "Up" : (dy < 0 ? "Down" : "");
if (horizontal && vertical) {
return `${vertical} ${horizontal}`;
} else if (horizontal) {
return horizontal;
} else if (vertical) {
return vertical;
} else {
return "No Movement";
}
};
Hlib Lukavetskyi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.