code: func dpWorker(points []geo.Point, threshold float64, mask []byte) int {
  
  	found := 0
  
  	var stack []int
  	stack = append(stack, 0, len(points)-1)
  
  	l := &geo.Line{}
  	for len(stack) > 0 {
  		start := stack[len(stack)-2]
  		end := stack[len(stack)-1]
  
  		// modify the line in place
  		a := l.A()
  		a[0], a[1] = points[start][0], points[start][1]
  
  		b := l.B()
  		b[0], b[1] = points[end][0], points[end][1]
  
  		maxDist := 0.0
  		maxIndex := 0
  		for i := start + 1; i < end; i++ {
  			dist := l.SquaredDistanceFrom(&points[i])
  
  			if dist > maxDist {
  				maxDist = dist
  				maxIndex = i
  			}
  		}
  
  		if maxDist > threshold*threshold {
  			found++
  			mask[maxIndex] = 1
  
  			stack[len(stack)-1] = maxIndex
  			stack = append(stack, maxIndex, end)
  		} else {
  			stack = stack[:len(stack)-2]
  		}
  	}
  
  	return found
  }
docs: // dpWorker does the recursive threshold checks.
  // Using a stack array with a stackLength variable resulted in 4x speed improvement
  // over calling the function recursively.