diff --git a/src/cmd/math/main.go b/src/cmd/math/main.go index f6fc777..645bfe5 100644 --- a/src/cmd/math/main.go +++ b/src/cmd/math/main.go @@ -6,69 +6,16 @@ import ( ) 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) } - - l1 := gx.LineSegment{ - gx.Point{0, 0}, - gx.Point{1, 1}, - }.Line() - - l2 := gx.LineSegment{ - gx.Point{0, 1}, - gx.Point{1, 0}, - }.Line() - - fmt.Println(gx.LinersCross(l1, l2)) - fmt.Println(l1.ContainsPoint(gx.Point{1, 4})) - angle := gx.LinersAngle(l1, l2) - fmt.Println("angle:", angle, gx.RadiansToDegrees(angle)) - - - t := gx.Rectangle{ - Transform: gx.Transform{ - S: gx.Vector{100, 200}, - P: gx.Point{0, 200}, - RA: gx.Point{0, 0}, - R: 0, - }, - } - - points := []gx.Point{ - gx.Point{}, - gx.Point{100, 0}, - gx.Point{0, 99}, - gx.Point{.1, .1}, - gx.Point{-1, -1}, - gx.Point{1, 1}, - gx.Point{101, 1}, - gx.Point{100, 1}, - gx.Point{50, 1}, - } - - ts := t.Triangles() - t1 := ts[0] - t2 := ts[1] - fmt.Printf("Rectangle triangles:\n\t%v\n\t%v\n", t1, t2) - for _, p := range points { - fmt.Println(p, t.ContainsPoint(p)) + p := gx.Polygon{ + gx.Vertex{0, 0}, + gx.Vertex{0, 50}, + gx.Vertex{25, 50}, + gx.Vertex{30, 40}, + gx.Vertex{40, 30}, } + fmt.Println("barycenter:", p.Barycenter()) + fmt.Println("edges:", p.Edges()) } diff --git a/src/gx/polygon.go b/src/gx/polygon.go index 154060e..e9ad719 100644 --- a/src/gx/polygon.go +++ b/src/gx/polygon.go @@ -5,12 +5,79 @@ package gx // Should split it somehow. type Polygon []Point +// Returns slice of edges. func (p Polygon) Edges() Edges { ret := Edges{} - /*for i := range p { - ret = append(ret, Edge{}) - }*/ + + l := p.Len() + for i := range p { + ret = append(ret, Edge{p[i], p[(i+1)%l]}) + } return ret } +func (p Polygon) Len() int { + return len(p) +} + +func (p Polygon) Vertices() []Vertex { + return p +} + +func (p Polygon) Barycenter() Point { + ret := Point{} + for _, v := range p { + ret.X += v.X + ret.Y += v.Y + } + + l := Float(len(p)) + ret.X /= l + ret.Y /= l + + return ret +} + +/* +func (p Polygon) anyPointInside() Point { + edges := p.Edges() + for _, e := range edges { + if + } +} +*/ + +func (p Polygon) Triangles() Triangles { + /* + if len(p) < 3 { + return Triangles{} + } else + vertices = p.Vertices() + ret := Triangles{} + + i := 0 + for len(vertices) != 3{ + + i1 = i % len(vertices) + i2 = (i+1) % len(vertices) + i3 = (i+2) % len(vertices) + + l1 = LineSegment{vertices[i1], vertices[i2]}.Line() + l2 = LineSegment{vertices[i1], vertices[i3]}.Line() + + if l1.K > 0 { + if l1.K + } else { + } + } + + return append( + ret, + Triangle{p[0], p[1], p[2]}, + ) + */ + + return Triangles{} +} + diff --git a/src/gx/vector.go b/src/gx/vector.go index 3c658bc..d807ceb 100644 --- a/src/gx/vector.go +++ b/src/gx/vector.go @@ -22,6 +22,10 @@ func V(x, y Float) Vector { return Vector{x, y} } +func (v Vector) Eq(o Vector) bool { + return v.X == o.X && v.Y == o.Y +} + // Returns the vector with the matrix applied func (v Vector) Apply(m *Matrix) Vector { x, y := m.Apply(v.X, v.Y)