# Changeset 7799f79

Ignore:
Timestamp:
Dec 13, 2019, 7:41:58 PM (4 years ago)
Branches:
Children:
ae09808
Parents:
ae3db00
Message:

Add various mathematical operations to vec2 + tests

Files:
5 edited

Unmodified
Removed
• ## libcfa/src/vec/vec2.hfa

 rae3db00 // Primitive mathematical operations // Subtraction forall(| subtract(T)) { vec2(T) ?-?(vec2(T) u, vec2(T) v) { // TODO( can't make this const ref ) } } forall(| negate(T)) { forall(| negate(T)) vec2(T) -?(vec2(T) v) with (v) { return [-x, -y]; } } // Addition forall(| { T --?(T&); }) { vec2(T)& --?(vec2(T)& v) { --v.x; --v.y; return v; } vec2(T)& ?--(vec2(T)& v) { vec2(T) copy = v; --v; return copy; } } forall(| add(T)) { vec2(T) ?+?(vec2(T) u, vec2(T) v) { // TODO( can't make this const ref ) } // Scalar Multiplication forall(| { T ++?(T&); }) { vec2(T)& ++?(vec2(T)& v) { ++v.x; ++v.y; return v; } vec2(T)& ?++(vec2(T)& v) { vec2(T) copy = v; ++v; return copy; } } forall(| multiply(T)) { vec2(T) ?*?(vec2(T) v, T scalar) with (v) { // TODO (can't make this const ref) return v * scalar; } vec2(T) ?*?(vec2(T) u, vec2(T) v) { return [u.x * v.x, u.y * v.y]; } vec2(T)& ?*=?(vec2(T)& v, T scalar) { v = v * scalar; return v; } } // Scalar Division vec2(T) ?*=?(vec2(T)& u, vec2(T) v) { u = u * v; return u; } } forall(| divide(T)) { vec2(T) ?/?(vec2(T) v, T scalar) with (v) { return [x / scalar, y / scalar]; } vec2(T) ?/?(vec2(T) u, vec2(T) v) { return [u.x / v.x, u.y / v.y]; } vec2(T)& ?/=?(vec2(T)& v, T scalar) with (v) { v = v / scalar; return v; } vec2(T) ?/=?(vec2(T)& u, vec2(T) v) { u = u / v; return u; } } forall(| { T ?%?(T,T); }) { vec2(T) ?%?(vec2(T) v, T scalar) with (v) { return [x % scalar, y % scalar]; } vec2(T)& ?%=?(vec2(T)& u, T scalar) { u = u % scalar; return u; } vec2(T) ?%?(vec2(T) u, vec2(T) v) { return [u.x % v.x, u.y % v.y]; } vec2(T)& ?%=?(vec2(T)& u, vec2(T) v) { u = u % v; return u; } } forall(| { T ?&?(T,T); }) { vec2(T) ?&?(vec2(T) v, T scalar) with (v) { return [x & scalar, y & scalar]; } vec2(T)& ?&=?(vec2(T)& u, T scalar) { u = u & scalar; return u; } vec2(T) ?&?(vec2(T) u, vec2(T) v) { return [u.x & v.x, u.y & v.y]; } vec2(T)& ?&=?(vec2(T)& u, vec2(T) v) { u = u & v; return u; } } forall(| { T ?|?(T,T); }) { vec2(T) ?|?(vec2(T) v, T scalar) with (v) { return [x | scalar, y | scalar]; } vec2(T)& ?|=?(vec2(T)& u, T scalar) { u = u | scalar; return u; } vec2(T) ?|?(vec2(T) u, vec2(T) v) { return [u.x | v.x, u.y | v.y]; } vec2(T)& ?|=?(vec2(T)& u, vec2(T) v) { u = u | v; return u; } } forall(| { T ?^?(T,T); }) { vec2(T) ?^?(vec2(T) v, T scalar) with (v) { return [x ^ scalar, y ^ scalar]; } vec2(T)& ?^=?(vec2(T)& u, T scalar) { u = u ^ scalar; return u; } vec2(T) ?^?(vec2(T) u, vec2(T) v) { return [u.x ^ v.x, u.y ^ v.y]; } vec2(T)& ?^=?(vec2(T)& u, vec2(T) v) { u = u ^ v; return u; } } forall(| { T ?<>?(T,T); }) { vec2(T) ?>>?(vec2(T) v, T scalar) with (v) { return [x >> scalar, y >> scalar]; } vec2(T)& ?>>=?(vec2(T)& u, T scalar) { u = u >> scalar; return u; } vec2(T) ?>>?(vec2(T) u, vec2(T) v) { return [u.x >> v.x, u.y >> v.y]; } vec2(T)& ?>>=?(vec2(T)& u, vec2(T) v) { u = u >> v; return u; } } forall(| { T ~?(T); }) vec2(T) ~?(vec2(T) v) with (v) { return [~v.x, ~v.y]; } } }
• ## tests/vector/.expect/vec2_float.txt

 rae3db00 s*v:<4.5,8.25> ?*=?:<4.5,8.25> ?*?(vec)<2.64,-17.4> ?*=?(vec)<2.64,-17.4> ?/?:<0.666667,-0.0333333> ?/=?:<0.666667,-0.0333333> ?/?(vec)<0.545455,-1.93333> ?/=?(vec)<0.545455,-1.93333> ++?<2.2,-2.5> --?<2.2,-2.5> ?++(part 1)<1.2,-3.5> ?++(part 2)<2.2,-2.5> ?--(part 1)<1.2,-3.5> ?--(part 2)<0.2,-4.5> dot_1:0. dot_2:8.6
• ## tests/vector/.expect/vec2_int.txt

 rae3db00 s*v:<45,825> ?*=?:<45,825> ?*?(vec)<2,-15> ?*=?(vec)<2,-15> ?/?:<7,-3> ?/=?:<7,-3> ?/?(vec)<5,-1> ?/=?(vec)<5,-1> ++?<2,-2> --?<2,-2> ?++(part 1)<1,-3> ?++(part 2)<2,-2> ?--(part 1)<1,-3> ?--(part 2)<0,-4> ?%?(int)<0,1> ?%=?(int)<0,1> ?%?(vec)<-2,3> ?%=?(vec)<-2,3> ?&?(int)<0,8> ?&=?(int)<0,8> ?&?(vec)<1,6> ?&=?(vec)<1,6> ?|?(int)<7,3> ?|=?(int)<7,3> ?|?(vec)<7,1> ?|=?(vec)<7,1> ?^?(int)<12,0> ?^=?(int)<12,0> ?^?(vec)<12,5> ?^=?(vec)<12,5> ?< ?<<=?(int)<4,2> ?< ?<<=?(vec)<32,4> ~?<-6,-3> dot_1:0 dot_2:32
• ## tests/vector/vec2_float.cfa

 rae3db00 sout | "?*=?:" | v1; { vec2(float) u = {1.2f, -5.8f}; vec2(float) v = {2.2f, 3}; sout | "?*?(vec)" | (u * v); sout | "?*=?(vec)" | (u *= v); } v1 = (vec2(float)){2, -0.1f}; sout | "?/?:" | (v1 / 3); v1 /= 3; sout | "?/=?:" | v1; { vec2(float) u = {1.2f, -5.8f}; vec2(float) v = {2.2f, 3}; sout | "?/?(vec)" | (u / v); sout | "?/=?(vec)" | (u /= v); } { vec2(float) u = {1.2f, -3.5f}; sout | "++?" | ++u; } { vec2(float) u = {1.2f, -3.5f}; sout | "--?" | ++u; } { vec2(float) u = {1.2f, -3.5f}; sout | "?++(part 1)" | u++; sout | "?++(part 2)" | u; } { vec2(float) u = {1.2f, -3.5f}; sout | "?--(part 1)" | u--; sout | "?--(part 2)" | u; } v1 = (vec2(float)){2, 3};
• ## tests/vector/vec2_int.cfa

 rae3db00 sout | "?*=?:" | v1; { vec2(int) u = {1, -5}; vec2(int) v = {2, 3}; sout | "?*?(vec)" | (u * v); sout | "?*=?(vec)" | (u *= v); } v1 = (vec2(int)){21, -10}; sout | "?/?:" | (v1 / 3); v1 /= 3; sout | "?/=?:" | v1; { vec2(int) u = {11, -5}; vec2(int) v = {2, 3}; sout | "?/?(vec)" | (u / v); sout | "?/=?(vec)" | (u /= v); } { vec2(int) u = {1, -3}; sout | "++?" | ++u; } { vec2(int) u = {1, -3}; sout | "--?" | ++u; } { vec2(int) u = {1, -3}; sout | "?++(part 1)" | u++; sout | "?++(part 2)" | u; } { vec2(int) u = {1, -3}; sout | "?--(part 1)" | u--; sout | "?--(part 2)" | u; } // % { vec2(int) u = {-12, 123}; sout | "?%?(int)" | (u % 2); sout | "?%=?(int)" | (u %= 2); } { vec2(int) u = {-12, 27}; vec2(int) v = {5, 4}; sout | "?%?(vec)" | (u % v); sout | "?%=?(vec)" | (u %= v); } // & { vec2(int) u = {5, 8}; sout | "?&?(int)" | (u & (1 << 3)); sout | "?&=?(int)" | (u &= (1 << 3)); } { vec2(int) u = {5, 6}; vec2(int) v = {3, 7}; sout | "?&?(vec)" | (u & v); sout | "?&=?(vec)" | (u &= v); } // | { vec2(int) u = {5, 1}; sout | "?|?(int)" | (u | 2); sout | "?|=?(int)" | (u |= 2); } { vec2(int) u = {5, 1}; vec2(int) v = {2, 1}; sout | "?|?(vec)" | (u | v); sout | "?|=?(vec)" | (u |= v); } // ^ { vec2(int) u = {9, 5}; sout | "?^?(int)" | (u ^ 5); sout | "?^=?(int)" | (u ^= 5); } { vec2(int) u = {9, 5}; vec2(int) v = {5, 0}; sout | "?^?(vec)" | (u ^ v); sout | "?^=?(vec)" | (u ^= v); } // << { vec2(int) u = {2, 1}; sout | "?<
Note: See TracChangeset for help on using the changeset viewer.