diff --git a/src/cmd/math/main.go b/src/cmd/math/main.go index b029f3a..788dd45 100644 --- a/src/cmd/math/main.go +++ b/src/cmd/math/main.go @@ -30,9 +30,9 @@ func main() { l2 := gx.LineSegment{ gx.Point{0, 1}, - gx.Point{1, 2}, + gx.Point{1, 0}, }.Line() - fmt.Println(l1.Parallel(l2)) + fmt.Println(l1.Crosses(l2)) /*t := gx.Triangle{ gx.Point{0, 0}, gx.Point{0, 100}, diff --git a/src/gx/camera.go b/src/gx/camera.go index a6ac4b4..f8d7088 100644 --- a/src/gx/camera.go +++ b/src/gx/camera.go @@ -1,19 +1,11 @@ package gx -import ( - "math" -) - // Implements the camera component // for the main window. type Camera struct { T Transform } -const ( - Pi = math.Pi -) - // Returns the matrix satysfying camera // position, scale and rotation to apply // it to the objects to get the real diff --git a/src/gx/line.go b/src/gx/line.go index 3e65f39..3fcaf45 100644 --- a/src/gx/line.go +++ b/src/gx/line.go @@ -27,6 +27,32 @@ func (l Line) Parallel(liner Liner) bool { return false } +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 + } + + switch with.(type) { + case Line : + return l.crossesLine(with.(Line)) + case LineSegment : + return with.(LineSegment).crossesLine(l) + default: + panic("unhandled type") + } +} + +func (l1 Line) crossesLine(l2 Line) (Point, bool) { + x := (l1.C - l2.C) / (l2.K - l1.K) + y := l1.K*x + l1.C + return Point{x, y}, true +} + // Get square of length of line segment. func (ls LineSegment) LenSqr() Float { return Sqr(ls[0].X - ls[1].X) + @@ -38,10 +64,6 @@ 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] @@ -53,8 +75,7 @@ func (l LineSegment) Line() Line { return Line{k, c} } - -func (l LineSegment) Crosses(with any) (bool, Point) { +func (l LineSegment) Crosses(with Liner) (Point, bool) { switch with.(type) { case Line : return l.crossesLine(with.(Line)) @@ -63,15 +84,26 @@ func (l LineSegment) Crosses(with any) (bool, Point) { 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) Contains(what any) bool { + switch what.(type) { + case Point : + return l.containsPoint(what.(Point)) + default : + panic("Unexpected type") + } } -func (l LineSegment) crossesLine(with Line) (bool, Point) { - return false, Point{} +func (l LineSegment) containsPoint(p Point) bool { + return false +} + +func (l LineSegment) crossesLineSegment(with LineSegment) (Point, bool) { + return Point{}, false +} + +func (l LineSegment) crossesLine(with Line) (Point, bool) { + return Point{}, false } diff --git a/src/gx/main.go b/src/gx/main.go index 70bc9d6..863b7ea 100644 --- a/src/gx/main.go +++ b/src/gx/main.go @@ -7,7 +7,6 @@ import ( "github.com/surdeus/godat/src/poolx" //"fmt" "time" - "math" ) // The type represents order of drawing. diff --git a/src/gx/math.go b/src/gx/math.go index c80ea15..97f73fa 100644 --- a/src/gx/math.go +++ b/src/gx/math.go @@ -10,6 +10,7 @@ type Float = float64 const ( MaxFloat = math.MaxFloat64 + Pi = math.Pi ) // Returns square of the value. @@ -17,3 +18,11 @@ func Sqr(v Float) Float { return v * v } +func RadiansToDegrees(v Float) Float { + return v +} + +func DeegresToRadians(v Float) Float { + return v +} + diff --git a/src/gx/ray.go b/src/gx/ray.go index 772d928..6d63c87 100644 --- a/src/gx/ray.go +++ b/src/gx/ray.go @@ -1,7 +1,10 @@ package gx +// The type represents math ray. type Ray struct { + // The start point. P Point - V Vector + // Rotation of the ray. + R Float }