#pragma once #include #include "vec.hfa" forall (otype T) { struct vec2 { T x, y; }; } forall (otype T) { static inline { void ?{}(vec2(T)& v, T x, T y) { v.[x, y] = [x, y]; } forall(| zero_assign(T)) void ?{}(vec2(T)& vec, zero_t) with (vec) { x = y = 0; } void ?{}(vec2(T)& vec, T val) with (vec) { x = y = val; } void ?{}(vec2(T)& vec, vec2(T) other) with (vec) { [x,y] = other.[x,y]; } // Assignment void ?=?(vec2(T)& vec, vec2(T) other) with (vec) { [x,y] = other.[x,y]; } forall(| zero_assign(T)) void ?=?(vec2(T)& vec, zero_t) with (vec) { x = y = 0; } // Primitive mathematical operations // Subtraction forall(| subtract(T)) { vec2(T) ?-?(vec2(T) u, vec2(T) v) { // TODO( can't make this const ref ) return [u.x - v.x, u.y - v.y]; } vec2(T)& ?-=?(vec2(T)& u, vec2(T) v) { u = u - v; return u; } } forall(| negate(T)) { vec2(T) -?(vec2(T) v) with (v) { return [-x, -y]; } } // Addition forall(| add(T)) { vec2(T) ?+?(vec2(T) u, vec2(T) v) { // TODO( can't make this const ref ) return [u.x + v.x, u.y + v.y]; } vec2(T)& ?+=?(vec2(T)& u, vec2(T) v) { u = u + v; return u; } } // Scalar Multiplication forall(| multiply(T)) { vec2(T) ?*?(vec2(T) v, T scalar) with (v) { // TODO (can't make this const ref) return [x * scalar, y * scalar]; } vec2(T) ?*?(T scalar, vec2(T) v) { // TODO (can't make this const ref) return v * scalar; } vec2(T)& ?*=?(vec2(T)& v, T scalar) { v = v * scalar; return v; } } // Scalar Division forall(| divide(T)) { vec2(T) ?/?(vec2(T) v, T scalar) with (v) { return [x / scalar, y / scalar]; } vec2(T)& ?/=?(vec2(T)& v, T scalar) with (v) { v = v / scalar; return v; } } // Relational Operators forall(| equality(T)) { bool ?==?(vec2(T) u, vec2(T) v) with (u) { return x == v.x && y == v.y; } bool ?!=?(vec2(T) u, vec2(T) v) { return !(u == v); } } // Geometric functions forall(| add(T) | multiply(T)) T dot(vec2(T) u, vec2(T) v) { return u.x * v.x + u.y * v.y; } } // static inline } forall(dtype ostype, otype T | writeable(T, ostype)) { ostype & ?|?(ostype & os, vec2(T) v) with (v) { return os | '<' | x | ',' | y | '>'; } void ?|?(ostype & os, vec2(T) v ) with (v) { (ostype &)(os | v); ends(os); } }