32 lines
997 B
Plaintext
32 lines
997 B
Plaintext
local fmt = require "fmt"
|
|
|
|
class vector2
|
|
static function polar(r, theta)
|
|
return new vector2(r * math.cos(theta), r * math.sin(theta))
|
|
end
|
|
|
|
static function scale(s, v) return v * s end
|
|
|
|
function __construct(public x, public y) end
|
|
|
|
function __add(v) return new vector2(self.x + v.x, self.y + v.y) end
|
|
function __sub(v) return new vector2(self.x - v.x, self.y - v.y) end
|
|
function __mul(s) return new vector2(self.x * s, self.y * s) end
|
|
function __div(s) return new vector2(self.x / s, self.y / s) end
|
|
|
|
function __tostring() return $"({self.x}, {self.y})" end
|
|
end
|
|
|
|
local v1 = new vector2(5, 7)
|
|
local v2 = new vector2(2, 3)
|
|
local v3 = vector2.polar(math.sqrt(2.0), math.pi / 4)
|
|
fmt.print("v1 = %s", v1)
|
|
fmt.print("v2 = %s", v2)
|
|
fmt.print("v3 = %s", v3)
|
|
print()
|
|
fmt.print("v1 + v2 = %s", v1 + v2)
|
|
fmt.print("v1 - v2 = %s", v1 - v2)
|
|
fmt.print("v1 * 11 = %s", v1 * 11)
|
|
fmt.print("11 * v2 = %s", vector2.scale(11, v2))
|
|
fmt.print("v1 / 2 = %s", v1 / 2)
|