Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

polygon intersection js

function lerp(A, B, t) {
    return A + (B - A) * t;
}

function getIntersection(A, B, C, D) {
    const tTop = (D.x - C.x) * (A.y - C.y) - (D.y - C.y) * (A.x - C.x);
    const uTop = (C.y - A.y) * (A.x - B.x) - (C.x - A.x) * (A.y - B.y);
    const bottom = (D.y - C.y) * (B.x - A.x) - (D.x - C.x) * (B.y - A.y);

    if (bottom != 0) {
        const t = tTop / bottom;
        const u = uTop / bottom;

        if (t >= 0 && t <= 1 && u >= 0 && u <= 1) {
            return {
                x: lerp(A.x, B.x, t),
                y: lerp(A.y, B.y, t),
                offset: t
            }
        }
    }

    return null;
}

function polysIntersect(poly1, poly2) {
    for (let i = 0; i < poly1.length; i++) {
        for (let j = 0; j < poly2.length; j++) {
            const touch  = getIntersection(
                poly1[i], 
                poly1[(i + 1) % poly1.length], 
                poly2[j], 
                poly2[(j + 1) % poly2.length]
            );
            if (touch) {
                return true;
            }
        }
    }
    return false;
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: get random item in array 
Javascript :: string variable 
Javascript :: declare 2 d vector js 
Javascript :: how to change name on tab when user goes to another tab 
Javascript :: store reference of event listener inside a element 
Javascript :: javascript change IFormFile to base64string 
Javascript :: smembers in redis 
Javascript :: Set A Function For An Element 
Javascript :: Self Invoking Functions Can Also be Used To Make Variables Global In JavaScript 
Javascript :: expected a string (for built-in components) or a class/function (for composite components) but got: undefined 
Javascript :: what are array methods in javascript 
Javascript :: fibonacci sequence array 
Javascript :: js filter example 
Javascript :: react native ios firebase push notifications not working 
Javascript :: what is local storage and session storage in javascript 
Javascript :: filter bootstrap 
Javascript :: vue js laravel tutorial 
Javascript :: how to do division in javascript 
Javascript :: remove duplicates array javascript 
Javascript :: swagger ui express 
Javascript :: adding pre tag javascript 
Javascript :: for loop in react native 
Javascript :: chrome.contextmenus 
Javascript :: white with opacity rgba to hex 
Javascript :: reference data types in javascript 
Javascript :: code cat 
Javascript :: Material-ui add circle icon 
Javascript :: for of loop in javascript 
Javascript :: insertar al inicio de un array javascript 
Javascript :: how to create my own filter in js 
ADD CONTENT
Topic
Content
Source link
Name
4+4 =