commit 4583937d7979e43a619f56ced50692ed865fcc6b
parent 943155d8ee18ee5437b83c653b9550e6f5664196
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Sun, 28 May 2023 00:16:43 -0700
cleanup
Diffstat:
1 file changed, 10 insertions(+), 9 deletions(-)
diff --git a/pkg/captcha/image.go b/pkg/captcha/image.go
@@ -330,6 +330,13 @@ func (m *Image) createPoint(col, row int) Point {
pxY: float64(m.borderTop+row*(m.numHeight+m.digitVertSpacing)) + (float64(m.numHeight) / 2) + m.RandFloat(-m.centerOffset, m.centerOffset)}
}
+func RandChoice[T any](rnd *rand.Rand, arr []T) T {
+ if len(arr) == 0 {
+ panic("empty array")
+ }
+ return arr[rnd.Intn(len(arr))]
+}
+
func (m *Image) RandInt(min, max int) int {
if min == max {
return min
@@ -537,17 +544,13 @@ func (m *Image) renderUselessLines() {
d := 20.0
m.withState(func() {
for i := 0; i < 3; i++ {
- if m.RandInt(0, 1) == 0 {
- d *= -1
- }
+ d = RandChoice(m.rnd, []float64{d, -d})
pt1 := Point{pxX: 0, pxY: m.RandFloat(-50, 30+float64(m.imageHeight))}
pt2 := Point{pxX: float64(m.imageWidth), pxY: m.RandFloat(-30, 50+float64(m.imageHeight))}
m.renderPath([]Point{pt1, pt2})
}
for i := 0; i < 3; i++ {
- if m.RandInt(0, 1) == 0 {
- d *= -1
- }
+ d = RandChoice(m.rnd, []float64{d, -d})
pt1 := Point{pxX: 0, pxY: m.RandFloat(-30, 30+float64(m.imageHeight))}
pt2 := Point{pxX: float64(m.imageWidth), pxY: m.RandFloat(-30, 30+float64(m.imageHeight))}
grad := getGradient1(pt1, pt2, m.getLineColor(), m.getLineColor(), m.getLineColor())
@@ -557,9 +560,7 @@ func (m *Image) renderUselessLines() {
m.strokeCubicLine(pt1, pt2, d)
}
for i := 0; i < 3; i++ {
- if m.RandInt(0, 1) == 0 {
- d *= -1
- }
+ d = RandChoice(m.rnd, []float64{d, -d})
pt1 := Point{pxY: 0, pxX: m.RandFloat(-30, 30+float64(m.imageWidth))}
pt2 := Point{pxY: float64(m.imageHeight), pxX: m.RandFloat(-30, 30+float64(m.imageHeight))}
grad := getGradient1(pt1, pt2, m.getLineColor(), m.getLineColor(), m.getLineColor())