Ejemplo n.º 1
0
func Draw(img *image.RGBA, col color.RGBA, box shp.Box, shape shp.Shape) error {
	poly, ok := shape.(*shp.Polygon)
	if !ok {
		return fmt.Errorf("cannot draw non-polygon shape")
	}

	gc := draw2dimg.NewGraphicContext(img)
	gc.SetStrokeColor(col)
	gc.SetLineWidth(1)

	rect := img.Bounds()
	dx := float64(rect.Max.X-rect.Min.X) / (box.MaxX - box.MinX)
	dy := float64(rect.Max.Y-rect.Min.Y) / (box.MaxY - box.MinY)

	for i, start := range poly.Parts {
		end := len(poly.Points)
		if i+1 < len(poly.Parts) {
			end = int(poly.Parts[i+1])
		}
		part := poly.Points[start:end]

		path := draw2d.Path{}
		for j, p := range part {
			x := ((p.X - box.MinX) * dx)
			y := ((box.MaxY - p.Y) * dy)
			if j == 0 {
				path.MoveTo(x, y)
			} else {
				path.LineTo(x, y)
			}
		}
		path.Close()
		gc.Stroke(&path)
	}
	return nil
}