Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript get point of line intersection

// line intercept math by Paul Bourke http://paulbourke.net/geometry/pointlineplane/
// Determine the intersection point of two line segments
// Return FALSE if the lines don't intersect
function intersect(x1, y1, x2, y2, x3, y3, x4, y4) {

  // Check if none of the lines are of length 0
    if ((x1 === x2 && y1 === y2) || (x3 === x4 && y3 === y4)) {
        return false
    }

    denominator = ((y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1))

  // Lines are parallel
    if (denominator === 0) {
        return false
    }

    let ua = ((x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3)) / denominator
    let ub = ((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)) / denominator

  // is the intersection along the segments
    if (ua < 0 || ua > 1 || ub < 0 || ub > 1) {
        return false
    }

  // Return a object with the x and y coordinates of the intersection
    let x = x1 + ua * (x2 - x1)
    let y = y1 + ua * (y2 - y1)

    return {x, y}
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: Sending an Ajax request before form submit 
Javascript :: nice react native shadow 
Javascript :: mui switch colours 
Javascript :: cypress get selected dropdown value 
Javascript :: js how to remove # from any url using js 
Javascript :: javascript countdown timer 
Javascript :: jquery move element to another without losing events 
Javascript :: json array to string in postgresql 
Javascript :: useeffect hook react 
Javascript :: react-router-dom navlink active 
Javascript :: how to remove key value pair from object js 
Javascript :: how to add alternate image in img tag in react 
Javascript :: check value exist in array javascript 
Javascript :: add array of object to state react 
Javascript :: jquery offsetheight 
Javascript :: remove element from array in usestate 
Javascript :: .ignore file nodejs 
Javascript :: install aos angular 10 
Javascript :: convert new date to minutes number javascript 
Javascript :: json.stringify formatting 
Javascript :: jquery if .val is blank 
Javascript :: vue js countdown timer 
Javascript :: sort object by key value js 
Javascript :: javascript integer to string 
Javascript :: angular access service in console 
Javascript :: how to preview a pdf document in react 
Javascript :: react navigation header background color 
Javascript :: check if string contains at least one number javascript 
Javascript :: javascript nth root 
Javascript :: install node on ubuntu and debian 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =