Implemented crossing for lines.

This commit is contained in:
Andrey Parhomenko 2023-05-28 21:07:05 +03:00
parent 522e9a2d58
commit 3259b8ba8d
6 changed files with 59 additions and 24 deletions

View file

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

View file

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

View file

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

View file

@ -7,7 +7,6 @@ import (
"github.com/surdeus/godat/src/poolx"
//"fmt"
"time"
"math"
)
// The type represents order of drawing.

View file

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

View file

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