From a3cf74a971c0043bf835d6e3ba94d3b220e9c20f Mon Sep 17 00:00:00 2001 From: surdeus Date: Sun, 28 May 2023 21:22:52 +0300 Subject: [PATCH] Implemented ContainsPoint function. --- src/cmd/math/main.go | 1 + src/gx/line.go | 17 +++++++++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/cmd/math/main.go b/src/cmd/math/main.go index 788dd45..d70acc1 100644 --- a/src/cmd/math/main.go +++ b/src/cmd/math/main.go @@ -33,6 +33,7 @@ func main() { gx.Point{1, 0}, }.Line() fmt.Println(l1.Crosses(l2)) + fmt.Println(l1.ContainsPoint(gx.Point{1, 4})) /*t := gx.Triangle{ gx.Point{0, 0}, gx.Point{0, 100}, diff --git a/src/gx/line.go b/src/gx/line.go index 3fcaf45..e5285a6 100644 --- a/src/gx/line.go +++ b/src/gx/line.go @@ -31,12 +31,17 @@ func (l Line) Line() Line { return l } -func (l Line) Crosses(with Liner) (Point, bool) { - // Parallel liners cannot cross by definition. - if l.Parallel(with) { - return Point{}, false +func (l Line) ContainsPoint(p Point) bool { + buf := Line{0, p.Y} + pc, ok := l.crossesLine(buf) + if !ok { + return false } + return pc == p +} + +func (l Line) Crosses(with Liner) (Point, bool) { switch with.(type) { case Line : return l.crossesLine(with.(Line)) @@ -48,6 +53,10 @@ func (l Line) Crosses(with Liner) (Point, bool) { } func (l1 Line) crossesLine(l2 Line) (Point, bool) { + if l1.Parallel(l2) { + return Point{}, false + } + x := (l1.C - l2.C) / (l2.K - l1.K) y := l1.K*x + l1.C return Point{x, y}, true