tinydraw icon indicating copy to clipboard operation
tinydraw copied to clipboard

FilledTriangle stretches too far horizontally

Open timboldt opened this issue 4 years ago • 8 comments

I was using a FilledTriangle to draw the hands on a clock, and I noticed that the triangle for the minute hand goes outside the boundaries of the clock near the 3 o'clock and 9 o'clock positions.

Assuming there is nothing wrong with math.Sincos(), I can only assume that something is not quite right with FilledTriangle().

My logic looks something like this:

	const (
		hourRadius       = 55
		minuteRadius     = 95
		hourWidth        = 6
		minuteWidth      = 5
	)

	// Draw the clock hands.
	minuteAngle := float64(t.Minute()) / 60 * 2 * math.Pi
	mx, my := math.Sincos(minuteAngle)
	hourAngle := float64(t.Hour()*60+t.Minute()) / 60 / 12 * 2 * math.Pi
	hx, hy := math.Sincos(hourAngle)
	tinydraw.Triangle(
		display,
		x-int16(hourWidth*hy), y-int16(hourWidth*hx),
		x+int16(hourWidth*hy), y+int16(hourWidth*hx),
		x+int16(hourRadius*hx), y-int16(hourRadius*hy),
		black)
	tinydraw.FilledTriangle(
		display,
		x-int16(minuteWidth*my), y-int16(minuteWidth*mx),
		x+int16(minuteWidth*my), y+int16(minuteWidth*mx),
		x+int16(minuteRadius*mx), y-int16(minuteRadius*my),
		black)

timboldt avatar Aug 11 '20 23:08 timboldt

At first glance the code seems ok, could you output the coordinates of the points to see if the error might be there? Which display are you using? I have drawn a few different clocks and gauges myself and don't remember any issue, but also don't remember setting it to 3-9 o'clock either. Code was based on Adafruit's GFX library https://github.com/adafruit/Adafruit-GFX-Library/blob/master/Adafruit_GFX.cpp#L619-L701 will check the implementation when possible.

conejoninja avatar Aug 12 '20 06:08 conejoninja

Okay, let me take a deeper look at it this weekend, and I'll see if I can find the exact inputs that reproduce the problem.

timboldt avatar Aug 12 '20 15:08 timboldt

I've also noticed some anomalies when filling triangles. I initally assumed this was due to the display driver I was implementing but I suspect something is wrong in tinydraw. I'll see if I can create a reproducible test.

spearson78 avatar Dec 10 '21 15:12 spearson78

No longer crashes due to #11. However, the shape of the 3 o'clock and 9 o'clock locations still looks a little odd. Also, the 6 o'clock spot seems a little thick.

And it seems that FilldTriangle() and Triangle() have different shapes.

image

souce code: https://gist.github.com/sago35/46ff4e8380e06ea6151c359e33ddb751

sago35 avatar Dec 11 '21 02:12 sago35

I created a pull request that re-uses the line drawing algorothm for filling the triangles unfortunately the changes were more significant than I anticipated when I started down this path. https://github.com/tinygo-org/tinydraw/pull/12

spearson78 avatar Dec 16 '21 15:12 spearson78

I created an additional pull request with some tests that reproduce the rendering issues. https://github.com/tinygo-org/tinydraw/pull/13

spearson78 avatar Dec 17 '21 15:12 spearson78

I finally spotted an additional error in the translation from c to go code. I should have noticed it with my fix for issue11 but somehow missed it. See https://github.com/tinygo-org/tinydraw/pull/13

The rendering is in my opinion not as good as the changes I made in https://github.com/tinygo-org/tinydraw/pull/12 but the change is much much simpler and easier to verify against the original code.

I'll leave it to the maintainers to decide which approach to take.

spearson78 avatar Dec 18 '21 10:12 spearson78

The FilledTriangle() is still a little misshaped, but it's much less of a problem now. The following is a drawing using the latest version of tinydraw.

image

sago35 avatar Feb 20 '22 02:02 sago35