diff --git a/src/cmd/test/main.go b/src/cmd/test/main.go index cf1a426..8f86070 100644 --- a/src/cmd/test/main.go +++ b/src/cmd/test/main.go @@ -53,13 +53,14 @@ func NewRect() *Rect { } func (r *Rect) Update(e *gx.Engine) error { - r.R += 0.3 * e.DT() + //r.R += 0.3 * e.DT() return nil } var ( playerImg *gx.Image player *Player + rectMove gx.Rectangle rect *Rect ) @@ -90,10 +91,11 @@ func (p *Player) Draw(e *gx.Engine, i *gx.Image) { t := p.Transform t.S.X *= 4. t.S.Y *= 4. + rectMove = gx.Rectangle{ + Transform: t, + } r := &gx.DrawableRectangle{ - Rectangle: gx.Rectangle{ - Transform: t, - }, + Rectangle: rectMove, Color: gx.Color{0, 0, gx.MaxColorV, gx.MaxColorV}, } r.Draw(e, i) @@ -184,7 +186,7 @@ func (d *Debug) Draw( keyStrs = append(keyStrs, k.String()) } - if rect.ContainsPoint(player.P) { + if rectMove.Vertices().Contained(rect).Len() > 0 { keyStrs = append(keyStrs, "THIS IS SHIT") } diff --git a/src/gx/circle.go b/src/gx/circle.go deleted file mode 100644 index 968980c..0000000 --- a/src/gx/circle.go +++ /dev/null @@ -1,8 +0,0 @@ -package gx - -// The structure represents any circles. -type Circle struct { - Transform -} - - diff --git a/src/gx/elipse.go b/src/gx/elipse.go new file mode 100644 index 0000000..44c2cf1 --- /dev/null +++ b/src/gx/elipse.go @@ -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 +} + diff --git a/src/gx/line.go b/src/gx/line.go index 8269c5c..8c7c2c1 100644 --- a/src/gx/line.go +++ b/src/gx/line.go @@ -16,6 +16,9 @@ type Liner interface { // The type represents a line segment. type LineSegment [2]Point +// The type represents multiple line segments. +type LineSegments []LineSegment + // Check whether the liner is parallel to the line. func (l Line) Parallel(liner Liner) bool { buf := liner.Line() diff --git a/src/gx/rect.go b/src/gx/rect.go index 758e70e..d29d7af 100644 --- a/src/gx/rect.go +++ b/src/gx/rect.go @@ -9,9 +9,7 @@ import ( // The type describes rectangle geometry. type Rectangle struct { - // Position of up left corner - // and the point to - // rotate around(relatively of position, not absolute). + // P - position of the rotating center. // Scale represent width and height. Transform } @@ -30,20 +28,27 @@ type DrawableRectangle struct { Visible bool } -// Return points of corners of the rectangle. -func (r Rectangle) Corners() []Point { - return []Point{} -} - -// Get 2 triangles that the rectangle consists of. -func (r Rectangle) Triangles() Triangles { +// Return points of vertices of the rectangle. +func (r Rectangle) Vertices() Points { m := r.Matrix() - p1 := V(0, 0).Apply(&m) p2 := V(1, 0).Apply(&m) p3 := V(1, 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) return Triangles{ diff --git a/src/gx/vector.go b/src/gx/vector.go index 1cecca8..e9c9a41 100644 --- a/src/gx/vector.go +++ b/src/gx/vector.go @@ -11,6 +11,11 @@ type Vector struct { type Point = Vector type Vectors []Vector +type Points []Point + +type PointContainer interface { + ContainsPoint(Point) bool +} func V(x, y Float) Vector { return Vector{x, y} @@ -62,3 +67,20 @@ func (v Vector) Norm() Vector { l := math.Sqrt(v.X*v.X + v.Y*v.Y) 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) +} +