This commit is contained in:
Andrey Parhomenko 2023-05-28 19:14:02 +03:00
parent e1268acd93
commit 75836cebe9
5 changed files with 116 additions and 19 deletions

49
src/cmd/math/main.go Normal file
View file

@ -0,0 +1,49 @@
package main
import (
"github.com/surdeus/gox/src/gx"
"fmt"
)
func main() {
lines := []gx.Line{
gx.LineSegment{
gx.Point{0, 1},
gx.Point{1, 2},
}.Line(),
gx.LineSegment{
gx.Point{0, 5},
gx.Point{1, 2},
}.Line(),
gx.LineSegment{
gx.Point{-1, -1},
gx.Point{1, 50},
}.Line(),
}
for _, l := range lines {
fmt.Println(l.Equation())
}
/*t := gx.Triangle{
gx.Point{0, 0},
gx.Point{0, 100},
gx.Point{100, 0},
}
points := []gx.Point{
gx.Point{},
gx.Point{.1, .1},
gx.Point{-1, -1},
gx.Point{1, 1},
gx.Point{101, 1},
gx.Point{100, 1},
gx.Point{50, 1},
}
for _, p := range points {
fmt.Println(p, t.PointIsIn(p))
}*/
}

View file

@ -1,17 +0,0 @@
package gx
import (
"math"
)
type LineSegment [2]Point
func (ls LineSegment) LenSqr() Float {
return Sqr(ls[0].X - ls[1].X) +
Sqr(ls[0].Y - ls[1].Y)
}
func (ls LineSegment) Len() Float {
return Sqrt(ls.LenSqr())
}

43
src/gx/line.go Normal file
View file

@ -0,0 +1,43 @@
package gx
import (
"math"
)
// The type represents mathematical equation of line.
type LineEquation struct {
K, C Float
}
// The type represents a line segment.
type LineSegment [2]Point
// The type represents a line.
type Line LineSegment
// Get square of length of line segment.
func (ls LineSegment) LenSqr() Float {
return Sqr(ls[0].X - ls[1].X) +
Sqr(ls[0].Y - ls[1].Y)
}
// Get length of the line segment.
func (ls LineSegment) Len() Float {
return math.Sqrt(ls.LenSqr())
}
// Returns corresponding to the line segment line.
func (ls LineSegment) Line() Line {
return Line(ls)
}
func (l Line) Equation() LineEquation {
p0 := l[0]
p1 := l[1]
k := (p0.Y - p1.Y) / (p0.X - p1.X)
c := p0.Y - p0.X*k
return LineEquation{k, c}
}

View file

@ -1,5 +1,9 @@
package gx
import (
"math"
)
// The type is used in all Engine interactions
// where you need floating values.
type Float = float64

View file

@ -33,14 +33,32 @@ func (t Triangle) SideLengthSquares() ([3]Float) {
l1 := LineSegment{t[0], t[1]}.LenSqr()
l2 := LineSegment{t[1], t[2]}.LenSqr()
l2 := LineSegment{t[2], t[0]}.LenSqr()
l3 := LineSegment{t[2], t[0]}.LenSqr()
return [3]Float{l1, l2, l3}
}
// Check whether the point is in the triangle.
func (t Triangle) PointIsIn(p Point) bool {
return false
sls := t.SideLengthSquares()
sl0 := LineSegment{p, t[0]}.LenSqr()
sl1 := LineSegment{p, t[1]}.LenSqr()
sl2 := LineSegment{p, t[2]}.LenSqr()
if sl0 > sls[0] || sl0 > sls[2] {
return false
}
if sl1 > sls[0] || sl1 > sls[1] {
return false
}
if sl2 > sls[1] || sl2 > sls[2] {
return false
}
return true
}
/*
func (r *DrawableRectangle) Draw(