12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 | struct Vec2{x,y:Int}
func plus(a,b:Vec2 -> Vec2; inline)
return Vec2{a.x+b.x, a.y+b.y}
func minus(a,b:Vec2 -> Vec2; inline)
return Vec2{a.x-b.x, a.y-b.y}
func dot(a,b:Vec2 -> Int; inline)
return a.x*b.x + a.y*b.y
func scaled_by(a:Vec2, k:Int -> Vec2; inline)
return Vec2{a.x*k, a.y*k}
func times(a,b:Vec2 -> Vec2; inline)
return Vec2{a.x*b.x, a.y*b.y}
func divided_by(a:Vec2, k:Int -> Vec2; inline)
return Vec2{a.x//k, a.y//k}
func negative(v:Vec2 -> Vec2; inline)
return Vec2{-v.x, -v.y}
func negated(v:Vec2 -> Vec2; inline)
return Vec2{not v.x, not v.y}
func bit_and(a,b:Vec2 -> Vec2; inline)
return Vec2{a.x and b.x, a.y and b.y}
func bit_or(a,b:Vec2 -> Vec2; inline)
return Vec2{a.x or b.x, a.y or b.y}
func bit_xor(a,b:Vec2 -> Vec2; inline)
return Vec2{a.x xor b.x, a.y xor b.y}
func left_shifted(v:Vec2, bits:Int -> Vec2; inline)
return Vec2{v.x >> bits, v.y >> bits}
func right_shifted(v:Vec2, bits:Int -> Vec2; inline)
return Vec2{v.x << bits, v.y << bits}
func modulo(v:Vec2, modulus:Int -> Vec2; inline)
return Vec2{v.x mod modulus, v.y mod modulus}
func modulo1(v:Vec2, modulus:Int -> Vec2; inline)
return Vec2{v.x mod1 modulus, v.y mod1 modulus}
test "arithmetic operators"
>> x := Vec2{10, 20}
>> y := Vec2{100, 200}
>> x + y
assert x + y == Vec2{x=110, y=220}
>> x - y
assert x - y == Vec2{x=-90, y=-180}
>> x * y
assert x * y == Vec2{x=1000, y=4000}
>> x.dot(y)
assert x.dot(y) == 5000
>> x * -1
assert x * -1 == Vec2{x=-10, y=-20}
>> -10 * x
assert -10 * x == Vec2{x=-100, y=-200}
test "update assignment operators"
>> x := Vec2{1, 2}
>> x += Vec2{10, 20}
assert x == Vec2{x=11, y=22}
>> x *= Vec2{10, -1}
assert x == Vec2{x=110, y=-22}
>> x *= -1
assert x == Vec2{x=-110, y=22}
test "negation"
>> x := Vec2{1, 2}
>> -x
assert -x == Vec2{x=-1, y=-2}
test "bitwise and division operators"
>> x := Vec2{1, 2}
>> y := Vec2{4, 3}
>> (x and y)
assert (x and y) == Vec2{x=0, y=2}
>> (x or y)
assert (x or y) == Vec2{x=5, y=3}
>> (x xor y)
assert (x xor y) == Vec2{x=5, y=1}
>> x / 2
assert x / 2 == Vec2{x=0, y=1}
>> x mod 3
assert x mod 3 == Vec2{x=1, y=2}
>> x mod1 3
assert x mod1 3 == Vec2{x=1, y=2}
|