Methods
(static) anglePnt(a_x, a_y, b_x, b_y, o_x, o_y) → {number}
Get angle from three points
Example
Geometry.anglePnt(1, 0, 0, 1, 0, 0);
//1.5707963267948966
Parameters:
Name | Type | Description |
---|---|---|
a_x |
number | x position of first point |
a_y |
number | y position of first point |
b_x |
number | x position of second point |
b_y |
number | y position of second point |
o_x |
number | x position of vertex point |
o_y |
number | y position of vertex point |
Returns:
Angle in radians
- Type
- number
(static) areaPoly(points, accurateopt) → {number}
Calculate area of a polygon (convex, concave, complex)
You can check if polygon is clockwise by check if result > 0
Example
Geometry.areaPoly([0, 0, 50, 0, 100, 50, 50, 100, 0, 100]);
//7500
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
points |
Array.<number> | array of points [x1, y1, x2, y2, ...] | ||
accurate |
boolean |
<optional> |
false
|
`true` if not accurate |
Returns:
- Type
- number
(static) areaTri(x_1, y_1, x_2, y_2, x_3, y_3, accurateopt) → {number}
Calculate area of the triangle
Example
Geometry.areaTri(0, 0, 2, 0, 1, 1);
//1
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
x_1 |
number | x position of the first vertex | ||
y_1 |
number | y position of the first vertex | ||
x_2 |
number | x position of the second vertex | ||
y_2 |
number | y position of the second vertex | ||
x_3 |
number | x position of the third vertex | ||
y_3 |
number | y position of the third vertex | ||
accurate |
boolean |
<optional> |
false
|
`true` if not accurate |
Returns:
- Type
- number
(static) boundElli(x, y, radius1, radius2, angle, returnDataopt) → {Object}
Find bounding box of an ellipse
Example
Geometry.boundElli(0, 0, 2, 1, Math.PI / 4);
//{xMin: -1.5811388300841895, yMin: -1.5811388300841895, xMax: 1.5811388300841895, yMax: 1.5811388300841895}
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
x |
number | x position of center point of the ellipse | |
y |
number | y position of center point of the ellipse | |
radius1 |
number | radius of major axis of the ellipse | |
radius2 |
number | radius of minor axis of the ellipse | |
angle |
number | rotation of ellipse in radians of the ellipse | |
returnData |
object |
<optional> |
Object to put data |
Returns:
- Type
- Object
(static) boundPoly(points, returnDataopt) → {Object}
Find bounding box of a polygon (convex, concave, complex)
Example
Geometry.boundPoly([0, 0, 50, 0, 100, 50, 50, 100, 0, 100]);
//{xMin: 0, yMin: 0, xMax: 100, yMax: 100}
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
points |
Array.<number> | array of points [x1, y1, x2, y2, ...] | |
returnData |
object |
<optional> |
Object to put data |
Returns:
- Type
- Object
(static) centroidPoly(points, returnDataopt) → {Object}
Find centroid of a polygon (convex, concave, complex)
Example
Geometry.centroidPoly([0, 0, 50, 0, 100, 50, 50, 100, 0, 100]);
//{x: 38.888888888888886, y: 50}
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
points |
Array.<number> | array of points [x1, y1, x2, y2, ...] | |
returnData |
object |
<optional> |
Object to put data |
Returns:
- Type
- Object
(static) centroidTri(x_1, y_1, x_2, y_2, x_3, y_3, returnDataopt) → {Object}
Calculate centroid point of triangle
Example
Geometry.centroidTri(0, 0, 2, 0, 1, 1);
//{x: 1, y: 0.3333333333333333}
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
x_1 |
number | x position of the first vertex | |
y_1 |
number | y position of the first vertex | |
x_2 |
number | x position of the second vertex | |
y_2 |
number | y position of the second vertex | |
x_3 |
number | x position of the third vertex | |
y_3 |
number | y position of the third vertex | |
returnData |
object |
<optional> |
Object to put data |
Returns:
returnData.x and returnData.y
- Type
- Object
(static) chevDistPnt(a_x, a_y, b_x, b_y) → {number}
Calculate Chebyshev distance of two points
Example
Geometry.chevDistPnt(0, 0, 0, 1);
//1
Parameters:
Name | Type | Description |
---|---|---|
a_x |
number | x position of first point |
a_y |
number | y position of first point |
b_x |
number | x position of second point |
b_y |
number | y position of second point |
Returns:
- Type
- number
(static) cmpPnt(a_x, a_y, b_x, b_y, accuracyopt) → {number}
Compare two points and return 1, 0 or -1
Example
Geometry.cmpPnt(1, 0, -1, 0);
//1
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
a_x |
number | x position of first point | ||
a_y |
number | y position of first point | ||
b_x |
number | x position of second point | ||
b_y |
number | y position of second point | ||
accuracy |
number |
<optional> |
1e-10
|
Accuracy |
Returns:
- Type
- number
(static) cntrPnt(a_x, a_y, b_x, b_y, returnDataopt, r) → {Object}
Get center of circles from two points and radius
Example
Geometry.cntrPnt(1, 0, -1, 0, 2);
//{x1: 0, y1: -1.7320508075688772, x2: 0, y2: 1.7320508075688772}
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
a_x |
number | x position of first point | |
a_y |
number | y position of first point | |
b_x |
number | x position of second point | |
b_y |
number | y position of second point | |
returnData |
object |
<optional> |
Object to put data |
r |
number | circle radius |
Returns:
Return two center points (both point will be NaN if there is no possible center)
- Type
- Object
(static) colliAnglePnt(o_1, o_2, o_x, o_y, a_x, a_y) → {boolean}
Check if an angle from two points is in an angle range
Example
Geometry.colliAnglePnt(0, Math.PI, 0, 0, 0, 1);
//true
Parameters:
Name | Type | Description |
---|---|---|
o_1 |
number | angle in radians |
o_2 |
number | angle in radians |
o_x |
number | x position of vertex point |
o_y |
number | y position of vertex point |
a_x |
number | x position of point want to check |
a_y |
number | y position of point want to check |
Returns:
- Type
- boolean
(static) colliArcCircPnt(a_x, a_y, o_x, o_y, o_r, x_x, x_y, y_x, y_y) → {boolean}
Check if a point is inside an arc
Example
Geometry.colliArcCircPnt(1, 1, 0, 0, 2, 2, 0, 0, 2);
//true
Parameters:
Name | Type | Description |
---|---|---|
a_x |
number | x position of point |
a_y |
number | y position of point |
o_x |
number | x position of vertex point |
o_y |
number | y position of vertex point |
o_r |
number | radius of the arc |
x_x |
number | x position of start point of the arc |
x_y |
number | y position of start point of the arc |
y_x |
number | x position of end point of the arc |
y_y |
number | y position of end point of the arc |
Returns:
- Type
- boolean
(static) colliCirc(a_x, a_y, a_r, b_x, b_y, b_r) → {boolean}
Check if a circle collide another circle
Example
Geometry.colliCirc(0, 0, 1, 2, 0, 1);
//true
Parameters:
Name | Type | Description |
---|---|---|
a_x |
number | x position of center point of first circle |
a_y |
number | y position of center point of first circle |
a_r |
number | radius of first circle |
b_x |
number | x position of center point of second circle |
b_y |
number | y position of center point of second circle |
b_r |
number | radius of second circle |
Returns:
- Type
- boolean
(static) colliCircPnt(o_x, o_y, o_r, a_x, a_y, accuracyopt) → {number}
Check if a point is inside a circle, return 1, 0, -1 based on location
Example
Geometry.colliCircPnt(0, 0, 2, 1, 1);
//1
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
o_x |
number | x position of center point | ||
o_y |
number | y position of center point | ||
o_r |
number | radius of the circle | ||
a_x |
number | x position of point | ||
a_y |
number | y position of point | ||
accuracy |
number |
<optional> |
1e-10
|
Accuracy |
Returns:
- Type
- number
(static) colliCircRect(o_x, o_y, o_r, x_min, y_min, x_max, y_max) → {boolean}
Check if a circle collide a rectangle
Example
Geometry.colliCircRect(0, 0, 2, 1, 0, 3, 2);
//true
Parameters:
Name | Type | Description |
---|---|---|
o_x |
number | x position of center point |
o_y |
number | y position of center point |
o_r |
number | radius of the circle |
x_min |
number | x position of top-left corner |
y_min |
number | y position of top-left corner |
x_max |
number | x position of bottom-right corner |
y_max |
number | y position of bottom-right corner |
Returns:
- Type
- boolean
(static) colliElliPnt(x, y, radius1, radius2, angle, o_x, o_y) → {boolean}
Check if a point inside an ellipse
Example
Geometry.colliElliPnt(0, 0, 2, 1, 0, 1, 0);
//true
Parameters:
Name | Type | Description |
---|---|---|
x |
number | x position of center point |
y |
number | y position of center point |
radius1 |
number | radius of major axis |
radius2 |
number | radius of minor axis |
angle |
number | rotation of ellipse in radians |
o_x |
number | x position of point |
o_y |
number | y position of point |
Returns:
- Type
- boolean
(static) colliLinePnt(type, a_x, a_y, b_x, b_y, o_x, o_y, accuracyopt) → {boolean}
Check if a point is on a line
Example
Geometry.colliLinePnt(true, 0, 0, 2, 0, 1, 0);
//true
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
type |
boolean | true if two points is line segment | ||
a_x |
number | x position of first point of the line segment | ||
a_y |
number | y position of first point of the line segment | ||
b_x |
number | x position of second point of the line segment | ||
b_y |
number | y position of second point of the line segment | ||
o_x |
number | x position of point | ||
o_y |
number | y position of point | ||
accuracy |
number |
<optional> |
1e-10
|
Accuracy |
Returns:
- Type
- boolean
(static) colliPoly(points, points) → {boolean}
Check if a polygon (convex, concave, complex) collide another polygon (convex, concave, complex)
Example
Geometry.colliPoly([0, 0, 50, 0, 100, 50, 50, 100, 0, 100], [0, 0, 50, 0, 100, 50, 50, 100, 0, 100]);
//true
Parameters:
Name | Type | Description |
---|---|---|
points |
Array.<number> | array of points of first polygon [x1, y1, x2, y2, ...] |
points |
Array.<number> | array of points of second polygon [x1, y1, x2, y2, ...] |
Returns:
- Type
- boolean
(static) colliPolyCirc(points, o_x, o_y, o_r) → {boolean}
Check if a circle collide a polygon (convex, concave, complex)
Example
Geometry.colliPolyCirc([0, 0, 50, 0, 100, 50, 50, 100, 0, 100], 10, 10, 10);
//true
Parameters:
Name | Type | Description |
---|---|---|
points |
Array.<number> | array of points [x1, y1, x2, y2, ...] |
o_x |
number | x position of circle center |
o_y |
number | y position of circle center |
o_r |
number | radius of the circle |
Returns:
- Type
- boolean
(static) colliPolyPnt(points, x, y) → {boolean}
Check if a point inside a polygon (convex, concave, complex)
Example
Geometry.colliPolyPnt([0, 0, 50, 0, 100, 50, 50, 100, 0, 100], 10, 10);
//true
Parameters:
Name | Type | Description |
---|---|---|
points |
Array.<number> | array of points [x1, y1, x2, y2, ...] |
x |
number | x position of the point |
y |
number | y position of the point |
Returns:
- Type
- boolean
(static) colliRayRect(a_x, a_y, b_x, b_y, x_min, y_min, x_max, y_max) → {boolean}
Check if a ray collide a rectangle
Example
Geometry.colliRayRect(0, 0, 1, 1, 2, 3, 3, 2);
//true
Parameters:
Name | Type | Description |
---|---|---|
a_x |
number | x position of vertex point of the ray |
a_y |
number | y position of vertex point of the ray |
b_x |
number | x position of direction point of the ray |
b_y |
number | y position of direction point of the ray |
x_min |
number | x position of top-left corner |
y_min |
number | y position of top-left corner |
x_max |
number | x position of bottom-right corner |
y_max |
number | y position of bottom-right corner |
Returns:
- Type
- boolean
(static) colliRect(x_min, y_min, x_max, y_max, x2_min, y2_min, x2_max, y2_max) → {boolean}
Check if a rectangle collide another rectangle
Example
Geometry.colliRect(0, 0, 2, 1, 1, 1, 3, 2);
//true
Parameters:
Name | Type | Description |
---|---|---|
x_min |
number | x position of top-left corner of first rectangle |
y_min |
number | y position of top-left corner of first rectangle |
x_max |
number | x position of bottom-right corner of first rectangle |
y_max |
number | y position of bottom-right corner of first rectangle |
x2_min |
number | x position of top-left corner of second rectangle |
y2_min |
number | y position of top-left corner of second rectangle |
x2_max |
number | x position of bottom-right corner of second rectangle |
y2_max |
number | y position of bottom-right corner of second rectangle |
Returns:
- Type
- boolean
(static) colliRectPnt(x_min, y_min, x_max, y_max, x, y) → {boolean}
Check if a point inside the rectangle
Example
Geometry.colliRectPnt(0, 0, 2, 1, 1, 1);
//true
Parameters:
Name | Type | Description |
---|---|---|
x_min |
number | x position of top-left corner |
y_min |
number | y position of top-left corner |
x_max |
number | x position of bottom-right corner |
y_max |
number | y position of bottom-right corner |
x |
number | x position of the point |
y |
number | y position of the point |
Returns:
- Type
- boolean
(static) colliTriPnt(x_1, y_1, x_2, y_2, x_3, y_3, x, y) → {boolean}
Check if a point inside the triangle
Example
Geometry.colliTriPnt(0, 0, 4, 0, 2, 2, 2, 1);
//true
Parameters:
Name | Type | Description |
---|---|---|
x_1 |
number | x position of the first vertex |
y_1 |
number | y position of the first vertex |
x_2 |
number | x position of the second vertex |
y_2 |
number | y position of the second vertex |
x_3 |
number | x position of the third vertex |
y_3 |
number | y position of the third vertex |
x |
number | x position of the point |
y |
number | y position of the point |
Returns:
- Type
- boolean
(static) convexHullPoly(points, returnDataopt) → {Array.<number>}
Find convex hull of a set of points
Example
Geometry.convexHullPoly([[0, 0], [50, 0], [100, 50], [50, 100], [0, 100]]);
//[[0, 0], [50, 0], [100, 50], [50, 100], [0, 100]]
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
points |
Array.<number> | array of points [[x1, y1], [x2, y2], ...] | |
returnData |
array |
<optional> |
Array to put data |
Returns:
- Type
- Array.<number>
(static) crcmCircTri(x_1, y_1, x_2, y_2, x_3, y_3, returnDataopt) → {Object}
Calculate center point of a circle form by three point from the triangle
Example
Geometry.crcmCircTri(0, 0, 2, 0, 1, 1);
//{x: 1, y: 0, r: 1}
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
x_1 |
number | x position of the first vertex | |
y_1 |
number | y position of the first vertex | |
x_2 |
number | x position of the second vertex | |
y_2 |
number | y position of the second vertex | |
x_3 |
number | x position of the third vertex | |
y_3 |
number | y position of the third vertex | |
returnData |
object |
<optional> |
Object to put data |
Returns:
- Type
- Object
(static) crcmCntrTri(x_1, y_1, x_2, y_2, x_3, y_3, returnDataopt) → {Object}
Calculate center point of triangle
Example
Geometry.crcmCntrTri(0, 0, 2, 0, 1, 1);
//{x: 2, y: 0}
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
x_1 |
number | x position of the first vertex | |
y_1 |
number | y position of the first vertex | |
x_2 |
number | x position of the second vertex | |
y_2 |
number | y position of the second vertex | |
x_3 |
number | x position of the third vertex | |
y_3 |
number | y position of the third vertex | |
returnData |
object |
<optional> |
Object to put data |
Returns:
returnData.x and returnData.y
- Type
- Object
(static) cssnAngle(cos, sin) → {number}
Find angle given the sine and cosine of that angle
Example
Geometry.cssnAngle(-1, 0);
//3.141592653589793
Parameters:
Name | Type | Description |
---|---|---|
cos |
number | |
sin |
number |
Returns:
- Type
- number
(static) diffAngle(num1, num2, diropt) → {number}
Get different angle between two angles
Example
Geometry.diffAngle(Math.PI / 4, Math.PI);
//2.356194490192345
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
num1 |
number | angle in radians | ||
num2 |
number | angle in radians | ||
dir |
boolean |
<optional> |
false
|
`true` if large angle |
Returns:
Angle in radians
- Type
- number
(static) distAccel(u, v, s) → {number}
Calculates the acceleration needed to cover distance [s] with starting speed [u] and max speed [v]
Example
Geometry.distAccel(1, 5, 50);
//0.24
Parameters:
Name | Type | Description |
---|---|---|
u |
number | current speed |
v |
number | target speed |
s |
number | distance |
Returns:
- Type
- number
(static) distDecel(u, f) → {number}
Calculate distance travelled given base speed [s] and friction [f]
Example
Geometry.distDecel(1, 5, 0.1);
//40
Parameters:
Name | Type | Description |
---|---|---|
u |
number | current speed |
f |
number | friction |
Returns:
- Type
- number
(static) distElliPnt(radius1, radius2, angle) → {number}
Calculate distance from a point on an ellipse to it's center
Example
Geometry.distElliPnt(2, 1, Math_HALF_PI);
//1
Parameters:
Name | Type | Description |
---|---|---|
radius1 |
number | radius of major axis of the ellipse |
radius2 |
number | radius of minor axis of the ellipse |
angle |
number | angle from center to point in radians |
Returns:
- Type
- number
(static) distLine(type, a_x, a_y, b_x, b_y, c_x, c_y, d_x, d_y, squareopt) → {number}
Calculate distance from a line to another line with different modes
Example
Geometry.distLine(false, 0, 0, 1, 1, 2, 0, 2, 1, true);
//1
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
type |
boolean | `true` if calculate perpendicular distance | ||
a_x |
number | x position of first point of the first segment | ||
a_y |
number | y position of first point of the first segment | ||
b_x |
number | x position of second point of the first segment | ||
b_y |
number | y position of second point of the first segment | ||
c_x |
number | x position of first point of the second segment | ||
c_y |
number | y position of first point of the second segment | ||
d_x |
number | x position of second point of the second segment | ||
d_y |
number | y position of second point of the second segment | ||
square |
boolean |
<optional> |
false
|
`false` if you want distance squared |
Returns:
- Type
- number
(static) distLinePnt(type, a_x, a_y, b_x, b_y, x_x, x_y, squareopt) → {number}
Calculate distance from a line to a point with different modes
Example
Geometry.distLinePnt(false, 0, 0, 1, 1, 2, 0, true);
//1.4142135623730951
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
type |
boolean | `true` if calculate perpendicular distance | ||
a_x |
number | x position of first point of the segment | ||
a_y |
number | y position of first point of the segment | ||
b_x |
number | x position of second point of the segment | ||
b_y |
number | y position of second point of the segment | ||
x_x |
number | x position of the point | ||
x_y |
number | y position of the point | ||
square |
boolean |
<optional> |
false
|
`false` if you want distance squared |
Returns:
- Type
- number
(static) distPnt(a_x, a_y, b_x, b_y, squareopt) → {number}
Calculate distance of two points
Example
Geometry.distPnt(0, 0, 0, 1, true);
//1
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
a_x |
number | x position of first point | ||
a_y |
number | y position of first point | ||
b_x |
number | x position of second point | ||
b_y |
number | y position of second point | ||
square |
boolean |
<optional> |
false
|
`false` if you want squared distance |
Returns:
- Type
- number
(static) distPolyRay(points, a_x, a_y, b_x, b_y, epsilonopt, returnDataopt) → {Object}
Finds the closest point of a polygon (convex, concave, ~~complex~~) that lay on ray
Example
Geometry.distPolyRay([0, 0, 50, 0, 100, 50, 50, 100, 0, 100], -25, 0, 25, 50);
//{dist: 55.90169943749474, edge: 4, norm_x: 1, norm_y: 0, refl_x: -25, refl_y: 50}
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
points |
Array.<number> | array of points [x1, y1, x2, y2, ...] | ||
a_x |
number | x position of vertex point of the ray | ||
a_y |
number | y position of vertex point of the ray | ||
b_x |
number | x position of direction point of the ray | ||
b_y |
number | y position of direction point of the ray | ||
epsilon |
number |
<optional> |
1e-10
|
|
returnData |
object |
<optional> |
Object to put data |
Returns:
"dist" is the distance of the polygon point, "edge" is the number of the edge, on which intersection occurs, "norm" is the normal in that place, "refl" is reflected direction
- Type
- Object
(static) equilTri(x, y, len, returnDataopt) → {Object}
Construct equilateral triangle
Example
Geometry.equilTri(0, 0, 2);
//{x1: 0, y1: 0, x2: 1, y2: 1.7320508075688772, x3: -1, y3: 1.7320508075688772}
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
x |
number | x position of point | |
y |
number | y position of point | |
len |
number | height of the triangle | |
returnData |
object |
<optional> |
Object to put data |
Returns:
- Type
- Object
(static) fullDeg(num) → {number}
Wrap angle within 0 to 360 in degrees
Example
Geometry.fullDeg(-90);
//270
Parameters:
Name | Type | Description |
---|---|---|
num |
number | The angle needs to wrap |
Returns:
- Type
- number
(static) fullRad(num) → {number}
Wrap angle within 0 to TAU in radians
Example
Geometry.fullRad(-Math.PI);
//3.141592653589793
Parameters:
Name | Type | Description |
---|---|---|
num |
number | The angle needs to wrap |
Returns:
- Type
- number
(static) getAngle(a_x, a_y, b_x, b_y) → {number}
Get angle from two points
Example
Geometry.getAngle(0, 0, 0, 1);
//1.5707963267948966
Parameters:
Name | Type | Description |
---|---|---|
a_x |
number | x position of first point |
a_y |
number | y position of first point |
b_x |
number | x position of second point |
b_y |
number | y position of second point |
Returns:
Angle in radians
- Type
- number
(static) getXLine(a_x, a_y, b_x, b_y, num) → {number}
Calculate x coordinate of a point on a line known y coordinate
Example
Geometry.getXLine(0, 0, 2, 2, 1);
//1
Parameters:
Name | Type | Description |
---|---|---|
a_x |
number | x position of first point of the line segment |
a_y |
number | y position of first point of the line segment |
b_x |
number | x position of second point of the line segment |
b_y |
number | y position of second point of the line segment |
num |
number | y position of the point |
Returns:
- Type
- number
(static) getYLine(a_x, a_y, b_x, b_y, num) → {number}
Calculate y coordinate of a point on a line known x coordinate
Example
Geometry.getYLine(0, 0, 2, 2, 1);
//1
Parameters:
Name | Type | Description |
---|---|---|
a_x |
number | x position of first point of the line segment |
a_y |
number | y position of first point of the line segment |
b_x |
number | x position of second point of the line segment |
b_y |
number | y position of second point of the line segment |
num |
number | x position of the point |
Returns:
- Type
- number
(static) inCircTri(x_1, y_1, x_2, y_2, x_3, y_3, returnDataopt) → {Object}
Calculate inscribed center point of a circle form by three point from the triangle
Example
Geometry.inCircTri(0, 0, 2, 0, 1, 1);
//{x: 1, y: 0.4142135623730951, r: 0.41421356237309487}
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
x_1 |
number | x position of the first vertex | |
y_1 |
number | y position of the first vertex | |
x_2 |
number | x position of the second vertex | |
y_2 |
number | y position of the second vertex | |
x_3 |
number | x position of the third vertex | |
y_3 |
number | y position of the third vertex | |
returnData |
object |
<optional> |
Object to put data |
Returns:
- Type
- Object
(static) intrCirc(a_x, a_y, a_r, b_x, b_y, b_r, returnDataopt) → {Array.<number>}
Find intersection of 2 circles
Example
Geometry.intrCirc(0, 0, 1, 2, 0, 1);
//[1, 0, 1, 0]
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
a_x |
number | x position of center point of first circle | |
a_y |
number | y position of center point of first circle | |
a_r |
number | radius of first circle | |
b_x |
number | x position of center point of second circle | |
b_y |
number | y position of center point of second circle | |
b_r |
number | radius of second circle | |
returnData |
array |
<optional> |
Array to put data |
Returns:
[x1, y1, x2, y2], NaN if no intersection
- Type
- Array.<number>
(static) intrCircLine(o_x, o_y, radius, a_x, a_y, b_x, b_y, accuracyopt, returnDataopt) → {Object}
Find intersections of a circle and a line
Example
Geometry.intrCircLine(0, 0, 4, -6, -2, -2, -2);
//{x: 0, y: -2, x1: -3.4641016151377544, y1: -2, x2: 3.4641016151377535, y2: -2, onLine: false, onLine1: true, onLine2: false}
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
o_x |
number | x position of circle center | |
o_y |
number | y position of circle center | |
radius |
number | radius of circle center | |
a_x |
number | x position of first point of the segment | |
a_y |
number | y position of first point of the segment | |
b_x |
number | x position of second point of the segment | |
b_y |
number | y position of second point of the segment | |
accuracy |
number |
<optional> |
accuracy to compare line vs points |
returnData |
object |
<optional> |
Object to put data |
Returns:
returnData.onLine will true if perpendicular intersection point is on line a_x a_y b_x b_y.
returnData.onLine1 will true if returnData.x1 and returnData.y1 is on line segment, and so on...
- Type
- Object
(static) intrElli(x_1, y_1, radius1_1, radius2_1, angle_1, x_2, y_2, radius1_2, radius2_2, angle_2, returnDataopt) → {Array}
Find intersection points of two ellipses
Example
Geometry.intrElli(0, 0, 2, 1, Math.PI / 4, 0, 0, 2, 1, -Math.PI / 4);
//[
// -1.1102230246251565e-16, 1.2649110640673515, 1.2649110640673515, 3.3306690738754696e-16,
// -1.1102230246251565e-16, -1.2649110640673515, -1.2649110640673515, -1.1102230246251565e-16
//]
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
x_1 |
number | x position of center point of first ellipse | |
y_1 |
number | y position of center point of first ellipse | |
radius1_1 |
number | radius of major axis of first ellipse | |
radius2_1 |
number | radius of minor axis of first ellipse | |
angle_1 |
number | rotation of ellipse in radians of first ellipse | |
x_2 |
number | x position of center point of second ellipse | |
y_2 |
number | y position of center point of second ellipse | |
radius1_2 |
number | radius of major axis of second ellipse | |
radius2_2 |
number | radius of minor axis of second ellipse | |
angle_2 |
number | rotation of ellipse in radians of second ellipse | |
returnData |
array |
<optional> |
Array to put data |
Returns:
[x1, y1, x2, y2, ...]
- Type
- Array
(static) intrElliLine(x, y, radius1, radius2, angle, a_x, a_y, b_x, b_y, returnDataopt) → {Array}
Find intersection points of an ellipse and a line
Example
Geometry.intrElliLine(0, 0, 2, 1, 0, 0, 2, 0, -2);
//[0, 1, 0, -1]
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
x |
number | x position of center point of the ellipse | |
y |
number | y position of center point of the ellipse | |
radius1 |
number | radius of major axis of the ellipse | |
radius2 |
number | radius of minor axis of the ellipse | |
angle |
number | rotation of ellipse in radians of the ellipse | |
a_x |
number | x position of first point of the line | |
a_y |
number | y position of first point of the line | |
b_x |
number | x position of second point of the line | |
b_y |
number | y position of second point of the line | |
returnData |
array |
<optional> |
Array to put data |
Returns:
[x1, y1, x2, y2, ...]
- Type
- Array
(static) intrLine(a_x, a_y, b_x, b_y, c_x, c_y, d_x, d_y, accuracyopt, returnDataopt) → {Object}
Find intersection of two lines
Example
Geometry.intrLine(0, 0, 1, 1, 1, 0, 1, -1);
//{x: 1, y: 1, a: 1, b: 1}
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
a_x |
number | x position of first point of the first segment | |
a_y |
number | y position of first point of the first segment | |
b_x |
number | x position of second point of the first segment | |
b_y |
number | y position of second point of the first segment | |
c_x |
number | x position of first point of the second segment | |
c_y |
number | y position of first point of the second segment | |
d_x |
number | x position of second point of the second segment | |
d_y |
number | y position of second point of the second segment | |
accuracy |
number |
<optional> |
accuracy to compare line vs points |
returnData |
object |
<optional> |
Object to put data |
Returns:
a and b number means: -1: left, 0: in, 1: right, 2: same slope and collide
- Type
- Object
(static) intrPolyPnt(points, x, y, returnDataopt) → {Object}
Finds the point on polygon edges (convex, concave, complex), which is closest to the point
Example
Geometry.intrPolyPnt([0, 0, 50, 0, 100, 50, 50, 100, 0, 100], 200, 200);
//{dist: 176.7766952966369, edge: 2, norm_x: 0.7071067811865475, norm_y: 0.7071067811865475, point_x: 75, point_y: 75}
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
points |
Array.<number> | array of points [x1, y1, x2, y2, ...] | |
x |
number | x position of the point | |
y |
number | y position of the point | |
returnData |
object |
<optional> |
Object to put data |
Returns:
"dist" is the distance of the polygon point, "edge" is the number of the closest edge, "point" is the closest point on that edge, "norm" is the normal from "point" to the point
- Type
- Object
(static) intrRect(x_min, y_min, x_max, y_max, x_min, y_min, x_max, y_max, returnDataopt) → {Object}
Calculate corner of bounding rectangle from two rectangles
Example
Geometry.intrRect(0, 0, 2, 1, 1, 1, 3, 2);
//{xMin: 1, yMin: 1, xMax: 2, yMax: 1}
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
x_min |
number | x position of top-left corner of first rectangle | |
y_min |
number | y position of top-left corner of first rectangle | |
x_max |
number | x position of bottom-right corner of first rectangle | |
y_max |
number | y position of bottom-right corner of first rectangle | |
x_min |
number | x position of top-left corner of second rectangle | |
y_min |
number | y position of top-left corner of second rectangle | |
x_max |
number | x position of bottom-right corner of second rectangle | |
y_max |
number | y position of bottom-right corner of second rectangle | |
returnData |
object |
<optional> |
Object to put data |
Returns:
- Type
- Object
(static) isBetwAngle(num, min, max) → {boolean}
Check if angle is between two angles
Example
Geometry.isBetwAngle(Math_HALF_PI, 0, Math.PI);
//true
Parameters:
Name | Type | Description |
---|---|---|
num |
number | angle in radians |
min |
number | angle in radians |
max |
number | angle in radians |
Returns:
- Type
- boolean
(static) isConvexPoly(points) → {boolean}
Check if a polygon is convex
Example
Geometry.isConvexPoly([0, 0, 50, 0, 100, 50, 50, 100, 0, 100]);
//true
Parameters:
Name | Type | Description |
---|---|---|
points |
Array.<number> | array of points [x1, y1, x2, y2, ...] |
Returns:
- Type
- boolean
(static) isSimplePoly(points) → {boolean}
Check if a polygon is not complex polygon (check if not self-intersecting)
Example
Geometry.isSimplePoly([0, 0, 50, 0, 100, 50, 50, 100, 0, 100]);
//true
Parameters:
Name | Type | Description |
---|---|---|
points |
Array.<number> | array of points [x1, y1, x2, y2, ...] |
Returns:
- Type
- boolean
(static) manDistPnt(a_x, a_y, b_x, b_y) → {number}
Calculate Manhattan distance of two points
Example
Geometry.manDistPnt(0, 0, 0, 1);
//1
Parameters:
Name | Type | Description |
---|---|---|
a_x |
number | x position of first point |
a_y |
number | y position of first point |
b_x |
number | x position of second point |
b_y |
number | y position of second point |
Returns:
- Type
- number
(static) normDeg(num) → {number}
Normalize angle in degrees
Example
Geometry.normDeg(720);
//0
Parameters:
Name | Type | Description |
---|---|---|
num |
number | The angle needs to normalize |
Returns:
- Type
- number
(static) normRad(num) → {number}
Normalize angle in radians
Example
Geometry.normRad(Math_TAU);
//0
Parameters:
Name | Type | Description |
---|---|---|
num |
number | The angle needs to normalize |
Returns:
- Type
- number
(static) onElli(x, y, radius1, radius2, angle, angle2, returnDataopt) → {Object}
Calculate point on an ellipse
Example
Geometry.onElli(0, 0, 2, 1, 0, 0);
//{x: 2, y: 0}
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
x |
number | x position of center point | |
y |
number | y position of center point | |
radius1 |
number | radius of major axis | |
radius2 |
number | radius of minor axis | |
angle |
number | rotation of ellipse in radians | |
angle2 |
number | angle from center to point in radians | |
returnData |
object |
<optional> |
Object to put data |
Returns:
returnData.x and returnData.y
- Type
- Object
(static) onLine(a_x, a_y, b_x, b_y, scale, returnDataopt) → {Object}
Calculate location of a point on a line
Example
Geometry.onLine(0, 0, 2, 2, 0.5);
//{x: 1, y: 1}
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
a_x |
number | x position of first point of the line segment | |
a_y |
number | y position of first point of the line segment | |
b_x |
number | x position of second point of the line segment | |
b_y |
number | y position of second point of the line segment | |
scale |
number | the location of the point on the line, 0 to 1, but can be larger or smaller than that range | |
returnData |
object |
<optional> |
Object to put data |
Returns:
- Type
- Object
(static) polarDistPnt(r_1, a_1, r_2, a_2, squareopt) → {number}
Calculate distance of two points using polar coordinate
Example
Geometry.polarDistPnt(0, 0, 1, Math_HALF_PI, true);
//1
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
r_1 |
number | radial of first point | ||
a_1 |
number | angle of first point in radians | ||
r_2 |
number | radial of second point | ||
a_2 |
number | angle of second point in radians | ||
square |
boolean |
<optional> |
false
|
`false` if you want squared distance |
Returns:
- Type
- number
(static) randomCirc(x, y, radius, returnDataopt, uniformopt) → {Object}
Generate random point inside circle
Example
Geometry.randomCirc(0, 0, 2, true);
//Random points
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
x |
number | x position of center point | ||
y |
number | y position of center point | ||
radius |
number | radius of the circle | ||
returnData |
object |
<optional> |
Object to put data | |
uniform |
boolean |
<optional> |
false
|
`true` if generate uniformly |
Returns:
returnData.x and returnData.y
- Type
- Object
(static) randomElli(x, y, radius1, radius2, angle, uniformopt, returnDataopt) → {Object}
Generate a random point inside an ellipse
Example
Geometry.randomElli(0, 0, 2, 1, Math.PI / 4);
//Random point
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
x |
number | x position of center point of the ellipse | ||
y |
number | y position of center point of the ellipse | ||
radius1 |
number | radius of major axis of the ellipse | ||
radius2 |
number | radius of minor axis of the ellipse | ||
angle |
number | rotation of ellipse in radians of the ellipse | ||
uniform |
boolean |
<optional> |
false
|
`true` if generate uniformly |
returnData |
object |
<optional> |
Object to put data |
Returns:
returnData.x and returnData.y
- Type
- Object
(static) randomRect(x_min, y_min, x_max, y_max, returnDataopt) → {Object}
Generate random point inside a rectangle
Example
Geometry.randomRect(0, 0, 2, 1);
//Random point
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
x_min |
number | x position of top-left corner | |
y_min |
number | y position of top-left corner | |
x_max |
number | x position of bottom-right corner | |
y_max |
number | y position of bottom-right corner | |
returnData |
object |
<optional> |
Object to put data |
Returns:
- Type
- Object
(static) randomTri(x_1, y_1, x_2, y_2, x_3, y_3, returnDataopt) → {Object}
Generate random point in the triangle
Example
Geometry.randomTri(0, 0, 2, 0, 1, 1);
//Random point
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
x_1 |
number | x position of the first vertex | |
y_1 |
number | y position of the first vertex | |
x_2 |
number | x position of the second vertex | |
y_2 |
number | y position of the second vertex | |
x_3 |
number | x position of the third vertex | |
y_3 |
number | y position of the third vertex | |
returnData |
object |
<optional> |
Object to put data |
Returns:
- Type
- Object
(static) reflAngle(num, mirror) → {number}
Get reflection angle from mirror angle
Example
Geometry.reflAngle(Math.PI, Math.PI / 4);
//-1.5707963267948966
Parameters:
Name | Type | Description |
---|---|---|
num |
number | current angle in radians |
mirror |
number | mirror angle in radians |
Returns:
Angle in radians
- Type
- number
(static) reflLinePnt(a_x, a_y, b_x, b_y, o_x, o_y, returnDataopt) → {Object}
Reflect a point over a line
Example
Geometry.reflLinePnt(0, 0, 2, 0, 1, 1);
//{x: 1, y: -1}
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
a_x |
number | x position of first point of the line segment | |
a_y |
number | y position of first point of the line segment | |
b_x |
number | x position of second point of the line segment | |
b_y |
number | y position of second point of the line segment | |
o_x |
number | x position of point | |
o_y |
number | y position of point | |
returnData |
object |
<optional> |
Object to put data |
Returns:
- Type
- Object
(static) reversePoly(points, returnDataopt) → {Array.<number>}
Reverse point sequence of a polygon
Example
Geometry.reversePoly([0, 0, 50, 0, 100, 50, 50, 100, 0, 100]);
//[0, 100, 50, 100, 100, 50, 50, 0, 0, 0]
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
points |
Array.<number> | array of points [x1, y1, x2, y2, ...] | |
returnData |
array |
<optional> |
Array to put data |
Returns:
- Type
- Array.<number>
(static) rightTri(x, y, width, height, returnDataopt) → {Object}
Construct right triangle
Example
Geometry.rightTri(0, 0, 2, 2);
//{x1: 0, y1: 0, x2: 0, y2: -2, x3: 2, y3: 0}
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
x |
number | x position of point | |
y |
number | y position of point | |
width |
number | width of the triangle | |
height |
number | height of the triangle | |
returnData |
object |
<optional> |
Object to put data |
Returns:
- Type
- Object
(static) rotPnt(a_x, a_y, b_x, b_y, x_x, x_yopt, returnDataopt) → {Object}
Rotate a point from center point
Recommended to set `x_x` and `x_y` to same value
Example
Geometry.rotPnt(0, 0, 0, 1, Math_HALF_PI, Math_HALF_PI);
//{x: -1, y: 6.123233995736766e-17}
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
a_x |
number | x position of anchor point | ||
a_y |
number | y position of anchor point | ||
b_x |
number | x position of current point | ||
b_y |
number | y position of current point | ||
x_x |
number | x angle in radians to rotate | ||
x_y |
number |
<optional> |
x_x
|
y angle in radians to rotate |
returnData |
object |
<optional> |
Object to put data |
Returns:
- Type
- Object
(static) scaleAngle(num, min, max, dir) → {number}
Scale angle
Example
Geometry.scaleAngle(0.25, 0, Math.PI);
//2.356194490192345
Parameters:
Name | Type | Description |
---|---|---|
num |
number | Scale percent |
min |
number | angle in radians |
max |
number | angle in radians |
dir |
number | if `1` then clockwise, `-1` is counter-clockwise |
Returns:
- Type
- number
(static) sideLine(a_x, a_y, b_x, b_y, o_x, o_y) → {number}
Calculate cross product of a line with a point
To check if point is left side of the line, check if returnData > 0
To check if collinear, check if abs(returnData) < accuracy (0.00000001, 1e-10, ...)
Example
Geometry.sideLine(0, 0, 1, 0, 1, 1);
//1
Parameters:
Name | Type | Description |
---|---|---|
a_x |
number | x position of first point of the line segment |
a_y |
number | y position of first point of the line segment |
b_x |
number | x position of second point of the line segment |
b_y |
number | y position of second point of the line segment |
o_x |
number | x position of point |
o_y |
number | y position of point |
Returns:
- Type
- number
(static) slicePoly(points, a_x, a_y, b_x, b_y, accuracyopt) → {Array.<number>}
Slice a polygon (convex, concave, complex) to half
Example
Geometry.slicePoly([0, 0, 50, 0, 100, 50, 50, 100, 0, 100], 25, -10, 25, 110);
//[
// [25, 0, 50, 0, 100, 50, 50, 100, 25, 100],
// [25, 100, 0, 100, 0, 0, 25, 0]
//]
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
points |
Array.<number> | array of points [x1, y1, x2, y2, ...] | ||
a_x |
number | x position of first point of the line segment | ||
a_y |
number | y position of first point of the line segment | ||
b_x |
number | x position of second point of the line segment | ||
b_y |
number | y position of second point of the line segment | ||
accuracy |
number |
<optional> |
1e-10
|
Returns:
- Type
- Array.<number>
(static) slopeLine(a_x, a_y, b_x, b_y) → {number}
Calculate slope of a line segment
Example
Geometry.slopeLine(0, 0, 1, 1);
//1
Parameters:
Name | Type | Description |
---|---|---|
a_x |
number | x position of first point of the segment |
a_y |
number | y position of first point of the segment |
b_x |
number | x position of second point of the segment |
b_y |
number | y position of second point of the segment |
Returns:
- Type
- number
(static) stdLine(a_x, a_y, b_x, b_y, returnDataopt) → {Object}
Convert to standard form of a line segment where `ax + by + c = 0`
Example
Geometry.stdLine(0, 0, 1, 1);
//{a: 1, b: -1, c: 0}
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
a_x |
number | x position of first point of the segment | |
a_y |
number | y position of first point of the segment | |
b_x |
number | x position of second point of the segment | |
b_y |
number | y position of second point of the segment | |
returnData |
object |
<optional> |
Object to put data |
Returns:
- Type
- Object
(static) timeAccel(u, v, a) → {number}
Calculates the time required to move with acceleration [a] from speed [u] to speed [v] or
Calculates the acceleration needed to move from speed [u] to speed [v] in time [t]
Example
Geometry.timeAccel(1, 5, 0.1);
//40
Parameters:
Name | Type | Description |
---|---|---|
u |
number | current speed |
v |
number | target speed |
a |
number | acceleration or time |
Returns:
- Type
- number
(static) timeDistAccel(u, s, a) → {number}
Calculates the time needed to cover distance [s] with acceleration [a] and base speed [u]
Example
Geometry.timeDistAccel(1, 50, 0.24);
//16.666666666666668
Parameters:
Name | Type | Description |
---|---|---|
u |
number | current speed |
s |
number | distance |
a |
number | acceleration |
Returns:
- Type
- number
(static) toDeg(num) → {number}
Convert radians to degrees
Example
Geometry.toRad(Math.PI);
//180
Parameters:
Name | Type | Description |
---|---|---|
num |
number | The angle needs to convert |
Returns:
Angle in degrees
- Type
- number
(static) toRad(num) → {number}
Convert degrees to radians
Example
Geometry.toRad(90);
//1.5707963267948966
Parameters:
Name | Type | Description |
---|---|---|
num |
number | The angle needs to convert |
Returns:
Angle in radians
- Type
- number
(static) triPoly(points, returnDataopt) → {Array.<number>}
Triangulate a polygon (convex, concave, complex)
Example
Geometry.triPoly([0, 0, 50, 0, 100, 50, 50, 100, 0, 100]);
//[0, 1, 2, 0, 2, 3, 0, 3, 4]
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
points |
Array.<number> | array of points [x1, y1, x2, y2, ...] | |
returnData |
array |
<optional> |
Array to put data |
Returns:
return array of input points that is connected to make triangles
Ex: input: [0,0, 1,-1, 2,0, 1,1], result: [0,1,2, 0,2,3], triangle: [[0,0, 1,-1, 2,0], [0,0, 2,0, 1,1]]
- Type
- Array.<number>