2023-10-23 15:45:18 +03:00
|
|
|
package gg
|
2023-05-11 12:28:34 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
//"github.com/hajimehoshi/ebiten/v2"
|
|
|
|
"math"
|
|
|
|
)
|
|
|
|
|
2023-12-24 15:05:34 +03:00
|
|
|
var (
|
|
|
|
ZV = V2(0)
|
|
|
|
)
|
|
|
|
|
2023-05-11 12:28:34 +03:00
|
|
|
type Vector struct {
|
|
|
|
X, Y Float
|
|
|
|
}
|
2023-05-30 14:34:10 +03:00
|
|
|
type Point = Vector
|
2023-06-15 20:18:58 +03:00
|
|
|
//type Vertex = Vector
|
2023-05-30 14:34:10 +03:00
|
|
|
|
|
|
|
type Vectors []Vector
|
2023-06-03 11:25:19 +03:00
|
|
|
type Points []Point
|
|
|
|
|
2023-05-11 12:28:34 +03:00
|
|
|
func V(x, y Float) Vector {
|
|
|
|
return Vector{x, y}
|
|
|
|
}
|
|
|
|
|
2023-12-24 15:05:34 +03:00
|
|
|
func V2(v Float) Vector {
|
2023-12-23 00:09:07 +03:00
|
|
|
return V(v, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v Vector) Div(o Vector) Vector {
|
|
|
|
return V(
|
|
|
|
v.X / o.X,
|
|
|
|
v.Y / o.Y,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v Vector) Scale(o Vector) Vector {
|
|
|
|
return V(
|
|
|
|
v.X * o.X,
|
|
|
|
v.Y * o.Y,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-06-10 17:43:04 +03:00
|
|
|
func (v Vector) Eq(o Vector) bool {
|
|
|
|
return v.X == o.X && v.Y == o.Y
|
|
|
|
}
|
|
|
|
|
2023-05-11 12:28:34 +03:00
|
|
|
// Returns the vector with the matrix applied
|
2024-01-08 02:53:00 +03:00
|
|
|
func (v Vector) Apply(m Matrix) Vector {
|
2023-05-11 12:28:34 +03:00
|
|
|
x, y := m.Apply(v.X, v.Y)
|
2024-01-08 02:53:00 +03:00
|
|
|
return Vector{x, y}
|
2023-05-11 12:28:34 +03:00
|
|
|
}
|
|
|
|
|
2023-05-30 14:34:10 +03:00
|
|
|
// Adds the vector to other one returning the result.
|
|
|
|
func (v Vector) Add(a ...Vector) Vector {
|
|
|
|
for _, r := range a {
|
|
|
|
v.X += r.X
|
|
|
|
v.Y += r.Y
|
|
|
|
}
|
|
|
|
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the subtraction of all the vectors from the current one.
|
|
|
|
func (v Vector) Sub(s ...Vector) Vector {
|
|
|
|
for _, r := range s {
|
|
|
|
v.X -= r.X
|
|
|
|
v.Y -= r.Y
|
|
|
|
}
|
|
|
|
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the negative version of the vector.
|
|
|
|
func (v Vector) Neg() Vector {
|
|
|
|
return Vector{
|
|
|
|
-v.X,
|
|
|
|
-v.Y,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-11 12:28:34 +03:00
|
|
|
// Returns the vector rotated by "a" angle in radians.
|
|
|
|
func (v Vector) Rotate(a Float) Vector {
|
2024-01-08 02:53:00 +03:00
|
|
|
m := Matrix{}
|
2023-05-11 12:28:34 +03:00
|
|
|
m.Rotate(a)
|
|
|
|
return v.Apply(m)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the normalized vector.
|
|
|
|
func (v Vector) Norm() Vector {
|
|
|
|
l := math.Sqrt(v.X*v.X + v.Y*v.Y)
|
|
|
|
return V(v.X / l, v.Y / l)
|
|
|
|
}
|
2023-06-03 11:25:19 +03:00
|
|
|
|
|
|
|
func (pts Points) Contained(c PointContainer) Points {
|
2024-01-08 06:09:36 +03:00
|
|
|
ret := make([]Point, 0, len(pts))
|
2023-06-03 11:25:19 +03:00
|
|
|
|
|
|
|
for _, pt := range pts {
|
|
|
|
if c.ContainsPoint(pt) {
|
|
|
|
ret = append(ret, pt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pts Points) Len() int {
|
|
|
|
return len(pts)
|
|
|
|
}
|
|
|
|
|