golnaz.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #!/usr/bin/python
  2. '''
  3. ZetCode PyCairo tutorial
  4. This code example draws a circle
  5. using the PyCairo library.
  6. Author: Jan Bodnar
  7. Website: zetcode.com
  8. Last edited: April 2016
  9. '''
  10. from gi.repository import Gtk
  11. import cairo
  12. import math
  13. import random
  14. class Example(Gtk.Window):
  15. def __init__(self):
  16. super(Example, self).__init__()
  17. self.init_ui()
  18. def init_ui(self):
  19. darea = Gtk.DrawingArea()
  20. darea.connect("draw", self.on_draw)
  21. self.add(darea)
  22. self.set_title("Fill & stroke")
  23. self.resize(3000, 2000)
  24. self.set_position(Gtk.WindowPosition.CENTER)
  25. self.connect("delete-event", Gtk.main_quit)
  26. self.show_all()
  27. def on_draw(self, wid, cr):
  28. stroke_size = 8
  29. line_factor = 1/8
  30. iterations = 10
  31. w, h = self.get_size()
  32. cr.set_line_width(stroke_size)
  33. cr.set_source_rgb(1, 0, 0)
  34. cr.translate(0, h/2)
  35. cr.move_to(0,0)
  36. cr.line_to(w,0)
  37. cr.stroke()
  38. cr.save()
  39. cr.set_source_rgb(0, 0, 0)
  40. #prima linea nera sotto
  41. cr.translate(0, stroke_size/2)
  42. stroke_size = 1
  43. cr.set_line_width(1)
  44. #qui il for delle linee sopr
  45. # drawBlackArea(cr,w,1,10)
  46. drawLines(cr, w, h,1, iterations)
  47. cr.restore()
  48. cr.set_source_rgb(0, 0, 0)
  49. stroke_size =8
  50. #prima linea nera sotto
  51. cr.translate(0, -stroke_size/2)
  52. stroke_size = 1
  53. cr.set_line_width(1)
  54. # drawBlackArea(cr,w,-1,10)
  55. drawLines(cr, w, h, -1, iterations)
  56. def drawLines(cr,w, h, direction, iterations):
  57. perc_partition = 20
  58. upper_bound = (int)(iterations*perc_partition/100)
  59. lower_bound = upper_bound - iterations
  60. print(str(lower_bound) + " -> " + str(upper_bound))
  61. for l in range(lower_bound,upper_bound):
  62. # cr.translate(0, direction * 1.05**l)
  63. cr.translate(0, direction * 2.7**l)
  64. cr.move_to(0,0)
  65. # cr.line_to(w,0)
  66. drawSegmentedLine(cr,w,l+upper_bound)
  67. cr.stroke()
  68. def drawBlackArea(cr,w, direction, iterations):
  69. for l in range(0,iterations):
  70. cr.translate(0, direction * 1)
  71. cr.move_to(0,0)
  72. cr.line_to(w,0)
  73. cr.stroke()
  74. def drawSegmentedLine(cr,w,currIt):
  75. print("segmented line")
  76. cr.save()
  77. rv = int(random.uniform(10,30))
  78. wresiduo=w
  79. for l in range(rv,0, -1):
  80. lw = wresiduo/l
  81. lw = lw + random.uniform(-5,5)
  82. deltaY = random.uniform(-currIt*0.3,currIt*0.3)
  83. cr.move_to(0,0)
  84. cr.line_to(lw,deltaY)
  85. cr.stroke()
  86. cr.translate(lw,deltaY)
  87. #cr.arc(0, 0, 5, 0, 2*math.pi)
  88. #cr.fill()
  89. wresiduo=wresiduo-lw
  90. cr.line_to(wresiduo,0)
  91. cr.stroke()
  92. cr.restore()
  93. def main():
  94. app = Example()
  95. Gtk.main()
  96. if __name__ == "__main__":
  97. main()