/angle

Library for calculating angles in javascript

Primary LanguageTypeScriptMIT LicenseMIT

yy-angle

Library of useful functions for working with angles in javascript.

rationale

I wanted to make my life much easier when working with angles since they usually hurt my brain.

Installation

include angle.js in your project or add to your workflow

npm i yy-angle

API

/** @constant {number} */
const UP = Math.PI / 2
const DOWN = 3 * Math.PI / 2
const LEFT = Math.PI
const RIGHT = 0

const NORTH = UP
const SOUTH = DOWN
const WEST = LEFT
const EAST = RIGHT

const PI_2 = Math.PI * 2
const PI_QUARTER = Math.PI / 4
const PI_HALF = Math.PI / 2

export const NORTH_WEST = (NORTH + WEST) / 2
export const NORTH_EAST = (NORTH + EAST) / 2
export const SOUTH_WEST = (SOUTH + WEST) / 2
export const SOUTH_EAST = (SOUTH + EAST) / 2

/**
 * converts from radians to degrees (all other functions expect radians)
 * @param {number} radians
 * @return {number} degrees
 */
function toDegrees(radians)

/**
 * converts from degrees to radians (all other functions expect radians)
 * @param {number} degrees
 * @return {number} radians
 */
function toRadians(degrees)

/**
 * returns whether the target angle is between angle1 and angle2 (in radians)
 * (based on: http://stackoverflow.com/questions/11406189/determine-if-angle-lies-between-2-other-angles)
 * @param {number} target angle
 * @param {number} angle1
 * @param {number} angle2
 * @return {boolean}
 */
function isAngleBetween(target, angle1, angle2)

/**
 * returns +1 or -1 based on whether the difference between two angles is positive or negative (in radians)
 * @param {number} target angle
 * @param {number} source angle
 * @return {number} 1 or -1
 */
function differenceAnglesSign(target, source)

/**
 * returns the normalized difference between two angles (in radians)
 * @param {number} a - first angle
 * @param {number} b - second angle
 * @return {number} normalized difference between a and b
 */
function differenceAngles(a, b)

/**
 * returns a target angle that is the shortest way to rotate an object between start and to--may choose a negative angle
 * @param {number} start
 * @param {number} to
 * @return {number} shortest target angle
 */
function shortestAngle(start, to)

/**
 * returns the normalized angle (0 - PI x 2)
 * @param {number} radians
 * @return {number} normalized angle in radians
 */
function normalize(radians)

/**
 * returns angle between two points (in radians)
 * @param {Point} [point1] {x: x, y: y}
 * @param {Point} [point2] {x: x, y: y}
 * @param {number} [x1]
 * @param {number} [y1]
 * @param {number} [x2]
 * @param {number} [y2]
 * @return {number} angle
 */
function angleTwoPoints(/* (point1, point2) OR (x1, y1, x2, y2) */)

/**
 * returns distance between two points
 * @param {Point} [point1] {x: x, y: y}
 * @param {Point} [point2] {x: x, y: y}
 * @param {number} [x1]
 * @param {number} [y1]
 * @param {number} [x2]
 * @param {number} [y2]
 * @return {number} distance
 */
function distanceTwoPoints(/* (point1, point2) OR (x1, y1, x2, y2) */)

/**
 * returns the squared distance between two points
 * @param {Point} [point1] {x: x, y: y}
 * @param {Point} [point2] {x: x, y: y}
 * @param {number} [x1]
 * @param {number} [y1]
 * @param {number} [x2]
 * @param {number} [y2]
 * @return {number} squared distance
 */
function distanceTwoPointsSquared(/* (point1, point2) OR (x1, y1, x2, y2) */)

/**
 * returns the closest cardinal (N, S, E, W) to the given angle (in radians)
 * @param {number} angle
 * @return {number} closest cardinal in radians
 */
function closestAngle(angle)

/**
 * returns the closest cardinal w/diagonal (N, S, E, W, NE, NW, SE, SW) to the given angle (in radians)
 * @param {number} angle
 * @return {number} closest cardinal in radians
 */
export function closestAngle2(angle: number)

/**
 * checks whether angles a1 and a2 are equal (after normalizing)
 * @param {number} a1
 * @param {number} a2
 * @param {number} [wiggle] return true if the difference between the angles is <= wiggle
 * @return {boolean} a1 === a2
 */
function equals(a1, a2, wiggle)

/**
 * return a text representation of the cardinal direction
 * @param {number} angle
 * @returns {string} UP, DOWN, LEFT, RIGHT, or NOT CARDINAL
 */
function explain(angle)

/**
 * returns a text representation of the closest cardinal w/diagonal (N, S, E, W, NE, NW, SE, SW) to the given angle
 * @param {number} angle
 * @return {string} NORTH, WEST, EAST, SOUTH, NORTH_EAST, NORTH_WEST, SOUTH_EAST, SOUTH_WEST
 */
function explain2(angle)

license

MIT License (c) 2017 YOPEY YOPEY LLC by David Figatner