tgenericconverter2.nim 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # bug #3799
  2. import strutils
  3. const output = splitLines("""
  4. 00000000000000000000000000000000000000000
  5. 00000000000001111111111111110000000000000
  6. 00000000001111111111111111111110000000000
  7. 00000000111111111111111111111111100000000
  8. 00000011111222222221111111111111111000000
  9. 00000111122222222222221111111111111100000
  10. 00001112222333333459432111111111111110000
  11. 00011122355544344463533221111111111111000
  12. 00111124676667556896443322211111111111100
  13. 00111126545561919686543322221111111111100
  14. 01111123333346967807554322222211111111110
  15. 01111122233334455582015332222221111111110
  16. 01111122222333344567275432222222111111110
  17. 01111112222222334456075443222222211111110
  18. 01111111222222233459965444332222221111110
  19. 01111111122222223457486554433322222111110
  20. 01111111112222222367899655543333322111110
  21. 01111111111122222344573948465444332111110
  22. 00111111111112222334467987727667762111100
  23. 00111111111111122233474655557836432111100
  24. 00011111111111112233 454433334 4321111000
  25. 00001111111111111122354333322222211110000
  26. 00000111111111111111222222222222111100000
  27. 00000001111111111111111122222111110000000
  28. 00000000111111111111111111111111100000000
  29. 00000000000111111111111111111100000000000
  30. 00000000000000111111111111100000000000000
  31. """)
  32. const nmax = 500
  33. type
  34. Complex*[T] = object
  35. re*: T
  36. im*: T
  37. converter toComplex*[T](x: tuple[re, im: T]): Complex[T] =
  38. result.re = x.re
  39. result.im = x.im
  40. proc julia*[T](z0, c: Complex[T], er2: T, nmax: int): int =
  41. result = 0
  42. var z = z0
  43. var sre = z0.re * z0.re
  44. var sim = z0.im * z0.im
  45. while (result < nmax) and (sre + sim < er2):
  46. z.im = z.re * z.im
  47. z.im = z.im + z.im
  48. z.im = z.im + c.im
  49. z.re = sre - sim + c.re
  50. sre = z.re * z.re
  51. sim = z.im * z.im
  52. inc result
  53. template dendriteFractal*[T](z0: Complex[T], er2: T, nmax: int): int =
  54. julia(z0, (T(0.0), T(1.0)), er2, nmax)
  55. iterator stepIt[T](start, step: T, iterations: int): (int, T) =
  56. for i in 0 .. iterations:
  57. yield (i, start + T(i) * step)
  58. var errors = 0
  59. let c = (0.36237, 0.32)
  60. for j, y in stepIt(2.0, -0.0375 * 4, 107 div 4):
  61. var row = ""
  62. for i, x in stepIt(-2.0, 0.025 * 4, 160 div 4):
  63. #let n = julia((x, y), c, 4.0, nmax) ### this works
  64. let n = dendriteFractal((x, y), 4.0, nmax)
  65. let c = char(int('0') + n mod 10)
  66. let e = output[j][i] # expected
  67. if c != e:
  68. errors += 1
  69. row.add(c)
  70. # Printing apparently makes the test fail when joined.
  71. # echo row
  72. doAssert errors < 10, "total errors: " & $errors