gg/ox/rect.go

100 lines
2.1 KiB
Go
Raw Normal View History

2024-05-28 11:24:12 +03:00
package ox
2023-05-22 23:39:01 +03:00
import (
//"github.com/hajimehoshi/ebiten/v2"
2023-05-22 23:39:01 +03:00
//"github.com/hajimehoshi/ebiten/v2/vector"
//"fmt"
2023-05-22 23:39:01 +03:00
//"image"
)
2024-05-28 11:24:12 +03:00
import "surdeus.su/core/gg"
import "surdeus.su/core/gg/mx"
2023-05-22 23:39:01 +03:00
// The type describes rectangle geometry with
// way to move, rotate and scale it.
2023-05-22 23:39:01 +03:00
type Rectangle struct {
2024-06-01 16:07:28 +03:00
Transform
Width, Height mx.Float
2023-05-22 23:39:01 +03:00
}
2023-06-03 11:25:19 +03:00
// Return points of vertices of the rectangle.
2024-05-28 11:24:12 +03:00
func (r Rectangle) Vertices() mx.Vectors {
t := r.Transform
2024-06-01 16:07:28 +03:00
wh := mx.Vector{r.Width, r.Height}
t.SetAround(t.GetAround().Mul(wh))
m := t.GetMatrice()
2024-06-01 22:01:17 +03:00
2024-06-01 16:07:28 +03:00
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)
2024-06-01 22:01:17 +03:00
2024-05-28 11:24:12 +03:00
return mx.Vectors{p1, p2, p3, p4}
2023-06-03 11:25:19 +03:00
}
2024-05-28 11:24:12 +03:00
func (r Rectangle) Edges() mx.Lines {
2023-06-03 12:26:31 +03:00
vs := r.Vertices()
2024-06-01 16:07:28 +03:00
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]},
2023-06-03 12:26:31 +03:00
}
2023-06-03 11:25:19 +03:00
}
// Get 2 triangles that the rectangle consists of.
2024-05-28 11:24:12 +03:00
func (r Rectangle) MakeTriangles() mx.Triangles {
2023-06-03 11:25:19 +03:00
pts := r.Vertices()
p1 := pts[0]
p2 := pts[1]
p3 := pts[2]
p4 := pts[3]
2023-05-22 23:39:01 +03:00
2024-05-28 11:24:12 +03:00
return mx.Triangles{
mx.Triangle{p1, p2, p3},
mx.Triangle{p1, p4, p3},
2023-05-30 14:34:10 +03:00
}
}
2023-05-30 14:34:10 +03:00
// Check whether the rectangle contains the point.
2024-06-01 16:07:28 +03:00
func (r Rectangle) ContainsPoint(pt mx.Vector) bool {
return r.MakeTriangles().ContainsPoint(pt)
}
// Get the points that are contained in the rectangle.
2024-05-28 11:24:12 +03:00
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
2024-06-01 16:07:28 +03:00
Drawity
}
2024-06-01 16:07:28 +03:00
func (r *DrawableRectangle) Draw(c gg.Context) *gg.Drawing {
tri := &DrawableTriangles{}
tri.Drawity = r.Drawity
tri.Triangles = r.MakeTriangles()
return tri.Draw(c)
2023-05-26 18:31:04 +03:00
/*// Use the Color as base image if no is provided.
//var did bool
2023-05-26 18:31:04 +03:00
if r.Images[0] == nil {
r.Images[0] = NewImage(1, 1)
r.Images[0].Set(0, 0, r.Color)
2023-05-25 00:42:48 +03:00
}
2023-05-26 18:31:04 +03:00
w, h := r.Images[0].Size()
2023-05-25 00:42:48 +03:00
// Drawing with shader.
c.DrawRectShader(w, h, r.Shader, &ebiten.DrawRectShaderOptions{
GeoM: m,
2023-05-26 18:31:04 +03:00
Images: r.Images,
Uniforms: r.Uniforms,
})*/
return nil
}