painter.go: Optimize isTick function

This reduces the loop frequency to one or two iterations in all cases.
I have been unable to find any single line equation that can produce this same behavior, but one likely exists.
This commit is contained in:
Mike Jensen 2023-05-04 17:59:11 -06:00
parent e7a49c2c21
commit 19173dfd37
No known key found for this signature in database
GPG key ID: 2C909C9AAFB1047D

View file

@ -617,10 +617,12 @@ func (p *Painter) TextFit(body string, x, y, width int, textAligns ...string) ch
func isTick(totalRange int, numTicks int, index int) bool {
step := float64(totalRange-1) / float64(numTicks-1)
for i := 0; i < numTicks; i++ {
value := float64(i) * step
if int(value + 0.5) == index {
for i := int(float64(index) / step); i < numTicks; i++ {
value := int((float64(i) * step) + 0.5)
if value == index {
return true
} else if value > index {
break
}
}
return false