Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FilledTriangle stretches too far horizontally #9

Open
timboldt opened this issue Aug 11, 2020 · 8 comments
Open

FilledTriangle stretches too far horizontally #9

timboldt opened this issue Aug 11, 2020 · 8 comments

Comments

@timboldt
Copy link

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)
@conejoninja
Copy link
Member

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.

@timboldt
Copy link
Author

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.

@spearson78
Copy link
Contributor

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.

@sago35
Copy link
Member

sago35 commented Dec 11, 2021

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

@spearson78
Copy link
Contributor

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. #12

@spearson78
Copy link
Contributor

I created an additional pull request with some tests that reproduce the rendering issues. #13

@spearson78
Copy link
Contributor

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 #13

The rendering is in my opinion not as good as the changes I made in #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.

@sago35
Copy link
Member

sago35 commented Feb 20, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants