Implemented checking whether point is in triangle.
This commit is contained in:
parent
a3cf74a971
commit
ab32a830e6
5 changed files with 46 additions and 21 deletions
|
@ -34,7 +34,8 @@ func main() {
|
||||||
}.Line()
|
}.Line()
|
||||||
fmt.Println(l1.Crosses(l2))
|
fmt.Println(l1.Crosses(l2))
|
||||||
fmt.Println(l1.ContainsPoint(gx.Point{1, 4}))
|
fmt.Println(l1.ContainsPoint(gx.Point{1, 4}))
|
||||||
/*t := gx.Triangle{
|
|
||||||
|
t := gx.Triangle{
|
||||||
gx.Point{0, 0},
|
gx.Point{0, 0},
|
||||||
gx.Point{0, 100},
|
gx.Point{0, 100},
|
||||||
gx.Point{100, 0},
|
gx.Point{100, 0},
|
||||||
|
@ -42,6 +43,8 @@ func main() {
|
||||||
|
|
||||||
points := []gx.Point{
|
points := []gx.Point{
|
||||||
gx.Point{},
|
gx.Point{},
|
||||||
|
gx.Point{100, 0},
|
||||||
|
gx.Point{0, 100},
|
||||||
gx.Point{.1, .1},
|
gx.Point{.1, .1},
|
||||||
gx.Point{-1, -1},
|
gx.Point{-1, -1},
|
||||||
gx.Point{1, 1},
|
gx.Point{1, 1},
|
||||||
|
@ -51,8 +54,8 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, p := range points {
|
for _, p := range points {
|
||||||
fmt.Println(p, t.PointIsIn(p))
|
fmt.Println(t, p, t.ContainsPoint(p))
|
||||||
}*/
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -62,6 +62,15 @@ func (l1 Line) crossesLine(l2 Line) (Point, bool) {
|
||||||
return Point{x, y}, true
|
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.
|
// Get square of length of line segment.
|
||||||
func (ls LineSegment) LenSqr() Float {
|
func (ls LineSegment) LenSqr() Float {
|
||||||
return Sqr(ls[0].X - ls[1].X) +
|
return Sqr(ls[0].X - ls[1].X) +
|
||||||
|
|
|
@ -18,6 +18,18 @@ func Sqr(v Float) Float {
|
||||||
return v * v
|
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 {
|
func RadiansToDegrees(v Float) Float {
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
@ -26,3 +38,4 @@ func DeegresToRadians(v Float) Float {
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -57,6 +57,10 @@ func (r Rectangle) Triangles() Triangles {
|
||||||
return img
|
return img
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
|
func (r Rectangle) ContainsPoint(p Point) bool {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
func NewImage(w, h int) (*Image) {
|
func NewImage(w, h int) (*Image) {
|
||||||
return ebiten.NewImage(w, h)
|
return ebiten.NewImage(w, h)
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,27 +39,23 @@ func (t Triangle) SideLengthSquares() ([3]Float) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check whether the point is in the triangle.
|
// Check whether the point is in the triangle.
|
||||||
func (t Triangle) PointIsIn(p Point) bool {
|
func (t Triangle) ContainsPoint(p Point) bool {
|
||||||
sls := t.SideLengthSquares()
|
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()
|
neg := (d1 < 0) || (d2 < 0) || (d3 < 0)
|
||||||
sl1 := LineSegment{p, t[1]}.LenSqr()
|
pos := (d1 > 0) || (d2 > 0) || (d3 > 0)
|
||||||
sl2 := LineSegment{p, t[2]}.LenSqr()
|
|
||||||
|
|
||||||
if sl0 > sls[0] || sl0 > sls[2] {
|
return !(neg && pos)
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
if sl1 > sls[0] || sl1 > sls[1] {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
if sl2 > sls[1] || sl2 > sls[2] {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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(
|
func (r *DrawableRectangle) Draw(
|
||||||
e *Engine,
|
e *Engine,
|
||||||
|
|
Loading…
Reference in a new issue