{"version":3,"file":"vectors.min.mjs","names":[],"sources":["../../../../src/util/misc/vectors.ts"],"sourcesContent":["import type { XY } from '../../Point';\nimport { Point } from '../../Point';\nimport type { TRadian } from '../../typedefs';\n\nconst unitVectorX = new Point(1, 0);\nconst zero = new Point();\n\n/**\n * Rotates `vector` with `radians`\n * @param {Point} vector The vector to rotate (x and y)\n * @param {Number} radians The radians of the angle for the rotation\n * @return {Point} The new rotated point\n */\nexport const rotateVector = (vector: Point, radians: TRadian) =>\n  vector.rotate(radians);\n\n/**\n * Creates a vector from points represented as a point\n *\n * @param {Point} from\n * @param {Point} to\n * @returns {Point} vector\n */\nexport const createVector = (from: XY, to: XY): Point =>\n  new Point(to).subtract(from);\n\n/**\n * return the magnitude of a vector\n * @return {number}\n */\nexport const magnitude = (point: Point) => point.distanceFrom(zero);\n\n/**\n * Calculates the angle between 2 vectors\n * @param {Point} a\n * @param {Point} b\n * @returns the angle in radians from `a` to `b`\n */\nexport const calcAngleBetweenVectors = (a: Point, b: Point): TRadian =>\n  Math.atan2(crossProduct(a, b), dotProduct(a, b)) as TRadian;\n\n/**\n * Calculates the angle between the x axis and the vector\n * @param {Point} v\n * @returns the angle in radians of `v`\n */\nexport const calcVectorRotation = (v: Point) =>\n  calcAngleBetweenVectors(unitVectorX, v);\n\n/**\n * @param {Point} v\n * @returns {Point} vector representing the unit vector pointing to the direction of `v`\n */\nexport const getUnitVector = (v: Point): Point =>\n  v.eq(zero) ? v : v.scalarDivide(magnitude(v));\n\n/**\n * @param {Point} v\n * @param {Boolean} [counterClockwise] the direction of the orthogonal vector, defaults to `true`\n * @returns {Point} the unit orthogonal vector\n */\nexport const getOrthonormalVector = (\n  v: Point,\n  counterClockwise = true,\n): Point =>\n  getUnitVector(new Point(-v.y, v.x).scalarMultiply(counterClockwise ? 1 : -1));\n\n/**\n * Cross product of two vectors in 2D\n * @param {Point} a\n * @param {Point} b\n * @returns {number} the magnitude of Z vector\n */\nexport const crossProduct = (a: Point, b: Point): number =>\n  a.x * b.y - a.y * b.x;\n\n/**\n * Dot product of two vectors in 2D\n * @param {Point} a\n * @param {Point} b\n * @returns {number}\n */\nexport const dotProduct = (a: Point, b: Point): number => a.x * b.x + a.y * b.y;\n\n/**\n * Checks if the vector is between two others. It is considered\n * to be inside when the vector to be tested is between the\n * initial vector and the final vector (included) in a counterclockwise direction.\n * @param {Point} t vector to be tested\n * @param {Point} a initial vector\n * @param {Point} b final vector\n * @returns {boolean} true if the vector is among the others\n */\nexport const isBetweenVectors = (t: Point, a: Point, b: Point): boolean => {\n  if (t.eq(a) || t.eq(b)) return true;\n  const AxB = crossProduct(a, b),\n    AxT = crossProduct(a, t),\n    BxT = crossProduct(b, t);\n  return AxB >= 0 ? AxT >= 0 && BxT <= 0 : !(AxT <= 0 && BxT >= 0);\n};\n"],"mappings":"4CAIA,MAAM,EAAc,IAAI,EAAM,EAAG,EAAA,CAC3B,EAAO,IAAI,EAQJ,GAAgB,EAAe,IAC1C,EAAO,OAAO,EAAA,CASH,GAAgB,EAAU,IACrC,IAAI,EAAM,EAAA,CAAI,SAAS,EAAA,CAMZ,EAAa,GAAiB,EAAM,aAAa,EAAA,CAQjD,GAA2B,EAAU,IAChD,KAAK,MAAM,EAAa,EAAG,EAAA,CAAI,EAAW,EAAG,EAAA,CAAA,CAOlC,EAAsB,GACjC,EAAwB,EAAa,EAAA,CAM1B,EAAiB,GAC5B,EAAE,GAAG,EAAA,CAAQ,EAAI,EAAE,aAAa,EAAU,EAAA,CAAA,CAO/B,GACX,EACA,EAAA,CAAmB,IAEnB,EAAc,IAAI,EAAA,CAAO,EAAE,EAAG,EAAE,EAAA,CAAG,eAAe,EAAmB,EAAA,GAAI,CAAA,CAQ9D,GAAgB,EAAU,IACrC,EAAE,EAAI,EAAE,EAAI,EAAE,EAAI,EAAE,EAQT,GAAc,EAAU,IAAqB,EAAE,EAAI,EAAE,EAAI,EAAE,EAAI,EAAE,EAWjE,GAAoB,EAAU,EAAU,IAAA,CACnD,GAAI,EAAE,G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