Skip to content

Commit

Permalink
Add Closed() and Close() functions to Polyline, see #193
Browse files Browse the repository at this point in the history
  • Loading branch information
tdewolff committed Dec 12, 2022
1 parent 315f0b5 commit 581438e
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion polyline.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ func (p *Polyline) Add(x, y float64) *Polyline {
return p
}

// Close adds a new point equal to the first, closing the polyline.
func (p *Polyline) Close() *Polyline {
if 0 < len(p.coords) {
p.coords = append(p.coords, p.coords[0])
}
return p
}

// Closed returns true if the last point coincides with the first.
func (p *Polyline) Closed() bool {
return 0 < len(p.coords) && p.coords[0].Equals(p.coords[len(p.coords)-1])
}

// Coords returns the list of coordinates of the polyline.
func (p *Polyline) Coords() []Point {
return p.coords
Expand Down Expand Up @@ -80,14 +93,17 @@ func (p *Polyline) Smoothen() *Path {
if len(K) < 2 {
return &Path{}
} else if len(K) == 2 { // there are only two coordinates, that's a straight line
if p.Closed() {
return &Path{}
}
q := &Path{}
q.MoveTo(K[0].X, K[0].Y)
q.LineTo(K[1].X, K[1].Y)
return q
}

var p1, p2 []Point
closed := K[0].Equals(K[len(K)-1])
closed := p.Closed()
if closed {
// see http://www.jacos.nl/jacos_html/spline/circular/index.html
n := len(K) - 1
Expand Down

0 comments on commit 581438e

Please sign in to comment.