Implemented checking whether point is in triangle.

This commit is contained in:
Andrey Parhomenko 2023-05-30 13:12:11 +03:00
parent a3cf74a971
commit ab32a830e6
5 changed files with 46 additions and 21 deletions

View file

@ -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))
}
}

View file

@ -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) +

View file

@ -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
}

View file

@ -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)
}

View file

@ -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,