Пример #1
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)
}
Пример #2
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
}
Пример #3
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)
}
Пример #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
}
Пример #5
0
// FillStyle demonstrates the difference between even odd and non zero winding rule.
func FillStyle(gc draw2d.GraphicContext, x, y, width, height float64) {
	sx, sy := width/232, height/220
	gc.SetLineWidth(width / 40)

	draw2dkit.Rectangle(gc, x+sx*0, y+sy*12, x+sx*232, y+sy*70)

	var wheel1, wheel2 draw2d.Path
	wheel1.ArcTo(x+sx*52, y+sy*70, sx*40, sy*40, 0, 2*math.Pi)
	wheel2.ArcTo(x+sx*180, y+sy*70, sx*40, sy*40, 0, -2*math.Pi)

	gc.SetFillRule(draw2d.FillRuleEvenOdd)
	gc.SetFillColor(color.NRGBA{0, 0xB2, 0, 0xFF})

	gc.SetStrokeColor(image.Black)
	gc.FillStroke(&wheel1, &wheel2)

	draw2dkit.Rectangle(gc, x, y+sy*140, x+sx*232, y+sy*198)
	wheel1.Clear()
	wheel1.ArcTo(x+sx*52, y+sy*198, sx*40, sy*40, 0, 2*math.Pi)
	wheel2.Clear()
	wheel2.ArcTo(x+sx*180, y+sy*198, sx*40, sy*40, 0, -2*math.Pi)

	gc.SetFillRule(draw2d.FillRuleWinding)
	gc.SetFillColor(color.NRGBA{0, 0, 0xE5, 0xFF})
	gc.FillStroke(&wheel1, &wheel2)
}