docs/.vitepress/theme/math.ts

988ae0063581d97628dcaeeefb657c0f857b9f7e
wotaku/docs/.vitepress/theme/math.ts history · blame · raw

58 lines · 1822 bytes

 1/**
 2 *  All Rights Reserved
 3 *
 4 *  Copyright (c) 2025 taskylizard
 5 *
 6 *  All rights reserved. This code and its associated files may not be copied, modified, distributed, sublicensed, or used in any form, in whole or in part, without prior written permission from the copyright holder.
 7 */
 8// Thanks to @yellowsink for the original code!! <3
 9// https://github.com/uwu/uwu.network/blob/master/src/components/sink/Takodachi.astro
10
11export type Vec2D = [number, number]
12
13// Mathmaticians hate him!!!
14/** Lenient square root */
15export const lSqrt = (n: number) => Math.sqrt(Math.abs(n))
16
17export const v2mag = (vec: Vec2D) =>
18  Math.sqrt(vec[0] * vec[0] + vec[1] * vec[1])
19
20export const v2add = (v1: Vec2D, v2: Vec2D): Vec2D => [
21  v1[0] + v2[0],
22  v1[1] + v2[1]
23]
24
25export const v2sub = (v1: Vec2D, v2: Vec2D): Vec2D => [
26  v1[0] - v2[0],
27  v1[1] - v2[1]
28]
29
30/** Multiplies a 2D vector by a scalar */
31export const v2smul = (vec: Vec2D, scalar: number): Vec2D => [
32  vec[0] * scalar,
33  vec[1] * scalar
34]
35
36export function v2norm(vec: Vec2D): Vec2D {
37  const l = v2mag(vec)
38  if (l === 0) return [Math.sqrt(2), Math.sqrt(2)]
39
40  return [vec[0] / l, vec[1] / l]
41}
42
43export const rad2deg = (n: number) => (n * 180) / Math.PI
44
45/** Calculates the gradient of a vector */
46export const v2grad = (vec: Vec2D) => vec[1] / vec[0]
47
48/** Calculates the angle of the vector against [1, 0] in radians */
49export const v2rad = (vec: Vec2D) => Math.atan(v2grad(vec))
50
51/** Calculates the angle of the vector against [1, 0] in degrees */
52export const v2deg = (vec: Vec2D) => rad2deg(v2rad(vec))
53
54/** Angle in degrees between two vec2ds in radians */
55export const v2radBtw = (v1: Vec2D, v2: Vec2D) => v2rad(v1) - v2rad(v2)
56
57/** Angle in degrees between two vec2ds in degrees */
58export const v2degBtw = (v1: Vec2D, v2: Vec2D) => rad2deg(v2radBtw(v1, v2))