Ejemplo n.º 1
0
// DrawClosedLine draws the line represented by the Points and closes it.
func DrawClosedLine(p Points, width, height float64, name string) {
	// Initialize the graphic context on an RGBA image
	dest := image.NewRGBA(image.Rect(0, 0, int(width), int(height)))
	gc := draw2dimg.NewGraphicContext(dest)

	// Set some properties
	gc.SetStrokeColor(color.RGBA{0x44, 0x44, 0x44, 0xff})
	gc.SetLineWidth(5)

	paths := []*draw2d.Path{}

	// Draw a path
	for idx, point := range p[:len(p)] {
		path := new(draw2d.Path)
		path.MoveTo(float64(point.X), height-float64(point.Y))
		if idx < len(p)-1 {
			path.LineTo(float64(p[idx+1].X), height-float64(p[idx+1].Y))
		} else {
			path.LineTo(float64(p[0].X), height-float64(p[0].Y))
		}
		paths = append(paths, path)
	}
	gc.Stroke(paths...)
	gc.FillStroke()

	// Save to file
	draw2dimg.SaveToPngFile(name, dest)
}
Ejemplo n.º 2
0
func drawPolyLine(img *image.RGBA, color color.Color, coords [][]float64) {
	path := new(draw2d.Path)
	for i, coord := range coords {
		if i == 0 {
			path.MoveTo(coord[0], coord[1])
		} else {
			path.LineTo(coord[0], coord[1])
		}
	}

	gc := draw2dimg.NewGraphicContext(img)
	gc.SetStrokeColor(color)
	gc.Stroke(path)
}
Ejemplo n.º 3
0
func (r *Renderer) coordsAsPath(coords geom.Coordinates) *draw2d.Path {
	path := new(draw2d.Path)
	for i, point := range coords {
		x, y, _ := r.m.Srs.Forward(point[0], point[1])
		x, y = r.matrix.TransformPoint(x, y)
		if math.IsNaN(x) || math.IsInf(x, 1) {
			continue
		}
		if i == 0 {
			path.MoveTo(x, y)
		} else {
			path.LineTo(x, y)
		}
	}
	return path
}
Ejemplo n.º 4
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
}