Started adding Flow annotations to functions in the geo submodule

(re: #3744)
This commit is contained in:
Bryan Housel
2018-03-17 01:18:05 -04:00
parent 61c7f64077
commit 3e47ca005c
5 changed files with 71 additions and 54 deletions
+15 -12
View File
@@ -1,5 +1,8 @@
// @flow
type Vec2 = [number, number];
// vector equals
export function geoVecEqual(a, b, epsilon) {
export function geoVecEqual(a: Vec2, b: Vec2, epsilon?: number) {
if (epsilon) {
return (Math.abs(a[0] - b[0]) <= epsilon) && (Math.abs(a[1] - b[1]) <= epsilon);
} else {
@@ -8,27 +11,27 @@ export function geoVecEqual(a, b, epsilon) {
}
// vector addition
export function geoVecAdd(a, b) {
export function geoVecAdd(a: Vec2, b: Vec2) {
return [ a[0] + b[0], a[1] + b[1] ];
}
// vector subtraction
export function geoVecSubtract(a, b) {
export function geoVecSubtract(a: Vec2, b: Vec2) {
return [ a[0] - b[0], a[1] - b[1] ];
}
// vector scaling
export function geoVecScale(a, mag) {
export function geoVecScale(a: Vec2, mag: number) {
return [ a[0] * mag, a[1] * mag ];
}
// vector rounding (was: geoRoundCoordinates)
export function geoVecFloor(a) {
export function geoVecFloor(a: Vec2) {
return [ Math.floor(a[0]), Math.floor(a[1]) ];
}
// linear interpolation
export function geoVecInterp(a, b, t) {
export function geoVecInterp(a: Vec2, b: Vec2, t: number) {
return [
a[0] + (b[0] - a[0]) * t,
a[1] + (b[1] - a[1]) * t
@@ -36,20 +39,20 @@ export function geoVecInterp(a, b, t) {
}
// http://jsperf.com/id-dist-optimization
export function geoVecLength(a, b) {
var x = a[0] - b[0];
var y = a[1] - b[1];
export function geoVecLength(a: Vec2, b: Vec2) {
var x: number = a[0] - b[0];
var y: number = a[1] - b[1];
return Math.sqrt((x * x) + (y * y));
}
// Return the counterclockwise angle in the range (-pi, pi)
// between the positive X axis and the line intersecting a and b.
export function geoVecAngle(a, b) {
export function geoVecAngle(a: Vec2, b: Vec2) {
return Math.atan2(b[1] - a[1], b[0] - a[0]);
}
// dot product
export function geoVecDot(a, b, origin) {
export function geoVecDot(a: Vec2, b: Vec2, origin?: Vec2) {
origin = origin || [0, 0];
return (a[0] - origin[0]) * (b[0] - origin[0]) +
(a[1] - origin[1]) * (b[1] - origin[1]);
@@ -58,7 +61,7 @@ export function geoVecDot(a, b, origin) {
// 2D cross product of OA and OB vectors, returns magnitude of Z vector
// Returns a positive value, if OAB makes a counter-clockwise turn,
// negative for clockwise turn, and zero if the points are collinear.
export function geoVecCross(a, b, origin) {
export function geoVecCross(a: Vec2, b: Vec2, origin?: Vec2) {
origin = origin || [0, 0];
return (a[0] - origin[0]) * (b[1] - origin[1]) -
(a[1] - origin[1]) * (b[0] - origin[0]);