divergenza.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/usr/bin/python
  2. '''
  3. Schotter pdf reproduction
  4. It design SchotterR
  5. Author: Luca COnte ''
  6. Website: polybius.fyi
  7. Last edited: June 2019
  8. '''
  9. import math,cairo,random,sys,configparser
  10. def plotSquares(ctx):
  11. global rsum
  12. for i in range(0,w-1):
  13. rsum += i*rs # add to the random value
  14. ctx.save()
  15. ctx.translate(s*i + deltaX(i), 0)
  16. for j in range(0,h):
  17. #
  18. rv = random.uniform(-rsum, rsum)
  19. print str(abs(rv))
  20. #grey = translate(abs(rv),0,2,0,0.7)
  21. #ctx.set_source_rgb(grey,grey,grey)
  22. ctx.save()
  23. #remove 0 or relace it with 1 if ypu want Y devercence
  24. ctx.translate(0, s*j + deltaY(i,j) * 0.8 )
  25. ctx.rotate(rv)
  26. ctx.rectangle(s/2,s/2, s, s)
  27. ctx.restore()
  28. ctx.stroke()
  29. #ctx.fill()
  30. ctx.restore()
  31. def deltaY(i,j):
  32. # return float(1.1*translate(j,0,h-1,-i*0.3,i*0.3)**2)
  33. # print "nterpolazione di "+ str(j) + " : " + str(translate(j,-h/2,h/2,-math.pi/2,math.pi/2))
  34. # return float(math.tan(translate(j,0,h-1,-math.pi/2,math.pi/2)))
  35. return float(math.tan(translate(j,0,h,-1.37,1.56))*i)
  36. def deltaX(i):
  37. return float(1.31**i*dp)
  38. def translate(value, leftMin, leftMax, rightMin, rightMax):
  39. # Figure out how 'wide' each range is
  40. leftSpan = leftMax - leftMin
  41. rightSpan = rightMax - rightMin
  42. # Convert the left range into a 0-1 range (float)
  43. valueScaled = float(value - leftMin) / float(leftSpan)
  44. # Convert the 0-1 range into a value in the right range.
  45. if(value==h/2):
  46. return 0
  47. return rightMin + (valueScaled * rightSpan)
  48. if len(sys.argv) < 2:
  49. print("Plz specify output file as argument")
  50. sys.exit(1)
  51. config = configparser.ConfigParser()
  52. config.read('cfg.ini')
  53. #float RGB touple
  54. FG_RGB_COLOR=(float(config['DEFAULT']['FG_R_COLOR']),float(config['DEFAULT']['FG_G_COLOR']),float(config['DEFAULT']['FG_B_COLOR']))
  55. #A4 in 300 DPI
  56. width, height = int(config['DEFAULT']['PAGE_W']),int(config['DEFAULT']['PAGE_H'])
  57. offsetX, offsetY = int(config['DEFAULT']['OFFSET_X']),int(config['DEFAULT']['OFFSET_Y'])
  58. w = int(config['DEFAULT']['GRID_W']) # width (how many squares - es. 22)
  59. h = int(config['DEFAULT']['GRID_H']) # height (how many squares - es. 12)
  60. s = int(config['DEFAULT']['SQUARE_SIZE'])# square size (es. 15)
  61. rs = float(config['DEFAULT']['RANDOM_STEP']) # random step (rotation increment in degrees)
  62. dp = float(config['DEFAULT']['DAMPEN']) # dampen (soften random effect for position)
  63. rsum = 0 # dummy initial value for rsum
  64. line_width = int(config['DEFAULT']['LINE_WIDTH'])
  65. surface = cairo.PDFSurface (sys.argv[1],width,height)
  66. ctx = cairo.Context (surface)
  67. ctx.set_source_rgb(FG_RGB_COLOR[0],FG_RGB_COLOR[1],FG_RGB_COLOR[2],)
  68. ctx.set_line_width(line_width)
  69. ctx.translate(offsetX, offsetY)
  70. plotSquares(ctx)
  71. ctx.show_page()