From 522e9a2d5820b84fa3161a51902b8dc0e12f0222 Mon Sep 17 00:00:00 2001 From: surdeus Date: Sun, 28 May 2023 20:02:07 +0300 Subject: [PATCH] Added the Liner interface for better and more general interaction. --- src/cmd/math/main.go | 11 +++++++++++ src/gx/line.go | 41 +++++++++++++++++++++++++++++++++++++++++ src/gx/main.go | 4 ---- 3 files changed, 52 insertions(+), 4 deletions(-) diff --git a/src/cmd/math/main.go b/src/cmd/math/main.go index 8891271..b029f3a 100644 --- a/src/cmd/math/main.go +++ b/src/cmd/math/main.go @@ -22,6 +22,17 @@ func main() { } for _, l := range lines { fmt.Println(l) } + + l1 := gx.LineSegment{ + gx.Point{0, 0}, + gx.Point{1, 1}, + }.Line() + + l2 := gx.LineSegment{ + gx.Point{0, 1}, + gx.Point{1, 2}, + }.Line() + fmt.Println(l1.Parallel(l2)) /*t := gx.Triangle{ gx.Point{0, 0}, gx.Point{0, 100}, diff --git a/src/gx/line.go b/src/gx/line.go index 2dfe1e8..3e65f39 100644 --- a/src/gx/line.go +++ b/src/gx/line.go @@ -9,9 +9,24 @@ type Line struct { K, C Float } +type Liner interface { + Line() Line +} + // The type represents a line segment. type LineSegment [2]Point +// Check whether the liner is parallel to the line. +func (l Line) Parallel(liner Liner) bool { + buf := liner.Line() + + if buf.K == l.K { + return true + } + + return false +} + // Get square of length of line segment. func (ls LineSegment) LenSqr() Float { return Sqr(ls[0].X - ls[1].X) + @@ -23,6 +38,10 @@ func (ls LineSegment) Len() Float { return math.Sqrt(ls.LenSqr()) } +func (l Line) Line() Line { + return l +} + // Returns corresponding to the segment line line. func (l LineSegment) Line() Line { p0 := l[0] @@ -34,3 +53,25 @@ func (l LineSegment) Line() Line { return Line{k, c} } + +func (l LineSegment) Crosses(with any) (bool, Point) { + switch with.(type) { + case Line : + return l.crossesLine(with.(Line)) + case LineSegment : + return l.crossesLineSegment(with.(LineSegment)) + default: + panic("The type that is not defined to be crossed") + } + + +} + +func (l LineSegment) crossesLineSegment(with LineSegment) (bool, Point) { + return false, Point{} +} + +func (l LineSegment) crossesLine(with Line) (bool, Point) { + return false, Point{} +} + diff --git a/src/gx/main.go b/src/gx/main.go index 2ec580d..70bc9d6 100644 --- a/src/gx/main.go +++ b/src/gx/main.go @@ -38,10 +38,6 @@ type Engine struct { type engine Engine -var ( - Infinity = math.MaxFloat64 -) - // Return current camera. func (e *Engine) Camera() *Camera { return e.camera