Last active
November 11, 2019 09:22
-
-
Save black-black-cat/2dab77454dd35039dc365d4e0c1040e2 to your computer and use it in GitHub Desktop.
get rotated corners
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>GistRun</title> | |
<link rel="stylesheet" href="styles.css"> | |
</head> | |
<body> | |
<h1>Hello world!</h1> | |
<script src="script.js"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getRotatedCorners (corners, angle = 0) { | |
return corners.map(v => { | |
return vRotate(v, angle) | |
}) | |
} | |
let { | |
PI, | |
cos, | |
sin, | |
min, | |
max | |
} = Math | |
function vRotate (v, angle, deg = true) { | |
if (deg) { | |
angle = angle * PI / 180 | |
} | |
let x = v[0] | |
let y = v[1] | |
let ret = [ | |
approxFix( x * cos(angle) - y * sin(angle) ), | |
approxFix( x * sin(angle) + y * cos(angle) ) | |
] | |
return ret | |
} | |
let innerCorners = getRotatedCorners([[2, 1], [-2, 1], [-2, -1], [2, -1]], 15) | |
console.log(innerCorners) | |
function approxFix (num, pow = 3) { | |
let factor = Math.pow(10, pow) | |
let fixed = Math.round(num * factor) / factor | |
let shouldFix = Math.abs(fixed - num) < 10e-8 | |
return shouldFix ? fixed : num | |
} | |
function getOuterRect (innerCorners) { | |
let xCoords = [] | |
let yCoords = [] | |
innerCorners.forEach(v => { | |
xCoords.push(v[0]) | |
yCoords.push(v[1]) | |
}) | |
let tl = [min.apply(null, xCoords), min.apply(null, yCoords)] | |
let br = [max.apply(null, xCoords), max.apply(null, yCoords)] | |
let tr = [br[0], tl[1]] | |
let bl = [tl[0], br[1]] | |
return { | |
corners: [br, bl, tl, tr], | |
width: br[0] - tl[0], | |
height: br[1] - tl[1] | |
} | |
} | |
console.log(getOuterRect(innerCorners)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* todo: add styles */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment