This commit is contained in:
Andrey Parhomenko 2023-06-03 11:25:19 +03:00
parent 3807fc4bc2
commit 506e2772f7
6 changed files with 62 additions and 25 deletions

View file

@ -53,13 +53,14 @@ func NewRect() *Rect {
} }
func (r *Rect) Update(e *gx.Engine) error { func (r *Rect) Update(e *gx.Engine) error {
r.R += 0.3 * e.DT() //r.R += 0.3 * e.DT()
return nil return nil
} }
var ( var (
playerImg *gx.Image playerImg *gx.Image
player *Player player *Player
rectMove gx.Rectangle
rect *Rect rect *Rect
) )
@ -90,10 +91,11 @@ func (p *Player) Draw(e *gx.Engine, i *gx.Image) {
t := p.Transform t := p.Transform
t.S.X *= 4. t.S.X *= 4.
t.S.Y *= 4. t.S.Y *= 4.
rectMove = gx.Rectangle{
Transform: t,
}
r := &gx.DrawableRectangle{ r := &gx.DrawableRectangle{
Rectangle: gx.Rectangle{ Rectangle: rectMove,
Transform: t,
},
Color: gx.Color{0, 0, gx.MaxColorV, gx.MaxColorV}, Color: gx.Color{0, 0, gx.MaxColorV, gx.MaxColorV},
} }
r.Draw(e, i) r.Draw(e, i)
@ -184,7 +186,7 @@ func (d *Debug) Draw(
keyStrs = append(keyStrs, k.String()) keyStrs = append(keyStrs, k.String())
} }
if rect.ContainsPoint(player.P) { if rectMove.Vertices().Contained(rect).Len() > 0 {
keyStrs = append(keyStrs, "THIS IS SHIT") keyStrs = append(keyStrs, "THIS IS SHIT")
} }

View file

@ -1,8 +0,0 @@
package gx
// The structure represents any circles.
type Circle struct {
Transform
}

13
src/gx/elipse.go Normal file
View file

@ -0,0 +1,13 @@
package gx
// The structure represents elipses.
type Elipse struct {
// In transform S.X and S.Y represent
// coefficents for the corresponding axises.
Transform
}
func (e Elipse) ContainsPoint(p Point) bool {
return true
}

View file

@ -16,6 +16,9 @@ type Liner interface {
// The type represents a line segment. // The type represents a line segment.
type LineSegment [2]Point type LineSegment [2]Point
// The type represents multiple line segments.
type LineSegments []LineSegment
// Check whether the liner is parallel to the line. // Check whether the liner is parallel to the line.
func (l Line) Parallel(liner Liner) bool { func (l Line) Parallel(liner Liner) bool {
buf := liner.Line() buf := liner.Line()

View file

@ -9,9 +9,7 @@ import (
// The type describes rectangle geometry. // The type describes rectangle geometry.
type Rectangle struct { type Rectangle struct {
// Position of up left corner // P - position of the rotating center.
// and the point to
// rotate around(relatively of position, not absolute).
// Scale represent width and height. // Scale represent width and height.
Transform Transform
} }
@ -30,20 +28,27 @@ type DrawableRectangle struct {
Visible bool Visible bool
} }
// Return points of corners of the rectangle. // Return points of vertices of the rectangle.
func (r Rectangle) Corners() []Point { func (r Rectangle) Vertices() Points {
return []Point{}
}
// Get 2 triangles that the rectangle consists of.
func (r Rectangle) Triangles() Triangles {
m := r.Matrix() m := r.Matrix()
p1 := V(0, 0).Apply(&m) p1 := V(0, 0).Apply(&m)
p2 := V(1, 0).Apply(&m) p2 := V(1, 0).Apply(&m)
p3 := V(1, 1).Apply(&m) p3 := V(1, 1).Apply(&m)
p4 := V(0, 1).Apply(&m) p4 := V(0, 1).Apply(&m)
return Points{p1, p2, p3, p4}
}
func (r Rectangle) Edges() LineSegments {
return LineSegments{}
}
// Get 2 triangles that the rectangle consists of.
func (r Rectangle) Triangles() Triangles {
pts := r.Vertices()
p1 := pts[0]
p2 := pts[1]
p3 := pts[2]
p4 := pts[3]
//fmt.Println("in:", p1, p2, p3, p4) //fmt.Println("in:", p1, p2, p3, p4)
return Triangles{ return Triangles{

View file

@ -11,6 +11,11 @@ type Vector struct {
type Point = Vector type Point = Vector
type Vectors []Vector type Vectors []Vector
type Points []Point
type PointContainer interface {
ContainsPoint(Point) bool
}
func V(x, y Float) Vector { func V(x, y Float) Vector {
return Vector{x, y} return Vector{x, y}
@ -62,3 +67,20 @@ func (v Vector) Norm() Vector {
l := math.Sqrt(v.X*v.X + v.Y*v.Y) l := math.Sqrt(v.X*v.X + v.Y*v.Y)
return V(v.X / l, v.Y / l) return V(v.X / l, v.Y / l)
} }
func (pts Points) Contained(c PointContainer) Points {
ret := Points{}
for _, pt := range pts {
if c.ContainsPoint(pt) {
ret = append(ret, pt)
}
}
return ret
}
func (pts Points) Len() int {
return len(pts)
}