Skip to content

Instantly share code, notes, and snippets.

@mvbattan
Created April 7, 2017 14:57
Show Gist options
  • Save mvbattan/b5421296ca370ffe2f6b20e2790783cc to your computer and use it in GitHub Desktop.
Save mvbattan/b5421296ca370ffe2f6b20e2790783cc to your computer and use it in GitHub Desktop.
pointUtils.js
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