llgcode/draw2d

always get empty png

rxda opened this issue · 3 comments

rxda commented

I want to draw a gis polygon by draw2d,but always get a empty png.Can anybody help me?
This is my code


func draw() {
	var polygon = []geography.Point{
		{112.57612, 38.10083},
		{112.32417, 37.98218},
		{112.46313791855954, 37.76073805971019},
		{112.8047062349504, 37.7710048494001},
		{112.8839293045308, 38.03437195311656},
		{112.63716, 37.86334},
		{112.55924, 37.99446},
		{112.57612, 38.10083},
	}
	minx, miny := math.MaxFloat64, math.MaxFloat64
	maxx, maxy := 0.0, 0.0
	scale := 10000
	scaleF := float64(scale)
	for k, v := range polygon {
		if v.X() > maxx {
			maxx = v.X()
		}
		if v.Y() > maxy {
			maxy = v.Y()
		}
		if v.X() < minx {
			minx = v.X()
		}
		if v.Y() < miny {
			miny = v.Y()
		}
		polygon[k] = geography.Point{v.X() * scaleF, v.Y() * scaleF}
	}

	a, b, c, d := int(minx*scaleF)-1, int(miny*scaleF)-1, int(maxx*scaleF)+1, int(maxy*scaleF)+1
	dest := image.NewRGBA(image.Rect(a, b, c, d))
	gc := draw2dimg.NewGraphicContext(dest)
	// Set some properties
	gc.SetFillColor(color.RGBA{R: 0x44, G: 0xff, B: 0x44, A: 0xff})
	gc.SetStrokeColor(color.RGBA{R: 0x44, G: 0x44, B: 0x44, A: 0xff})
	gc.SetLineWidth(1)

	// Draw a closed shape
	gc.BeginPath()                            // Initialize a new path
	gc.MoveTo(polygon[0].X(), polygon[0].Y()) // Move to a position to start the new path
	for i := 1; i < len(polygon); i++ {
		gc.LineTo(polygon[i].X(), polygon[i].Y())
	}
	gc.Close()
	gc.FillStroke()
	// Save to file
	draw2dimg.SaveToPngFile("hello.png", dest)
}
rxda commented

geography.Point is [2]float64

Hi RXDA,
I guess the problem comes from the rectangle of the image.

rxda commented

Hi RXDA,
I guess the problem comes from the rectangle of the image.

thanks, I'll try fixing my code