[..] Module – math

#import "jc/math";

Index

Constants (11)

Procedures (105)

Macros (32)

Types (10)

Imports (3)

Constants

EPSILON

EPSILON :: 0.001;

ORIGIN

ORIGIN :: Vec2.{.[0, 0]};

PI

PI :: 3.1415926;

TAU

TAU :: 6.2831853;

UNITS

UNITS :: 2;

from_rad

from_rad :: rad_to_turn;

m2_identity

m2_identity :: Mat2.{_00=1, _11=1};

m4_identity

m4_identity :: Mat4.{_00=1, _11=1, _22=1, _33=1};

norm

norm :: normalize;

quat_identity

quat_identity :: Quat.{x=0, y=0, z=0, w=1};

to_rad

to_rad :: turn_to_rad;

Procedures

abs

abs :: (v: Vec) -> Vec(v.N, v.T)

abs

abs :: (v: $T) -> T

area

area :: (c: Circle) -> float

area

area :: (r: Rect) -> float

ceil

ceil :: (l: Vec) -> Vec(l.N, l.T)

clamp

clamp :: (v: Vec, low: v.T, high: v.T) -> Vec(v.N, v.T)

collides

collides :: (t: Triangle, l: Line) -> bool #symmetric

collides

collides :: (r: Rect, l: Line) -> bool #symmetric

collides

collides :: (c: Circle, r: Rect) -> bool #symmetric

collides

collides :: (r1: Rect, r2: Rect) -> bool

collides

collides :: (t1: Triangle, t2: Triangle) -> bool

collides

collides :: (t: Triangle, c: Circle) -> bool #symmetric

collides

collides :: (t: Triangle, r: Rect) -> bool #symmetric

collides

collides :: (c: Circle, seg: Line) -> bool #symmetric

collides

collides :: (s1: Line, s2: Line) -> bool

collides

collides :: (c1: Circle, c2: Circle) -> bool

cross

cross :: (a: Vec3, b: Vec3) -> Vec3

cut_bottom

cut_bottom :: (rect: *Rect, want: RECT_TYPE) -> Rect

cut_left

cut_left :: (rect: *Rect, want: RECT_TYPE) -> Rect

cut_right

cut_right :: (rect: *Rect, want: RECT_TYPE) -> Rect

cut_top

cut_top :: (rect: *Rect, want: RECT_TYPE) -> Rect

determinate

determinate :: (m: Mat4) -> float

determinate

determinate :: (m: Mat2) -> float

dot

dot :: (a: Vec, b: Vec(a.N, a.T)) -> a.T

dot

dot :: (a: Quat, b: Quat) -> float

ease

ease :: (progress: $T, $$ease: Ease = .linear, $$transition: Transition = .in) -> T

float_eq

float_eq :: (f: float, with: float) -> bool

floor

floor :: (l: Vec) -> Vec(l.N, l.T)

gjk

gjk :: (s1: []Vec2, s2: []Vec2) -> bool

inside

inside :: (c: Circle, p: Vec2) -> bool #symmetric

inside

inside :: (r: Rect, p: Vec2) -> bool #symmetric

inside

inside :: (t: Triangle, p: Vec2) -> bool #symmetric

inverse

inverse :: (q: Quat) -> Quat

inverse

inverse :: (m: Mat4) -> Mat4

inverse

inverse :: (m: Mat2) -> Mat2

inverse_unit

inverse_unit :: (q: Quat) -> Quat

length

length :: (q: Quat) -> float

length

length :: (v: Vec) -> float

length_squared

length_squared :: (v: Vec) -> float

length_squared

length_squared :: (q: Quat) -> float

lerp

lerp :: (a: Vec, b: Vec(a.N, a.T), t: float) -> Vec(a.N, a.T)

make_look_at

make_look_at :: (camera: Vec3, at: Vec3, up_vector: Vec3) -> Mat4

make_rect

make_rect :: (x: RECT_TYPE, y: RECT_TYPE, w: RECT_TYPE, h: RECT_TYPE) -> Rect

max

max :: (l: Vec, r: l.T) -> Vec(l.N, l.T)

max

max :: (x: float, y: float) -> float

max

max :: (l: Vec, r: Vec(l.N, l.T)) -> Vec(l.N, l.T)

min

min :: (l: Vec, r: Vec(l.N, l.T)) -> Vec(l.N, l.T)

min

min :: (l: Vec, r: l.T) -> Vec(l.N, l.T)

min

min :: (x: float, y: float) -> float

mul_sse

mul_sse :: (a: Mat4, b: Mat4) -> Mat4

mul_sse_aligned

mul_sse_aligned :: (dst: *Mat4, a: *Mat4, b: *Mat4)

normalize

normalize :: (v: Vec) -> Vec(v.N, v.T)

operator*

operator* :: (q: Quat, v: Vec3) -> Vec3 #symmetric

operator*

operator* :: (a: Mat2, b: Mat2) -> Mat2

operator*

operator* :: (a: Mat2, b: Vec2) -> Vec2

operator*

operator* :: (q: Quat, r: $T) -> Quat #symmetric

operator*

operator* :: (a: Mat4, v: Vec4) -> Vec4

operator*

operator* :: (a: Quat, b: Quat) -> Quat

operator*

operator* :: (a: Mat4, b: Mat4) -> Mat4

operator*

operator* :: (l: Vec, r: $R) -> Vec(l.N, l.T) #symmetric

operator*

operator* :: (l: Vec, r: Vec(l.N, l.T)) -> Vec(l.N, l.T)

operator*[]

operator*[] :: (v: *Vec, $$idx: int) -> *v.T

operator*[]

operator*[] :: (m: *Mat4, $$idx: int) -> *Vec4

operator*[]

operator*[] :: (m: *Mat2, $$idx: int) -> *Vec2

operator+

operator+ :: (a: Quat, b: Quat) -> Quat

operator+

operator+ :: (l: Vec, r: $R) -> Vec(l.N, l.T) #symmetric

operator+

operator+ :: (l: Vec, r: Vec(l.N, l.T)) -> Vec(l.N, l.T)

operator-

operator- :: (l: Vec, r: $R) -> Vec(l.N, l.T)

operator-

operator- :: (q: Quat) -> Quat

operator-

operator- :: (v: Vec) -> Vec(v.N, v.T)

operator-

operator- :: (l: Vec, r: Vec(l.N, l.T)) -> Vec(l.N, l.T)

operator-

operator- :: (l: $R, r: Vec) -> Vec(l.N, l.T)

operator/

operator/ :: (l: Vec, r: Vec(l.N, l.T)) -> Vec(l.N, l.T)

operator/

operator/ :: (l: Quat, r: $T) -> Quat

operator/

operator/ :: (l: Vec, r: $R) -> Vec(l.N, l.T)

operator==

operator== :: (l: Vec, r: Vec(l.N, l.T)) -> bool

operator==

operator== :: (l: Quat, r: Quat) -> bool

operator==

operator== :: (a: Mat2, b: Mat2) -> bool

operator==

operator== :: (a: Mat4, b: Mat4) -> bool

operator[]

operator[] :: (v: Vec, $$idx: int) -> v.T

operator[]=

operator[]= :: (m: *Mat4, $$idx: int, value: Vec4)

operator[]=

operator[]= :: (v: *Vec, $$idx: int, value: v.T)

operator[]=

operator[]= :: (m: *Mat2, $$idx: int, value: Vec2)

orthographic_projection

orthographic_projection :: (left: float, right: float, top: float, bottom: float, near: float, far: float, $$depth_01: bool) -> Mat4

perspective_projection

perspective_projection :: (aspect: float, fov: float, near: float, far: float, $$depth_01: bool) -> Mat4

perspective_projection_left_handed

perspective_projection_left_handed :: (aspect: float, fov: float, near: float, far: float, $$depth_01: bool) -> Mat4

reflect

reflect :: (v: Vec2, p: Vec2) -> Vec2

reflect

reflect :: (v: Vec3, p: Vec3) -> Vec3

rotation_mat2

rotation_mat2 :: (angle: float) -> Mat2

rotation_mat4

rotation_mat4 :: (q: Quat) -> Mat4

rotation_mat4

rotation_mat4 :: (axis: Vec3, angle: float) -> Mat4

rotation_quat

rotation_quat :: (theta: float, axis: Vec3) -> Quat

round

round :: (v: Vec($N, $T)) -> Vec(N, T)

same_dir

same_dir :: (a: Vec2, b: Vec2) -> bool

same_dir

same_dir :: (a: Vec3, b: Vec3) -> bool

sign

sign :: (x: float) -> float

sign

sign :: (x: int) -> int

slerp

slerp :: (a: Quat, _b: Quat, t: float) -> Quat

translate

translate :: (with: Vec3) -> Mat4

translate

translate :: (from: Mat4, with: Vec3) -> Mat4

transpose

transpose :: (m: Mat2) -> Mat2

transpose

transpose :: (m: Mat4) -> Mat4

v2_eq

v2_eq :: (a: Vec2, b: Vec2) -> bool

v3_eq

v3_eq :: (a: Vec3, b: Vec3) -> bool

v4_eq

v4_eq :: (a: Vec4, b: Vec4) -> bool

Macros

asin

asin :: (ang: float) -> float #expand

atan2

atan2 :: (y: float, x: float) -> float #expand

conjugate

conjugate :: (q: Quat) -> Quat #expand

cos

cos :: (ang: float) -> float #expand

deg_to_rad

deg_to_rad :: (deg: float) -> float #expand

deg_to_rad

deg_to_rad :: (deg: float64) -> float #expand

for_expansion

for_expansion :: (v: *Vec, body: Code, flags: For_Flags) #expand

m2

m2 :: (r0: Vec2, r1: Vec2) -> Mat2 #expand

m2

m2 :: (_xx: float, xy: float, yx: float, yy: float) -> Mat2 #expand

m2d

m2d :: (diag: float) -> Mat2 #expand

m4

m4 :: (r0: Vec4, r1: Vec4, r2: Vec4, r3: Vec4) -> Mat4 #expand

m4d

m4d :: (diag: float) -> Mat4 #expand

operator[]

operator[] :: (m: Mat2, idx: int) -> Vec2 #expand

operator[]

operator[] :: (m: Mat4, idx: int) -> Vec4 #expand

quat

quat :: (xyz: Vec3, w: float) -> Quat #expand

quat

quat :: (x: float = 0, y: float = 0, z: float = 0, w: float = 1) -> Quat #expand

rad_to_deg

rad_to_deg :: (rad: float) -> float #expand

rad_to_deg

rad_to_deg :: (rad: float64) -> float #expand

rad_to_rad

rad_to_rad :: (rad: float) -> float #expand

rad_to_rad

rad_to_rad :: (rad: float64) -> float #expand

rad_to_turn

rad_to_turn :: (rad: float64) -> float #expand

rad_to_turn

rad_to_turn :: (rad: float) -> float #expand

sin

sin :: (ang: float) -> float #expand

turn_to_rad

turn_to_rad :: (turns: float) -> float #expand

turn_to_rad

turn_to_rad :: (turns: float64) -> float #expand

v2f

v2f :: (x: $T = 0, y: T = 0) -> Vec2 #expand

v2i

v2i :: (x: $T = 0, y: T = 0) -> Vec(2, T) #expand

v3f

v3f :: (x: $T = 0, y: T = 0, z: T = 0) -> Vec3 #expand

v3i

v3i :: (x: $T = 0, y: T = 0, z: T = 0) -> Vec(3, T) #expand

v4f

v4f :: (v: Vec3, $$w: float) -> Vec4 #expand

v4f

v4f :: (x: $T = 0, y: T = 0, z: T = 0, w: T = 0) -> Vec4 #expand

v4i

v4i :: (x: $T = 0, y: T = 0, z: T = 0, w: T = 0) -> Vec(4, T) #expand

Types

Circle

Circle :: struct {
   x,y,r: float;
   #place x;
   pos: Vec2;
}

Ease

Ease :: enum {
   linear;
   quad;
   cubic;
   quart;
   quint;
   expo;
   sine;
   circ;
   back;
   elastic;
}

Line

Line :: struct {
   x0,y0,x1,y1: float;
   #place x0;
   a: Vec2;
   #place x1;
   b: Vec2;
   #place x0;
   e: [2]Vec2;
}

Mat2

Mat2 :: #type,distinct Vec(2 * 2, float);

Mat4

Mat4 :: #type,distinct Vec(4 * 4, float);

Quat

Quat :: #type,distinct Vec4; // Note(Jesse): I Had to make this distinct, otherwise operators stomp on eachother

Rect

Rect :: #type,distinct Vec(4, RECT_TYPE);

Transition

Transition :: enum {
  in;
  out;
  in_out;
}

Triangle

Triangle :: [3]Vec2;

Vec

Vec :: struct(N: int, T: Type)
#modify {
   info := T.(*Type_Info);
   return info.type == .INTEGER || info.type == .FLOAT, "Vec T must be a numeric type (int or float)";
} {
   #assert (N > 0) "Vec N cannot be <= 0";

   // Vecs are backed by an array internally. The #insert block below
   // generates #place'd unions of named fields or cN fields when N is > 4.
   #as components: [N]T;

   #insert -> string {
      b: basic.String_Builder;
      basic.append(*b, "#place components;\n");

      fields :: []string.[
         string.[ "x", "r", "u", "min_x", ""        ],
         string.[ "y", "g", "v", "min_y", ""        ],
         string.[ "z", "b", "d", "max_x", "width"   ],
         string.[ "w", "a", "" , "max_y", "height"  ],
      ];

      for i: 0..N - 1 {
         if i < fields.count {
            basic.append(*b, "union {\n");
            for field: fields[i] if field.count != 0 {
               basic.print_to_builder(*b, "\t%: T = ---;\n", field);
            }
            basic.print_to_builder(*b, "\tc%: T = ---;\n", i);
            basic.append(*b, "};\n");
         }
         else {
            basic.print_to_builder(*b, "c%: T = ---;\n", i);
         }
      }

      // compound accessors
      basic.append(*b, "#place components;\n");
      if N >= 4 {
         basic.append(*b, "union {\n");
         basic.append(*b, "\txy:  Vec2 = ---;\n");
         basic.append(*b, "\txyz: Vec3 = ---;\n");
         basic.append(*b, "};");
      }
      else if N >= 3 {
         basic.append(*b, "xy: T = ---;\n");
      }

      // Vec4 row accessors (Possibly becoming/used as SIMD lanes)
      if N/4 > 1 {
         basic.append(*b, "#place components;\n");
         basic.print_to_builder(*b, "v4: [%]Vec4 = ---;\n", N/4);
      }

      // Matrix Accessors
      if N == 4 { // Mat2
         basic.append(*b, "#place components;\n");
         for i: 0..3 {
            basic.print_to_builder(*b, "_%0%: T = ---;\n", i/2, i%2);
         }
      }
      if N == 16 { // Mat4
         basic.append(*b, "#place components;\n");
         for i: 0..15 {
            basic.print_to_builder(*b, "_%0%: T = ---;\n", i/4, i%4);
         }
      }

      return basic.builder_to_string(*b);
   };
}