Simplified the line type.
This commit is contained in:
parent
75836cebe9
commit
07915de2a1
2 changed files with 6 additions and 15 deletions
|
@ -21,9 +21,7 @@ func main() {
|
||||||
}.Line(),
|
}.Line(),
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, l := range lines {
|
for _, l := range lines { fmt.Println(l) }
|
||||||
fmt.Println(l.Equation())
|
|
||||||
}
|
|
||||||
/*t := gx.Triangle{
|
/*t := gx.Triangle{
|
||||||
gx.Point{0, 0},
|
gx.Point{0, 0},
|
||||||
gx.Point{0, 100},
|
gx.Point{0, 100},
|
||||||
|
|
|
@ -4,17 +4,14 @@ import (
|
||||||
"math"
|
"math"
|
||||||
)
|
)
|
||||||
|
|
||||||
// The type represents mathematical equation of line.
|
// The type represents mathematical equation of line and line itself.
|
||||||
type LineEquation struct {
|
type Line struct {
|
||||||
K, C Float
|
K, C Float
|
||||||
}
|
}
|
||||||
|
|
||||||
// The type represents a line segment.
|
// The type represents a line segment.
|
||||||
type LineSegment [2]Point
|
type LineSegment [2]Point
|
||||||
|
|
||||||
// The type represents a line.
|
|
||||||
type Line LineSegment
|
|
||||||
|
|
||||||
// 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) +
|
||||||
|
@ -26,18 +23,14 @@ func (ls LineSegment) Len() Float {
|
||||||
return math.Sqrt(ls.LenSqr())
|
return math.Sqrt(ls.LenSqr())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns corresponding to the line segment line.
|
// Returns corresponding to the segment line line.
|
||||||
func (ls LineSegment) Line() Line {
|
func (l LineSegment) Line() Line {
|
||||||
return Line(ls)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l Line) Equation() LineEquation {
|
|
||||||
p0 := l[0]
|
p0 := l[0]
|
||||||
p1 := l[1]
|
p1 := l[1]
|
||||||
|
|
||||||
k := (p0.Y - p1.Y) / (p0.X - p1.X)
|
k := (p0.Y - p1.Y) / (p0.X - p1.X)
|
||||||
c := p0.Y - p0.X*k
|
c := p0.Y - p0.X*k
|
||||||
|
|
||||||
return LineEquation{k, c}
|
return Line{k, c}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue