gg icon indicating copy to clipboard operation
gg copied to clipboard

invalid plot when increasing number of lines

Open sbinet opened this issue 4 years ago • 8 comments

hi there,

running the following program:

package main

import (
	"flag"

	"github.com/fogleman/gg"
)

func main() {
	const (
		W = 1024.0
		H = 1024.0
	)

	nn := flag.Int("n", 1000, "number of lines")
	flag.Parse()

	var (
		N  = float64(*nn)
		dx = float64(W) / float64(*nn)

		ylow = H / 3.0
		yhi  = 2.0 * H / 3.0
	)

	dc := gg.NewContext(W, H)
	dc.SetRGB(1, 1, 1)
	dc.Clear()
	dc.SetRGB(0, 0, 0)
	dc.SetLineWidth(1)
	var (
		x = 0.0
		y = ylow
	)
	dc.MoveTo(x, y)
	for i := 1; i < *nn; i++ {
		j := float64(i)
		x += dx
		switch {
		case j < 0.1*N:
			y = ylow
		case 0.1*N <= j && j < 0.5*N:
			y = yhi
		case 0.5*N <= j && j < 0.55*N:
			y = ylow
		case 0.55*N <= j && j < 0.9*N:
			y = yhi
		default:
			y = ylow
		}

		dc.LineTo(x, y)
	}
	dc.Stroke()
	dc.SavePNG("out.png")
}

with -n=1000 draws the following plot: out-1000

but when increasing to e.g. 10000, it gives the following one: out-10000

behaviour uncovered in gonum/plot#559

sbinet avatar Sep 23 '19 13:09 sbinet

(this was using [email protected])

sbinet avatar Sep 23 '19 13:09 sbinet

gentle ping :)

I don't know where to begin with... but if you give me a hand I could try to help tackle this

sbinet avatar Oct 15 '19 07:10 sbinet

That's strange indeed. I suspect it may be a bug in the underlying freetype/raster package.

fogleman avatar Oct 15 '19 14:10 fogleman

This effect occurs near around 8200 with a significant discontinuity at 8192. Given the precision of fixed.Int26_6, this piqued my curiosity as to whether there was adjacent comparisons between values in this type. Yep! Removing this call to flattenPath fixes the rendering issue here. Reducing the constant here to 4 also fixes the problem — this was previously 4, but there is no issue associated with the change, so I can't see what happens when I do this (as far as I can see, there is no substantive change to the test images).

kortschak avatar Oct 03 '21 22:10 kortschak

I took upon myself to steal @kortschak fix and provide it as a PR: https://github.com/fogleman/gg/pull/148

sbinet avatar Oct 22 '21 13:10 sbinet

@kortschak it seems gg isn't a super reactive repo anymore... (my other PR, sent in Dec 2020, to add Go-modules support hasn't been merged yet)

I'll give it another 2 weeks but... pfff. what should we do here? (I know I can't correctly maintain yet another fork)

sbinet avatar Oct 29 '21 13:10 sbinet

Yeah. I don't know. There aren't any others are there?

kortschak avatar Oct 29 '21 19:10 kortschak

I have forked fogleman/gg to https://sr.ht/~sbinet/gg

sbinet avatar Jan 18 '22 11:01 sbinet