From ab32a830e6e7430ddc2f092df8fc57e7fa69d286 Mon Sep 17 00:00:00 2001 From: surdeus Date: Tue, 30 May 2023 13:12:11 +0300 Subject: [PATCH] Implemented checking whether point is in triangle. --- src/cmd/math/main.go | 9 ++++++--- src/gx/line.go | 9 +++++++++ src/gx/math.go | 13 +++++++++++++ src/gx/rect.go | 4 ++++ src/gx/triangle.go | 32 ++++++++++++++------------------ 5 files changed, 46 insertions(+), 21 deletions(-) diff --git a/src/cmd/math/main.go b/src/cmd/math/main.go index d70acc1..bcaac25 100644 --- a/src/cmd/math/main.go +++ b/src/cmd/math/main.go @@ -34,7 +34,8 @@ func main() { }.Line() fmt.Println(l1.Crosses(l2)) fmt.Println(l1.ContainsPoint(gx.Point{1, 4})) - /*t := gx.Triangle{ + + t := gx.Triangle{ gx.Point{0, 0}, gx.Point{0, 100}, gx.Point{100, 0}, @@ -42,6 +43,8 @@ func main() { points := []gx.Point{ gx.Point{}, + gx.Point{100, 0}, + gx.Point{0, 100}, gx.Point{.1, .1}, gx.Point{-1, -1}, gx.Point{1, 1}, @@ -51,8 +54,8 @@ func main() { } for _, p := range points { - fmt.Println(p, t.PointIsIn(p)) - }*/ + fmt.Println(t, p, t.ContainsPoint(p)) + } } diff --git a/src/gx/line.go b/src/gx/line.go index e5285a6..8269c5c 100644 --- a/src/gx/line.go +++ b/src/gx/line.go @@ -62,6 +62,15 @@ func (l1 Line) crossesLine(l2 Line) (Point, bool) { return Point{x, y}, true } +func (l LineSegment) ContainsPoint(p Point) bool { + line := l.Line() + if !line.ContainsPoint(p) { + return false + } + + return false +} + // Get square of length of line segment. func (ls LineSegment) LenSqr() Float { return Sqr(ls[0].X - ls[1].X) + diff --git a/src/gx/math.go b/src/gx/math.go index 97f73fa..0b4dec3 100644 --- a/src/gx/math.go +++ b/src/gx/math.go @@ -18,6 +18,18 @@ func Sqr(v Float) Float { return v * v } +func Sgn(v Float) Float { + if v > 0 { + return 1 + } + + if v < 0 { + return -1 + } + + return 0 +} + func RadiansToDegrees(v Float) Float { return v } @@ -26,3 +38,4 @@ func DeegresToRadians(v Float) Float { return v } + diff --git a/src/gx/rect.go b/src/gx/rect.go index 8ade214..0252bcc 100644 --- a/src/gx/rect.go +++ b/src/gx/rect.go @@ -57,6 +57,10 @@ func (r Rectangle) Triangles() Triangles { return img }*/ +func (r Rectangle) ContainsPoint(p Point) bool { + return false +} + func NewImage(w, h int) (*Image) { return ebiten.NewImage(w, h) } diff --git a/src/gx/triangle.go b/src/gx/triangle.go index 4a1e133..e1e5bac 100644 --- a/src/gx/triangle.go +++ b/src/gx/triangle.go @@ -39,27 +39,23 @@ func (t Triangle) SideLengthSquares() ([3]Float) { } // Check whether the point is in the triangle. -func (t Triangle) PointIsIn(p Point) bool { - sls := t.SideLengthSquares() +func (t Triangle) ContainsPoint(p Point) bool { + d1 := Triangle{p, t[0], t[1]}.Sgn() + d2 := Triangle{p, t[1], t[2]}.Sgn() + d3 := Triangle{p, t[2], t[0]}.Sgn() - sl0 := LineSegment{p, t[0]}.LenSqr() - sl1 := LineSegment{p, t[1]}.LenSqr() - sl2 := LineSegment{p, t[2]}.LenSqr() + neg := (d1 < 0) || (d2 < 0) || (d3 < 0) + pos := (d1 > 0) || (d2 > 0) || (d3 > 0) - if sl0 > sls[0] || sl0 > sls[2] { - return false - } - - if sl1 > sls[0] || sl1 > sls[1] { - return false - } - - if sl2 > sls[1] || sl2 > sls[2] { - return false - } - - return true + return !(neg && pos) } + +func (t Triangle) Sgn() Float { + return (t[0].X - t[2].X) * (t[1].Y - t[2].Y) - + (t[1].X - t[2].X) * (t[0].Y - t[2].Y) +} + +//func (t Triangle) /* func (r *DrawableRectangle) Draw( e *Engine,