2020-07-01 14:29:58 +03:00
|
|
|
// Copyright(C) 2019 Lars Pontoppidan. All rights reserved.
|
|
|
|
// Use of this source code is governed by an MIT license file distributed with this software package
|
|
|
|
module vec2
|
|
|
|
|
|
|
|
pub struct Vec2 {
|
|
|
|
pub mut:
|
2020-07-02 15:31:42 +03:00
|
|
|
x f64
|
|
|
|
y f64
|
2020-07-01 14:29:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut v Vec2) zero() {
|
2020-07-02 15:31:42 +03:00
|
|
|
v.x = 0.0
|
|
|
|
v.y = 0.0
|
2020-07-01 14:29:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut v Vec2) from(src Vec2) {
|
2020-07-02 15:31:42 +03:00
|
|
|
v.x = src.x
|
|
|
|
v.y = src.y
|
2020-07-01 14:29:58 +03:00
|
|
|
}
|
|
|
|
|
2020-07-02 15:31:42 +03:00
|
|
|
// * Addition
|
2020-07-01 14:29:58 +03:00
|
|
|
// + operator overload. Adds two vectors
|
2021-04-20 17:16:35 +03:00
|
|
|
pub fn (v1 Vec2) + (v2 Vec2) Vec2 {
|
2020-07-02 15:31:42 +03:00
|
|
|
return Vec2{v1.x + v2.x, v1.y + v2.y}
|
2020-07-01 14:29:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (v Vec2) add(vector Vec2) Vec2 {
|
2020-07-02 15:31:42 +03:00
|
|
|
return Vec2{v.x + vector.x, v.y + vector.y}
|
2020-07-01 14:29:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (v Vec2) add_f64(scalar f64) Vec2 {
|
2020-07-02 15:31:42 +03:00
|
|
|
return Vec2{v.x + scalar, v.y + scalar}
|
2020-07-01 14:29:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut v Vec2) plus(vector Vec2) {
|
2020-07-02 15:31:42 +03:00
|
|
|
v.x += vector.x
|
|
|
|
v.y += vector.y
|
2020-07-01 14:29:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut v Vec2) plus_f64(scalar f64) {
|
2020-07-02 15:31:42 +03:00
|
|
|
v.x += scalar
|
|
|
|
v.y += scalar
|
2020-07-01 14:29:58 +03:00
|
|
|
}
|
|
|
|
|
2020-07-02 15:31:42 +03:00
|
|
|
// * Subtraction
|
2021-04-20 17:16:35 +03:00
|
|
|
pub fn (v1 Vec2) - (v2 Vec2) Vec2 {
|
2020-07-02 15:31:42 +03:00
|
|
|
return Vec2{v1.x - v2.x, v1.y - v2.y}
|
2020-07-01 14:29:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (v Vec2) sub(vector Vec2) Vec2 {
|
2020-07-02 15:31:42 +03:00
|
|
|
return Vec2{v.x - vector.x, v.y - vector.y}
|
2020-07-01 14:29:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (v Vec2) sub_f64(scalar f64) Vec2 {
|
2020-07-02 15:31:42 +03:00
|
|
|
return Vec2{v.x - scalar, v.y - scalar}
|
2020-07-01 14:29:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut v Vec2) subtract(vector Vec2) {
|
2020-07-02 15:31:42 +03:00
|
|
|
v.x -= vector.x
|
|
|
|
v.y -= vector.y
|
2020-07-01 14:29:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut v Vec2) subtract_f64(scalar f64) {
|
2020-07-02 15:31:42 +03:00
|
|
|
v.x -= scalar
|
|
|
|
v.y -= scalar
|
2020-07-01 14:29:58 +03:00
|
|
|
}
|
|
|
|
|
2020-07-02 15:31:42 +03:00
|
|
|
// * Multiplication
|
2021-04-20 17:16:35 +03:00
|
|
|
pub fn (v1 Vec2) * (v2 Vec2) Vec2 {
|
2020-07-02 15:31:42 +03:00
|
|
|
return Vec2{v1.x * v2.x, v1.y * v2.y}
|
2020-07-01 14:29:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (v Vec2) mul(vector Vec2) Vec2 {
|
2020-07-02 15:31:42 +03:00
|
|
|
return Vec2{v.x * vector.x, v.y * vector.y}
|
2020-07-01 14:29:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (v Vec2) mul_f64(scalar f64) Vec2 {
|
2020-07-02 15:31:42 +03:00
|
|
|
return Vec2{v.x * scalar, v.y * scalar}
|
2020-07-01 14:29:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut v Vec2) multiply(vector Vec2) {
|
2020-07-02 15:31:42 +03:00
|
|
|
v.x *= vector.x
|
|
|
|
v.y *= vector.y
|
2020-07-01 14:29:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut v Vec2) multiply_f64(scalar f64) {
|
2020-07-02 15:31:42 +03:00
|
|
|
v.x *= scalar
|
|
|
|
v.y *= scalar
|
|
|
|
}
|