126 lines
2.9 KiB
Python
Executable file
126 lines
2.9 KiB
Python
Executable file
#!/usr/bin/python
|
|
|
|
'''
|
|
ZetCode PyCairo tutorial
|
|
|
|
This code example draws a circle
|
|
using the PyCairo library.
|
|
|
|
Author: Jan Bodnar
|
|
Website: zetcode.com
|
|
Last edited: April 2016
|
|
'''
|
|
|
|
from gi.repository import Gtk
|
|
import cairo
|
|
import math
|
|
import random
|
|
|
|
|
|
class Example(Gtk.Window):
|
|
|
|
def __init__(self):
|
|
super(Example, self).__init__()
|
|
|
|
self.init_ui()
|
|
|
|
|
|
def init_ui(self):
|
|
|
|
darea = Gtk.DrawingArea()
|
|
darea.connect("draw", self.on_draw)
|
|
self.add(darea)
|
|
|
|
self.set_title("Fill & stroke")
|
|
self.resize(3000, 2000)
|
|
self.set_position(Gtk.WindowPosition.CENTER)
|
|
self.connect("delete-event", Gtk.main_quit)
|
|
self.show_all()
|
|
|
|
|
|
def on_draw(self, wid, cr):
|
|
stroke_size = 8
|
|
line_factor = 1/8
|
|
iterations = 10
|
|
w, h = self.get_size()
|
|
|
|
cr.set_line_width(stroke_size)
|
|
cr.set_source_rgb(1, 0, 0)
|
|
|
|
|
|
cr.translate(0, h/2)
|
|
cr.move_to(0,0)
|
|
cr.line_to(w,0)
|
|
cr.stroke()
|
|
cr.save()
|
|
|
|
|
|
cr.set_source_rgb(0, 0, 0)
|
|
|
|
#prima linea nera sotto
|
|
cr.translate(0, stroke_size/2)
|
|
stroke_size = 1
|
|
cr.set_line_width(1)
|
|
#qui il for delle linee sopr
|
|
# drawBlackArea(cr,w,1,10)
|
|
drawLines(cr, w, h,1, iterations)
|
|
cr.restore()
|
|
|
|
cr.set_source_rgb(0, 0, 0)
|
|
stroke_size =8
|
|
#prima linea nera sotto
|
|
cr.translate(0, -stroke_size/2)
|
|
stroke_size = 1
|
|
cr.set_line_width(1)
|
|
# drawBlackArea(cr,w,-1,10)
|
|
drawLines(cr, w, h, -1, iterations)
|
|
|
|
def drawLines(cr,w, h, direction, iterations):
|
|
perc_partition = 20
|
|
upper_bound = (int)(iterations*perc_partition/100)
|
|
lower_bound = upper_bound - iterations
|
|
print(str(lower_bound) + " -> " + str(upper_bound))
|
|
for l in range(lower_bound,upper_bound):
|
|
# cr.translate(0, direction * 1.05**l)
|
|
cr.translate(0, direction * 2.7**l)
|
|
cr.move_to(0,0)
|
|
# cr.line_to(w,0)
|
|
drawSegmentedLine(cr,w,l+upper_bound)
|
|
cr.stroke()
|
|
|
|
def drawBlackArea(cr,w, direction, iterations):
|
|
for l in range(0,iterations):
|
|
cr.translate(0, direction * 1)
|
|
cr.move_to(0,0)
|
|
cr.line_to(w,0)
|
|
cr.stroke()
|
|
|
|
def drawSegmentedLine(cr,w,currIt):
|
|
print("segmented line")
|
|
cr.save()
|
|
rv = int(random.uniform(10,30))
|
|
wresiduo=w
|
|
for l in range(rv,0, -1):
|
|
lw = wresiduo/l
|
|
lw = lw + random.uniform(-5,5)
|
|
deltaY = random.uniform(-currIt*0.3,currIt*0.3)
|
|
cr.move_to(0,0)
|
|
cr.line_to(lw,deltaY)
|
|
cr.stroke()
|
|
cr.translate(lw,deltaY)
|
|
#cr.arc(0, 0, 5, 0, 2*math.pi)
|
|
#cr.fill()
|
|
wresiduo=wresiduo-lw
|
|
|
|
cr.line_to(wresiduo,0)
|
|
cr.stroke()
|
|
cr.restore()
|
|
|
|
def main():
|
|
|
|
app = Example()
|
|
Gtk.main()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|