Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func (imd *IMDraw) polyline(thickness float64, closed bool) {
- points := imd.getAndClearPoints()
- // filter identical adjacent points
- filtered := points[:1]
- for i := 1; i < len(points); i++ {
- if points[i] != points[i-1] {
- filtered = append(filtered, points[i])
- }
- }
- points = filtered
- if len(points) < 2 {
- return
- }
- // first point
- j, i := 0, 1
- normal := (points[i].pos - points[j].pos).Rotated(math.Pi / 2).Unit().Scaled(thickness / 2)
- if !closed {
- switch points[j].endshape {
- case NoEndShape:
- // nothing
- case SharpEndShape:
- imd.pushPt(points[j].pos+normal, points[j])
- imd.pushPt(points[j].pos-normal, points[j])
- imd.pushPt(points[j].pos+normal.Rotated(math.Pi/2), points[j])
- imd.fillPolygon()
- case RoundEndShape:
- imd.pushPt(points[j].pos, points[j])
- imd.fillEllipseArc(V(thickness, thickness)/2, normal.Angle(), normal.Angle()+math.Pi)
- }
- }
- imd.pushPt(points[j].pos+normal, points[j])
- imd.pushPt(points[j].pos-normal, points[j])
- // middle points
- for i := 0; i < len(points); i++ {
- j, k := i+1, i+2
- closing := false
- if j >= len(points) {
- if !closed {
- break
- }
- j -= len(points)
- closing = true
- }
- if k >= len(points) {
- k -= len(points)
- }
- ijNormal := (points[j].pos - points[i].pos).Rotated(math.Pi / 2).Unit().Scaled(thickness / 2)
- jkNormal := (points[k].pos - points[j].pos).Rotated(math.Pi / 2).Unit().Scaled(thickness / 2)
- orientation := 1.0
- if ijNormal.Cross(jkNormal) > 0 {
- orientation = -1.0
- }
- imd.pushPt(points[j].pos-ijNormal, points[j])
- imd.pushPt(points[j].pos+ijNormal, points[j])
- imd.fillPolygon()
- switch points[j].endshape {
- case NoEndShape:
- // nothing
- case SharpEndShape:
- imd.pushPt(points[j].pos, points[j])
- imd.pushPt(points[j].pos+ijNormal.Scaled(orientation), points[j])
- imd.pushPt(points[j].pos+jkNormal.Scaled(orientation), points[j])
- imd.fillPolygon()
- case RoundEndShape:
- imd.pushPt(points[j].pos, points[j])
- imd.fillEllipseArc(V(thickness, thickness)/2, ijNormal.Angle(), ijNormal.Angle()-math.Pi)
- imd.pushPt(points[j].pos, points[j])
- imd.fillEllipseArc(V(thickness, thickness)/2, jkNormal.Angle(), jkNormal.Angle()+math.Pi)
- }
- if !closing {
- imd.pushPt(points[j].pos+jkNormal, points[j])
- imd.pushPt(points[j].pos-jkNormal, points[j])
- }
- }
- // last point
- i, j = len(points)-2, len(points)-1
- normal = (points[j].pos - points[i].pos).Rotated(math.Pi / 2).Unit().Scaled(thickness / 2)
- imd.pushPt(points[j].pos-normal, points[j])
- imd.pushPt(points[j].pos+normal, points[j])
- imd.fillPolygon()
- if !closed {
- switch points[j].endshape {
- case NoEndShape:
- // nothing
- case SharpEndShape:
- imd.pushPt(points[j].pos+normal, points[j])
- imd.pushPt(points[j].pos-normal, points[j])
- imd.pushPt(points[j].pos+normal.Rotated(-math.Pi/2), points[j])
- imd.fillPolygon()
- case RoundEndShape:
- imd.pushPt(points[j].pos, points[j])
- imd.fillEllipseArc(V(thickness, thickness)/2, normal.Angle(), normal.Angle()-math.Pi)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement