Created
April 7, 2017 14:57
-
-
Save mvbattan/b5421296ca370ffe2f6b20e2790783cc to your computer and use it in GitHub Desktop.
pointUtils.js
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
import React from 'react'; | |
export const Point = (x, y) => { | |
return { x, y }; | |
}; | |
export const dist = (pointA, pointB) => { | |
return Math.sqrt( | |
(pointA.x - pointB.x) * (pointA.x - pointB.x) + | |
(pointA.y - pointB.y) * (pointA.y - pointB.y) | |
); | |
}; | |
export const diff = (pointA, pointB) => { | |
return Point(pointB.x - pointA.x, pointB.y - pointA.y); | |
}; | |
export const add = (pointA, pointB) => { | |
return Point(pointA.x + pointB.x, pointA.y + pointB.y); | |
}; | |
export const angle = (pointA, pointB) => { | |
const euclideanDistance = dist(pointA, pointB); | |
if (!euclideanDistance) { | |
return 0; | |
} | |
return Math.asin((pointB.y - pointA.y) / euclideanDistance); | |
}; | |
export const pointPropTypes = { | |
x: React.PropTypes.number.isRequired, | |
y: React.PropTypes.number.isRequired | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment