Math

Math Functions

- Angle Math Functions

These functions convert between angles and rectangular components. All angles are measured in degrees. Positive angles indicate counter-clockwise rotation. A heading angle of 0 degrees points straight right (east) along the positive X axis.

convertToComponents

Converts an angle and length into rectangular vector components.

name
type
default
description

angle*

number

-

the angle of counter-clockwise rotation, in degrees, where zero degrees lies along the positive X axis

length*

number

-

the length of the desired vector

Returns

array An array [x, y] containing the two vector components

let [x, y] = convertToComponents(45, 2)

convertToAngle

Converts rectangular vector components into an angle in degrees.

name
type
default
description

x*

number

-

the x coordinate of the vector

y*

number

-

the y coordinate of the vector

Returns

number The heading angle defined by the given vector, in degrees.

let angle = convertToAngle(3,4)

- Other Math Functions

pointInPolygon

Draws a single pixel.

name
type
default
description

x*

number

-

the x coordinate of the given point

y*

number

-

the y coordinate of the given point

polygon*

array

-

The polygon, as an array of [x, y] subarrays

Returns

boolean true if the point is inside the polygon, and false otherwise.

let polygon = [[10, 20], [100, 40], [50, 90]]
if (pointInPolygon(mousex, mousey, polygon)) {
  console.log("mouse is inside the polygon")
}

Last updated