Skip to main content

@rtorcato/js-common / geometry

geometry

Functions

distance2D()

distance2D(x1, y1, x2, y2): number

Defined in: geometry/index.ts:16

Calculates the distance between two points in 2D space.

Example

distance2D(0, 0, 3, 4) // 5
distance2D(1, 1, 1, 1) // 0

Parameters

x1

number

X coordinate of first point.

y1

number

Y coordinate of first point.

x2

number

X coordinate of second point.

y2

number

Y coordinate of second point.

Returns

number

The distance.


midpoint2D()

midpoint2D(x1, y1, x2, y2): [number, number]

Defined in: geometry/index.ts:35

Calculates the midpoint between two points in 2D space.

Example

midpoint2D(0, 0, 4, 6) // [2, 3]
midpoint2D(-2, 5, 2, 5) // [0, 5]

Parameters

x1

number

X coordinate of first point.

y1

number

Y coordinate of first point.

x2

number

X coordinate of second point.

y2

number

Y coordinate of second point.

Returns

[number, number]

The midpoint as [x, y].


angle2D()

angle2D(x1, y1, x2, y2): number

Defined in: geometry/index.ts:54

Calculates the angle (in radians) between two points in 2D space.

Example

angle2D(0, 0, 1, 0) // 0
angle2D(0, 0, 0, 1) // 1.5707963267948966 (Math.PI / 2)

Parameters

x1

number

X coordinate of first point.

y1

number

Y coordinate of first point.

x2

number

X coordinate of second point.

y2

number

Y coordinate of second point.

Returns

number

The angle in radians.


pointInRect()

pointInRect(px, py, rx, ry, rw, rh): boolean

Defined in: geometry/index.ts:68

Checks if a point is inside a rectangle.

Parameters

px

number

X coordinate of the point.

py

number

Y coordinate of the point.

rx

number

X coordinate of the rectangle's top-left corner.

ry

number

Y coordinate of the rectangle's top-left corner.

rw

number

Rectangle width.

rh

number

Rectangle height.

Returns

boolean

True if inside, false otherwise.


rectsOverlap()

rectsOverlap(ax, ay, aw, ah, bx, by, bw, bh): boolean

Defined in: geometry/index.ts:91

Checks if two rectangles overlap.

Parameters

ax

number

X of rect A.

ay

number

Y of rect A.

aw

number

Width of rect A.

ah

number

Height of rect A.

bx

number

X of rect B.

by

number

Y of rect B.

bw

number

Width of rect B.

bh

number

Height of rect B.

Returns

boolean

True if rectangles overlap.


polygonArea()

polygonArea(points): number

Defined in: geometry/index.ts:109

Calculates the area of a polygon given its vertices (Shoelace formula).

Parameters

points

[number, number][]

Array of [x, y] pairs.

Returns

number

The area (positive value).