94 lines
1.9 KiB
Go
94 lines
1.9 KiB
Go
package gg
|
|
|
|
import (
|
|
//"github.com/hajimehoshi/ebiten/v2"
|
|
//"github.com/hajimehoshi/ebiten/v2/vector"
|
|
//"fmt"
|
|
//"image"
|
|
)
|
|
|
|
// The type describes rectangle geometry with
|
|
// way to move, rotate and scale it.
|
|
type Rectangle struct {
|
|
Object
|
|
Transform
|
|
Width, Height Float
|
|
}
|
|
|
|
// Return points of vertices of the rectangle.
|
|
func (r Rectangle) Vertices() Vertices {
|
|
t := r.Transform
|
|
wh := V(r.Width, r.Height)
|
|
t.SetAround(t.Around().Mul(wh))
|
|
m := t.Matrix()
|
|
p1 := V(0, 0).Apply(m)
|
|
p2 := V(wh.X, 0).Apply(m)
|
|
p3 := V(wh.X, wh.Y).Apply(m)
|
|
p4 := V(0, wh.Y).Apply(m)
|
|
return Vertices{p1, p2, p3, p4}
|
|
}
|
|
|
|
func (r Rectangle) Edges() Edges {
|
|
vs := r.Vertices()
|
|
return Edges{
|
|
Edge{vs[0], vs[1]},
|
|
Edge{vs[1], vs[2]},
|
|
Edge{vs[2], vs[3]},
|
|
Edge{vs[3], vs[0]},
|
|
}
|
|
}
|
|
|
|
// Get 2 triangles that the rectangle consists of.
|
|
func (r Rectangle) MakeTriangles() Triangles {
|
|
pts := r.Vertices()
|
|
p1 := pts[0]
|
|
p2 := pts[1]
|
|
p3 := pts[2]
|
|
p4 := pts[3]
|
|
|
|
return Triangles{
|
|
Triangle{p1, p2, p3},
|
|
Triangle{p1, p4, p3},
|
|
}
|
|
}
|
|
|
|
// Check whether the rectangle contains the point.
|
|
func (r Rectangle) ContainedPoints(pts Points) (Points) {
|
|
return r.MakeTriangles().ContainedPoints(pts)
|
|
}
|
|
|
|
// The type describes rectangle that can be drawn.
|
|
type DrawableRectangle struct {
|
|
Rectangle
|
|
ShaderOptions
|
|
|
|
// Solid color of the rectangle.
|
|
// Will be ignored if the Shader
|
|
// field is not nil.
|
|
Colority
|
|
}
|
|
|
|
func (r *DrawableRectangle) Draw(c *Context) []EVertex {
|
|
return (&DrawableTriangles{
|
|
Colority: r.Colority,
|
|
Triangles: r.MakeTriangles(),
|
|
}).MakeEVertices(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
|
|
}
|
|
|