Advertisement
Guest User

Untitled

a guest
Mar 18th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 3.05 KB | None | 0 0
  1. func (imd *IMDraw) polyline(thickness float64, closed bool) {
  2.     points := imd.getAndClearPoints()
  3.  
  4.     // filter identical adjacent points
  5.     filtered := points[:1]
  6.     for i := 1; i < len(points); i++ {
  7.         if points[i] != points[i-1] {
  8.             filtered = append(filtered, points[i])
  9.         }
  10.     }
  11.     points = filtered
  12.  
  13.     if len(points) < 2 {
  14.         return
  15.     }
  16.  
  17.     // first point
  18.     j, i := 0, 1
  19.     normal := (points[i].pos - points[j].pos).Rotated(math.Pi / 2).Unit().Scaled(thickness / 2)
  20.  
  21.     if !closed {
  22.         switch points[j].endshape {
  23.         case NoEndShape:
  24.             // nothing
  25.         case SharpEndShape:
  26.             imd.pushPt(points[j].pos+normal, points[j])
  27.             imd.pushPt(points[j].pos-normal, points[j])
  28.             imd.pushPt(points[j].pos+normal.Rotated(math.Pi/2), points[j])
  29.             imd.fillPolygon()
  30.         case RoundEndShape:
  31.             imd.pushPt(points[j].pos, points[j])
  32.             imd.fillEllipseArc(V(thickness, thickness)/2, normal.Angle(), normal.Angle()+math.Pi)
  33.         }
  34.     }
  35.  
  36.     imd.pushPt(points[j].pos+normal, points[j])
  37.     imd.pushPt(points[j].pos-normal, points[j])
  38.  
  39.     // middle points
  40.     for i := 0; i < len(points); i++ {
  41.         j, k := i+1, i+2
  42.  
  43.         closing := false
  44.         if j >= len(points) {
  45.             if !closed {
  46.                 break
  47.             }
  48.             j -= len(points)
  49.             closing = true
  50.         }
  51.         if k >= len(points) {
  52.             k -= len(points)
  53.         }
  54.  
  55.         ijNormal := (points[j].pos - points[i].pos).Rotated(math.Pi / 2).Unit().Scaled(thickness / 2)
  56.         jkNormal := (points[k].pos - points[j].pos).Rotated(math.Pi / 2).Unit().Scaled(thickness / 2)
  57.  
  58.         orientation := 1.0
  59.         if ijNormal.Cross(jkNormal) > 0 {
  60.             orientation = -1.0
  61.         }
  62.  
  63.         imd.pushPt(points[j].pos-ijNormal, points[j])
  64.         imd.pushPt(points[j].pos+ijNormal, points[j])
  65.         imd.fillPolygon()
  66.  
  67.         switch points[j].endshape {
  68.         case NoEndShape:
  69.             // nothing
  70.         case SharpEndShape:
  71.             imd.pushPt(points[j].pos, points[j])
  72.             imd.pushPt(points[j].pos+ijNormal.Scaled(orientation), points[j])
  73.             imd.pushPt(points[j].pos+jkNormal.Scaled(orientation), points[j])
  74.             imd.fillPolygon()
  75.         case RoundEndShape:
  76.             imd.pushPt(points[j].pos, points[j])
  77.             imd.fillEllipseArc(V(thickness, thickness)/2, ijNormal.Angle(), ijNormal.Angle()-math.Pi)
  78.             imd.pushPt(points[j].pos, points[j])
  79.             imd.fillEllipseArc(V(thickness, thickness)/2, jkNormal.Angle(), jkNormal.Angle()+math.Pi)
  80.         }
  81.  
  82.         if !closing {
  83.             imd.pushPt(points[j].pos+jkNormal, points[j])
  84.             imd.pushPt(points[j].pos-jkNormal, points[j])
  85.         }
  86.     }
  87.  
  88.     // last point
  89.     i, j = len(points)-2, len(points)-1
  90.     normal = (points[j].pos - points[i].pos).Rotated(math.Pi / 2).Unit().Scaled(thickness / 2)
  91.  
  92.     imd.pushPt(points[j].pos-normal, points[j])
  93.     imd.pushPt(points[j].pos+normal, points[j])
  94.     imd.fillPolygon()
  95.  
  96.     if !closed {
  97.         switch points[j].endshape {
  98.         case NoEndShape:
  99.             // nothing
  100.         case SharpEndShape:
  101.             imd.pushPt(points[j].pos+normal, points[j])
  102.             imd.pushPt(points[j].pos-normal, points[j])
  103.             imd.pushPt(points[j].pos+normal.Rotated(-math.Pi/2), points[j])
  104.             imd.fillPolygon()
  105.         case RoundEndShape:
  106.             imd.pushPt(points[j].pos, points[j])
  107.             imd.fillEllipseArc(V(thickness, thickness)/2, normal.Angle(), normal.Angle()-math.Pi)
  108.         }
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement