171 lines
4.5 KiB
Go
171 lines
4.5 KiB
Go
package validator
|
|
|
|
import (
|
|
"fmt"
|
|
)
|
|
|
|
type Validator struct {
|
|
printMode bool
|
|
r int
|
|
g int
|
|
b int
|
|
}
|
|
|
|
func NewValidator(print bool) Validator {
|
|
return Validator{print, 0, 0, 0}
|
|
}
|
|
|
|
func (val *Validator) Calculate(x int, y int) {
|
|
val.print("x = %d", x)
|
|
val.print("y = %d", y)
|
|
var u int = x - 36
|
|
val.print("u = %d - 36 = %d", x, u)
|
|
var v int = 18 - y
|
|
val.print("v = 18 - %d = %d", y, v)
|
|
var h int = u*u + v*v
|
|
val.print("h = %d² + %d² = %d + %d = %d", u, v, u*u, v*v, h)
|
|
if h < 200 {
|
|
val.print("⇒ h < 200 → %d < 200", h)
|
|
val.print("=> Continuing to section B")
|
|
val.sectionB(u, v, h)
|
|
} else if v < 0 {
|
|
val.print("⇒ h < 200 → %d ≮ 200", h)
|
|
val.print("⇒ v < 0 → %d < 0", v)
|
|
val.print("=> Continuing to section C")
|
|
val.sectionC(u, v, h)
|
|
} else {
|
|
val.print("⇒ h < 200 → %d ≮ 200", h)
|
|
val.print("⇒ v < 0 → %d ≮ 0", v)
|
|
val.print("=> Continuing to section D")
|
|
val.sectionD(x, y)
|
|
}
|
|
}
|
|
|
|
func (val *Validator) sectionB(u int, v int, h int) {
|
|
val.r = 420
|
|
val.print("R = %d", val.r)
|
|
val.b = 520
|
|
val.print("B = %d", val.b)
|
|
var t int = 5000 + 8*h
|
|
val.print("t = 5000 + 8 ⋅ %d = 5000 + %d = %d", h, 8*h, t)
|
|
|
|
var p int = val.mod(t*u, 2)
|
|
val.print("p = (%d ⋅ %d) | 2 = %d | 2 = %d", t, u, t*u, p)
|
|
var q int = val.mod(t*v, 2)
|
|
val.print("q = (%d ⋅ %d) | 2 = %d | 2 = %d", t, v, t*v, q)
|
|
var s int = 2 * q
|
|
val.print("s = 2 ⋅ %d = %d", q, s)
|
|
var w int = val.mod(1000+p-s, 2) + 8
|
|
val.print("w = (1000 + %d - %d) | 2 + 8 = (1000 + %d) | 2 + 8 = %d | 2 + 8 = %d + 8 = %d", p, s, p-s, 1000+p-s, val.mod(1000+p-s, 2), w)
|
|
if w > 0 {
|
|
val.print("⇒ w > 0 → %d > 0", w)
|
|
val.r = val.r + w*w
|
|
val.print(" R → R + %d² → R + %d ⇒ R = %d", w, w*w, val.r)
|
|
}
|
|
var o int = s + 2200
|
|
val.print("o = %d + 2200", s)
|
|
val.r = val.mod(val.r*o, 4)
|
|
val.print("R → R ⋅ %d | 4 ⇒ R = %d", o, val.r)
|
|
val.b = val.mod(val.b*o, 4)
|
|
val.print("B → B ⋅ %d | 4 ⇒ B = %d", o, val.b)
|
|
if p > -q {
|
|
val.print("⇒ p > -q → %d > -%d", p, q)
|
|
w = val.mod(p+q, 1)
|
|
val.print(" w = (%d + %q) | 1 = %d | 1 = %d", p, q, p+q, w)
|
|
val.r = val.r + w
|
|
val.print(" R → R + %d ⇒ R = %d", w, val.r)
|
|
val.b = val.b + w
|
|
val.print(" B → B + %d ⇒ B = %d", w, val.b)
|
|
}
|
|
val.print("=> Continuing to section E")
|
|
val.sectionE()
|
|
}
|
|
|
|
func (val *Validator) sectionC(u int, v int, h int) {
|
|
val.r = 150 + 2*v
|
|
val.print("R = 150 + 2 ⋅ %d = 150 + %d = %d", v, 2*v, val.r)
|
|
val.b = 50
|
|
val.print("B = %d", val.b)
|
|
var p int = h + 8*v*v
|
|
val.print("p = %d + 8 ⋅ %d² = %d + 8 ⋅ %d = %d + %d = %d", h, v, h, v*v, h, 8*v*v, p)
|
|
var c int = 240*(-v) - p
|
|
val.print("c = 240 ⋅ (-%d) - %d = %d - %d = %d", v, p, 240*(-v), p, c)
|
|
if c > 1200 {
|
|
val.print("⇒ c > 1200 → %d > 1200", c)
|
|
var o int = val.mod(6*c, 1)
|
|
val.print(" o = (6 ⋅ %d) | 1 = %d | 1 = %d", c, 6*c, o)
|
|
o = c * (1500 - o)
|
|
val.print(" o → %d ⋅ (1500 - o) = %d", c, o)
|
|
o = val.mod(o, 2) - 8360
|
|
val.print(" o → o | 2 - 8360 = %d", o)
|
|
val.r = val.mod(val.r*o, 3)
|
|
val.print(" R → R ⋅ %d | 3 ⇒ R = %d", o, val.r)
|
|
val.b = val.mod(val.b*o, 3)
|
|
val.print(" B → B ⋅ %d | 3 ⇒ B = %d", o, val.b)
|
|
}
|
|
var r int = c + u*v
|
|
val.print("r = %d + %d ⋅ %d = %d + %d = %d", c, u, v, c, u*v, r)
|
|
var d int = 3200 - h - 2*r
|
|
val.print("d = 3200 - %d - 2 ⋅ %d = %d + %d = %d", h, r, 3200-h, 2*r, d)
|
|
if d > 0 {
|
|
val.print("⇒ c > 0 → %d > 0", d)
|
|
val.r = val.r + d
|
|
val.print(" R → R + %d ⇒ R = %d", d, val.r)
|
|
}
|
|
val.print("=> Continuing to section E")
|
|
val.sectionE()
|
|
}
|
|
|
|
func (val *Validator) sectionD(x int, y int) {
|
|
var c int = x + 4*y
|
|
val.print("c = %d + 4 ⋅ %d", x, y)
|
|
val.r = 132 + c
|
|
val.print("R = 132 + %d = %d", c, val.r)
|
|
val.b = 192 + c
|
|
val.print("R = 192 + %d = %d", c, val.b)
|
|
val.print("=> Continuing to section E")
|
|
val.sectionE()
|
|
}
|
|
|
|
func (val *Validator) sectionE() {
|
|
if val.r > 255 {
|
|
val.r = 255
|
|
val.print("⇒ R > 255 → R = %d", val.r)
|
|
}
|
|
if val.b > 255 {
|
|
val.b = 255
|
|
val.print("⇒ B > 255 → B = %d", val.b)
|
|
}
|
|
val.g = val.mod(7*val.r+3*val.b, 1)
|
|
val.print("G = (7 + %d + 3 ⋅ %d) | 1 = (%d + %d) | 1 = %d | 1 = %d", val.r, val.b, 7+val.r, 3*val.b, 7*val.r+3*val.b, val.g)
|
|
}
|
|
|
|
func (val *Validator) mod(left int, right int) int {
|
|
l := left
|
|
if left < 0 {
|
|
l = -left
|
|
}
|
|
for i := 0; i < right-1; i++ {
|
|
l /= 10
|
|
}
|
|
r := 0
|
|
if l%10 < 5 {
|
|
r = l / 10
|
|
} else {
|
|
r = l/10 + 1
|
|
}
|
|
if left < 0 {
|
|
r = -r
|
|
}
|
|
return r
|
|
}
|
|
|
|
func (val *Validator) GetColor() (int, int, int) {
|
|
return val.r, val.g, val.b
|
|
}
|
|
|
|
func (val *Validator) print(str string, args... any) {
|
|
if val.printMode {
|
|
fmt.Printf(str + "\n", args...)
|
|
}
|
|
}
|