Skip to content

Instantly share code, notes, and snippets.

@zalesky
Created October 7, 2021 08:03
Show Gist options
  • Save zalesky/016d8829f5f428e8745d62309839d216 to your computer and use it in GitHub Desktop.
Save zalesky/016d8829f5f428e8745d62309839d216 to your computer and use it in GitHub Desktop.
/**
* Checks if line is crossed by rectangle
* @param x1 most left X of rectangle
* @param y1 most left Y of rectangle
* @param a side length of rectangle
* @param b side length of rectangle
* @param degree is an angle to X axis
* @param borderX - x coordinate of a line to check crossing
* @return {boolean} Returns true if rectangle that described with params x1, y1, a, b, degree is crossing the line.
*/
const isCrossed = (x1, y1, a, b, degree, borderX) => {
const radian1 = degreeToRadian(degree);
const radian2 = degreeToRadian(degree-90);
const { cos, sin } = Math;
const x2 = x1 + a * cos(radian1);
const y2 = y1 + a * sin(radian1);
const x3 = x1 + b * cos(radian2);
const y3 = y1 + b * sin(radian2);
const x4 = x3 + a * cos(radian1);
const y4 = y3 + a * sin(radian1);
console.log({ x1, y1 });
console.log({ x2, y2 });
console.log({ x3, y3 });
console.log({ x4, y4 });
console.log('______________');
return Math.max(x1, x2, x3, x4) >= borderX;
};
const degreeToRadian = d => d * Math.PI / 180;
isCrossed(0,0,1,1,0, 1)
isCrossed(0,0,1,1,45, 2)
isCrossed(0,0,2,3,0, 2)
isCrossed(0,0,4,3,37, 4.9)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment