mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-01 15:45:38 +00:00
0f05231c35
Signed-off-by: Elias Naur <mail@eliasnaur.com>
96 lines
1.6 KiB
Go
96 lines
1.6 KiB
Go
// SPDX-License-Identifier: Unlicense OR MIT
|
|
|
|
package f32
|
|
|
|
type Point struct {
|
|
X, Y float32
|
|
}
|
|
|
|
type Rectangle struct {
|
|
Min, Max Point
|
|
}
|
|
|
|
func (p Point) Add(p2 Point) Point {
|
|
return Point{X: p.X + p2.X, Y: p.Y + p2.Y}
|
|
}
|
|
|
|
func (p Point) Sub(p2 Point) Point {
|
|
return Point{X: p.X - p2.X, Y: p.Y - p2.Y}
|
|
}
|
|
|
|
func (p Point) Mul(s float32) Point {
|
|
return Point{X: p.X * s, Y: p.Y * s}
|
|
}
|
|
|
|
func (r Rectangle) Size() Point {
|
|
return Point{X: r.Dx(), Y: r.Dy()}
|
|
}
|
|
|
|
func (r Rectangle) Dx() float32 {
|
|
return r.Max.X - r.Min.X
|
|
}
|
|
|
|
func (r Rectangle) Dy() float32 {
|
|
return r.Max.Y - r.Min.Y
|
|
}
|
|
|
|
func (r Rectangle) Intersect(s Rectangle) Rectangle {
|
|
if r.Min.X < s.Min.X {
|
|
r.Min.X = s.Min.X
|
|
}
|
|
if r.Min.Y < s.Min.Y {
|
|
r.Min.Y = s.Min.Y
|
|
}
|
|
if r.Max.X > s.Max.X {
|
|
r.Max.X = s.Max.X
|
|
}
|
|
if r.Max.Y > s.Max.Y {
|
|
r.Max.Y = s.Max.Y
|
|
}
|
|
return r
|
|
}
|
|
|
|
func (r Rectangle) Union(s Rectangle) Rectangle {
|
|
if r.Min.X > s.Min.X {
|
|
r.Min.X = s.Min.X
|
|
}
|
|
if r.Min.Y > s.Min.Y {
|
|
r.Min.Y = s.Min.Y
|
|
}
|
|
if r.Max.X < s.Max.X {
|
|
r.Max.X = s.Max.X
|
|
}
|
|
if r.Max.Y < s.Max.Y {
|
|
r.Max.Y = s.Max.Y
|
|
}
|
|
return r
|
|
}
|
|
|
|
func (r Rectangle) Canon() Rectangle {
|
|
if r.Max.X < r.Min.X {
|
|
r.Min.X, r.Max.X = r.Max.X, r.Min.X
|
|
}
|
|
if r.Max.Y < r.Min.Y {
|
|
r.Min.Y, r.Max.Y = r.Max.Y, r.Min.Y
|
|
}
|
|
return r
|
|
}
|
|
|
|
func (r Rectangle) Empty() bool {
|
|
return r.Min.X >= r.Max.X || r.Min.Y >= r.Max.Y
|
|
}
|
|
|
|
func (r Rectangle) Add(p Point) Rectangle {
|
|
return Rectangle{
|
|
Point{r.Min.X + p.X, r.Min.Y + p.Y},
|
|
Point{r.Max.X + p.X, r.Max.Y + p.Y},
|
|
}
|
|
}
|
|
|
|
func (r Rectangle) Sub(p Point) Rectangle {
|
|
return Rectangle{
|
|
Point{r.Min.X - p.X, r.Min.Y - p.Y},
|
|
Point{r.Max.X - p.X, r.Max.Y - p.Y},
|
|
}
|
|
}
|