diff --git a/src/cmd/math/main.go b/src/cmd/math/main.go new file mode 100644 index 0000000..79eebde --- /dev/null +++ b/src/cmd/math/main.go @@ -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)) + }*/ +} + + + diff --git a/src/gx/line-segment.go b/src/gx/line-segment.go deleted file mode 100644 index 79c8e84..0000000 --- a/src/gx/line-segment.go +++ /dev/null @@ -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()) -} - diff --git a/src/gx/line.go b/src/gx/line.go new file mode 100644 index 0000000..5d526d3 --- /dev/null +++ b/src/gx/line.go @@ -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} +} + diff --git a/src/gx/math.go b/src/gx/math.go index 3578a0d..c80ea15 100644 --- a/src/gx/math.go +++ b/src/gx/math.go @@ -1,5 +1,9 @@ package gx +import ( + "math" +) + // The type is used in all Engine interactions // where you need floating values. type Float = float64 diff --git a/src/gx/triangle.go b/src/gx/triangle.go index 6a36bae..4a1e133 100644 --- a/src/gx/triangle.go +++ b/src/gx/triangle.go @@ -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(