Implemented crossing for lines.
This commit is contained in:
parent
522e9a2d58
commit
3259b8ba8d
6 changed files with 59 additions and 24 deletions
|
@ -30,9 +30,9 @@ func main() {
|
||||||
|
|
||||||
l2 := gx.LineSegment{
|
l2 := gx.LineSegment{
|
||||||
gx.Point{0, 1},
|
gx.Point{0, 1},
|
||||||
gx.Point{1, 2},
|
gx.Point{1, 0},
|
||||||
}.Line()
|
}.Line()
|
||||||
fmt.Println(l1.Parallel(l2))
|
fmt.Println(l1.Crosses(l2))
|
||||||
/*t := gx.Triangle{
|
/*t := gx.Triangle{
|
||||||
gx.Point{0, 0},
|
gx.Point{0, 0},
|
||||||
gx.Point{0, 100},
|
gx.Point{0, 100},
|
||||||
|
|
|
@ -1,19 +1,11 @@
|
||||||
package gx
|
package gx
|
||||||
|
|
||||||
import (
|
|
||||||
"math"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Implements the camera component
|
// Implements the camera component
|
||||||
// for the main window.
|
// for the main window.
|
||||||
type Camera struct {
|
type Camera struct {
|
||||||
T Transform
|
T Transform
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
|
||||||
Pi = math.Pi
|
|
||||||
)
|
|
||||||
|
|
||||||
// Returns the matrix satysfying camera
|
// Returns the matrix satysfying camera
|
||||||
// position, scale and rotation to apply
|
// position, scale and rotation to apply
|
||||||
// it to the objects to get the real
|
// it to the objects to get the real
|
||||||
|
|
|
@ -27,6 +27,32 @@ func (l Line) Parallel(liner Liner) bool {
|
||||||
return false
|
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.
|
// 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) +
|
||||||
|
@ -38,10 +64,6 @@ func (ls LineSegment) Len() Float {
|
||||||
return math.Sqrt(ls.LenSqr())
|
return math.Sqrt(ls.LenSqr())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l Line) Line() Line {
|
|
||||||
return l
|
|
||||||
}
|
|
||||||
|
|
||||||
// Returns corresponding to the segment line line.
|
// Returns corresponding to the segment line line.
|
||||||
func (l LineSegment) Line() Line {
|
func (l LineSegment) Line() Line {
|
||||||
p0 := l[0]
|
p0 := l[0]
|
||||||
|
@ -53,8 +75,7 @@ func (l LineSegment) Line() Line {
|
||||||
return Line{k, c}
|
return Line{k, c}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (l LineSegment) Crosses(with Liner) (Point, bool) {
|
||||||
func (l LineSegment) Crosses(with any) (bool, Point) {
|
|
||||||
switch with.(type) {
|
switch with.(type) {
|
||||||
case Line :
|
case Line :
|
||||||
return l.crossesLine(with.(Line))
|
return l.crossesLine(with.(Line))
|
||||||
|
@ -63,15 +84,26 @@ func (l LineSegment) Crosses(with any) (bool, Point) {
|
||||||
default:
|
default:
|
||||||
panic("The type that is not defined to be crossed")
|
panic("The type that is not defined to be crossed")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l LineSegment) crossesLineSegment(with LineSegment) (bool, Point) {
|
func (l LineSegment) Contains(what any) bool {
|
||||||
return false, Point{}
|
switch what.(type) {
|
||||||
|
case Point :
|
||||||
|
return l.containsPoint(what.(Point))
|
||||||
|
default :
|
||||||
|
panic("Unexpected type")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l LineSegment) crossesLine(with Line) (bool, Point) {
|
func (l LineSegment) containsPoint(p Point) bool {
|
||||||
return false, Point{}
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l LineSegment) crossesLineSegment(with LineSegment) (Point, bool) {
|
||||||
|
return Point{}, false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l LineSegment) crossesLine(with Line) (Point, bool) {
|
||||||
|
return Point{}, false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,6 @@ import (
|
||||||
"github.com/surdeus/godat/src/poolx"
|
"github.com/surdeus/godat/src/poolx"
|
||||||
//"fmt"
|
//"fmt"
|
||||||
"time"
|
"time"
|
||||||
"math"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// The type represents order of drawing.
|
// The type represents order of drawing.
|
||||||
|
|
|
@ -10,6 +10,7 @@ type Float = float64
|
||||||
|
|
||||||
const (
|
const (
|
||||||
MaxFloat = math.MaxFloat64
|
MaxFloat = math.MaxFloat64
|
||||||
|
Pi = math.Pi
|
||||||
)
|
)
|
||||||
|
|
||||||
// Returns square of the value.
|
// Returns square of the value.
|
||||||
|
@ -17,3 +18,11 @@ func Sqr(v Float) Float {
|
||||||
return v * v
|
return v * v
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func RadiansToDegrees(v Float) Float {
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeegresToRadians(v Float) Float {
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
package gx
|
package gx
|
||||||
|
|
||||||
|
// The type represents math ray.
|
||||||
type Ray struct {
|
type Ray struct {
|
||||||
|
// The start point.
|
||||||
P Point
|
P Point
|
||||||
V Vector
|
// Rotation of the ray.
|
||||||
|
R Float
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue