Created
April 4, 2020 17:11
-
-
Save wan2land/d96c4cbbfcf054265397dd20e419072e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| export function average(...points) { | |
| if (points.length === 0) { | |
| return { x: 0, y: 0 } | |
| } | |
| return { | |
| x: points.reduce((carry, p) => carry + p.x, 0) / points.length, | |
| y: points.reduce((carry, p) => carry + p.y, 0) / points.length, | |
| } | |
| } | |
| export function intersect(p1, r1, p2, r2) { | |
| // a1 * x + b1 + y + c1 = 0 | |
| // a2 * x + b2 + y + c2 = 0 | |
| if (r1 === r2) { | |
| r2 += Number.EPSILON | |
| } | |
| const a1 = Math.floor(Math.sin(r1) * 10000) / 10000 | |
| const a2 = Math.floor(Math.sin(r2) * 10000) / 10000 | |
| const b1 = Math.floor(Math.cos(r1) * 10000) / -10000 | |
| const b2 = Math.floor(Math.cos(r2) * 10000) / -10000 | |
| const c1 = (a1 * p1.x + b1 * p1.y) * -1 | |
| const c2 = (a2 * p2.x + b2 * p2.y) * -1 | |
| if (b1 === 0 && b2 === 0) { | |
| throw new Error('구할 수 없습니다.') | |
| } | |
| if (b1 === 0) { | |
| return { | |
| x: -c1 / a1, | |
| y: (a2 * c1) / (a1 * b2) - c2 / b2, | |
| } | |
| } | |
| if (b2 === 0) { | |
| return { | |
| x: (a1 * c2) / (a2 * b1) - c1 / b1, | |
| y: -c2 / a2, | |
| } | |
| } | |
| return { | |
| x: (b1 * c2 - b2 * c1) / (a1 * b2 - a2 * b1), | |
| y: (b1 * c2 - b2 * c1) / (a1 * b2 - a2 * b1) * (a1 / b1 * -1) - (c1 / b1), | |
| } | |
| } | |
| export function projection(from, p1, p2) { | |
| return intersect(from, Math.atan2(p2.y - p1.y, p2.x - p1.x) + Math.PI / 2, p2, Math.atan2(p2.y - p1.y, p2.x - p1.x)) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment