mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-06 09:55:40 +00:00
@@ -1,39 +1,55 @@
|
|||||||
// SPDX-License-Identifier: Unlicense OR MIT
|
// SPDX-License-Identifier: Unlicense OR MIT
|
||||||
|
/*
|
||||||
|
Package f32 is a float32 implementation of package image's
|
||||||
|
Point and Rectangle
|
||||||
|
|
||||||
|
The coordinate space has the origin in the top left
|
||||||
|
corner with the axes extending right and down.
|
||||||
|
*/
|
||||||
package f32
|
package f32
|
||||||
|
|
||||||
|
// A Point is a two dimensional point.
|
||||||
type Point struct {
|
type Point struct {
|
||||||
X, Y float32
|
X, Y float32
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// A Rectangle contains the points (X, Y) where Min.X <= X < Max.X,
|
||||||
|
// Min.Y <= Y < Max.Y.
|
||||||
type Rectangle struct {
|
type Rectangle struct {
|
||||||
Min, Max Point
|
Min, Max Point
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add return the point p+p2.
|
||||||
func (p Point) Add(p2 Point) Point {
|
func (p Point) Add(p2 Point) Point {
|
||||||
return Point{X: p.X + p2.X, Y: p.Y + p2.Y}
|
return Point{X: p.X + p2.X, Y: p.Y + p2.Y}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sub returns the vector p-p2.
|
||||||
func (p Point) Sub(p2 Point) Point {
|
func (p Point) Sub(p2 Point) Point {
|
||||||
return Point{X: p.X - p2.X, Y: p.Y - p2.Y}
|
return Point{X: p.X - p2.X, Y: p.Y - p2.Y}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Mul returns p scaled by s.
|
||||||
func (p Point) Mul(s float32) Point {
|
func (p Point) Mul(s float32) Point {
|
||||||
return Point{X: p.X * s, Y: p.Y * s}
|
return Point{X: p.X * s, Y: p.Y * s}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Size returns r's width and height.
|
||||||
func (r Rectangle) Size() Point {
|
func (r Rectangle) Size() Point {
|
||||||
return Point{X: r.Dx(), Y: r.Dy()}
|
return Point{X: r.Dx(), Y: r.Dy()}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Dx returns r's width.
|
||||||
func (r Rectangle) Dx() float32 {
|
func (r Rectangle) Dx() float32 {
|
||||||
return r.Max.X - r.Min.X
|
return r.Max.X - r.Min.X
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Dy returns r's Height.
|
||||||
func (r Rectangle) Dy() float32 {
|
func (r Rectangle) Dy() float32 {
|
||||||
return r.Max.Y - r.Min.Y
|
return r.Max.Y - r.Min.Y
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Intersect returns the intersection of r and s.
|
||||||
func (r Rectangle) Intersect(s Rectangle) Rectangle {
|
func (r Rectangle) Intersect(s Rectangle) Rectangle {
|
||||||
if r.Min.X < s.Min.X {
|
if r.Min.X < s.Min.X {
|
||||||
r.Min.X = s.Min.X
|
r.Min.X = s.Min.X
|
||||||
@@ -50,6 +66,7 @@ func (r Rectangle) Intersect(s Rectangle) Rectangle {
|
|||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Union returns the union of r and s.
|
||||||
func (r Rectangle) Union(s Rectangle) Rectangle {
|
func (r Rectangle) Union(s Rectangle) Rectangle {
|
||||||
if r.Min.X > s.Min.X {
|
if r.Min.X > s.Min.X {
|
||||||
r.Min.X = s.Min.X
|
r.Min.X = s.Min.X
|
||||||
@@ -66,6 +83,8 @@ func (r Rectangle) Union(s Rectangle) Rectangle {
|
|||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Canon returns the canonical version of r, where Min is to
|
||||||
|
// the upper left of Max.
|
||||||
func (r Rectangle) Canon() Rectangle {
|
func (r Rectangle) Canon() Rectangle {
|
||||||
if r.Max.X < r.Min.X {
|
if r.Max.X < r.Min.X {
|
||||||
r.Min.X, r.Max.X = r.Max.X, r.Min.X
|
r.Min.X, r.Max.X = r.Max.X, r.Min.X
|
||||||
@@ -76,10 +95,12 @@ func (r Rectangle) Canon() Rectangle {
|
|||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Empty reports whether r represents the empty area.
|
||||||
func (r Rectangle) Empty() bool {
|
func (r Rectangle) Empty() bool {
|
||||||
return r.Min.X >= r.Max.X || r.Min.Y >= r.Max.Y
|
return r.Min.X >= r.Max.X || r.Min.Y >= r.Max.Y
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add offsets r with the vector p.
|
||||||
func (r Rectangle) Add(p Point) Rectangle {
|
func (r Rectangle) Add(p Point) Rectangle {
|
||||||
return Rectangle{
|
return Rectangle{
|
||||||
Point{r.Min.X + p.X, r.Min.Y + p.Y},
|
Point{r.Min.X + p.X, r.Min.Y + p.Y},
|
||||||
@@ -87,6 +108,7 @@ func (r Rectangle) Add(p Point) Rectangle {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sub offsets r with the vector -p.
|
||||||
func (r Rectangle) Sub(p Point) Rectangle {
|
func (r Rectangle) Sub(p Point) Rectangle {
|
||||||
return Rectangle{
|
return Rectangle{
|
||||||
Point{r.Min.X - p.X, r.Min.Y - p.Y},
|
Point{r.Min.X - p.X, r.Min.Y - p.Y},
|
||||||
|
|||||||
Reference in New Issue
Block a user