gg/ox/rect.go
2024-06-02 00:01:17 +05:00

99 lines
2.1 KiB
Go

package ox
import (
//"github.com/hajimehoshi/ebiten/v2"
//"github.com/hajimehoshi/ebiten/v2/vector"
//"fmt"
//"image"
)
import "surdeus.su/core/gg"
import "surdeus.su/core/gg/mx"
// The type describes rectangle geometry with
// way to move, rotate and scale it.
type Rectangle struct {
Transform
Width, Height mx.Float
}
// Return points of vertices of the rectangle.
func (r Rectangle) Vertices() mx.Vectors {
t := r.Transform
wh := mx.Vector{r.Width, r.Height}
t.SetAround(t.GetAround().Mul(wh))
m := t.GetMatrice()
p1 := mx.Vector{0, 0}.Apply(m)
p2 := mx.Vector{wh.X, 0}.Apply(m)
p3 := mx.Vector{wh.X, wh.Y}.Apply(m)
p4 := mx.Vector{0, wh.Y}.Apply(m)
return mx.Vectors{p1, p2, p3, p4}
}
func (r Rectangle) Edges() mx.Lines {
vs := r.Vertices()
return mx.Lines{
mx.Line{vs[0], vs[1]},
mx.Line{vs[1], vs[2]},
mx.Line{vs[2], vs[3]},
mx.Line{vs[3], vs[0]},
}
}
// Get 2 triangles that the rectangle consists of.
func (r Rectangle) MakeTriangles() mx.Triangles {
pts := r.Vertices()
p1 := pts[0]
p2 := pts[1]
p3 := pts[2]
p4 := pts[3]
return mx.Triangles{
mx.Triangle{p1, p2, p3},
mx.Triangle{p1, p4, p3},
}
}
// Check whether the rectangle contains the point.
func (r Rectangle) ContainsPoint(pt mx.Vector) bool {
return r.MakeTriangles().ContainsPoint(pt)
}
// Get the points that are contained in the rectangle.
func (r Rectangle) GetContainedPoints(
pts mx.Vectors,
) (mx.Vectors) {
return r.MakeTriangles().GetContainedPoints(pts)
}
// The type describes rectangle that can be drawn.
type DrawableRectangle struct {
Rectangle
Drawity
}
func (r *DrawableRectangle) Draw(c gg.Context) *gg.Drawing {
tri := &DrawableTriangles{}
tri.Drawity = r.Drawity
tri.Triangles = r.MakeTriangles()
return tri.Draw(c)
/*// Use the Color as base image if no is provided.
//var did bool
if r.Images[0] == nil {
r.Images[0] = NewImage(1, 1)
r.Images[0].Set(0, 0, r.Color)
}
w, h := r.Images[0].Size()
// Drawing with shader.
c.DrawRectShader(w, h, r.Shader, &ebiten.DrawRectShaderOptions{
GeoM: m,
Images: r.Images,
Uniforms: r.Uniforms,
})*/
return nil
}