2020-09-29 08:51:34 +02:00
|
|
|
#!/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
|
2020-09-29 17:57:50 +02:00
|
|
|
import random
|
2020-09-29 08:51:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
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 = 1000
|
|
|
|
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()
|
2020-09-29 17:57:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
cr.set_source_rgb(0, 0, 0)
|
2020-09-29 08:51:34 +02:00
|
|
|
|
|
|
|
#prima linea nera sotto
|
|
|
|
cr.translate(0, stroke_size/2)
|
|
|
|
stroke_size = 1
|
|
|
|
cr.set_line_width(1)
|
2020-09-29 17:57:50 +02:00
|
|
|
#qui il for delle linee sopr
|
|
|
|
drawBlackArea(cr,w,1,10)
|
2020-09-29 08:51:34 +02:00
|
|
|
drawLines(cr, w, h,1, iterations)
|
|
|
|
cr.restore()
|
2020-09-29 17:57:50 +02:00
|
|
|
|
|
|
|
cr.set_source_rgb(0, 0, 0)
|
2020-09-29 08:51:34 +02:00
|
|
|
stroke_size =8
|
|
|
|
#prima linea nera sotto
|
|
|
|
cr.translate(0, -stroke_size/2)
|
|
|
|
stroke_size = 1
|
|
|
|
cr.set_line_width(1)
|
2020-09-29 17:57:50 +02:00
|
|
|
drawBlackArea(cr,w,-1,10)
|
2020-09-29 08:51:34 +02:00
|
|
|
drawLines(cr, w, h, -1, iterations)
|
|
|
|
|
|
|
|
def drawLines(cr,w, h, direction, iterations):
|
|
|
|
for l in range(0,iterations):
|
|
|
|
cr.translate(0, direction * 1.05**l)
|
|
|
|
cr.move_to(0,0)
|
2020-09-29 17:57:50 +02:00
|
|
|
# cr.line_to(w,0)
|
|
|
|
drawSegmentedLine(cr,w,l)
|
|
|
|
cr.stroke()
|
|
|
|
|
|
|
|
def drawBlackArea(cr,w, direction, iterations):
|
|
|
|
for l in range(0,iterations):
|
|
|
|
cr.translate(0, direction * 1)
|
|
|
|
cr.move_to(0,0)
|
2020-09-29 08:51:34 +02:00
|
|
|
cr.line_to(w,0)
|
|
|
|
cr.stroke()
|
|
|
|
|
2020-09-29 17:57:50 +02:00
|
|
|
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,currIt)
|
|
|
|
cr.move_to(0,0)
|
|
|
|
cr.line_to(lw,deltaY)
|
|
|
|
cr.stroke()
|
2020-09-29 23:49:47 +02:00
|
|
|
cr.translate(lw,deltaY)
|
2020-09-29 17:57:50 +02:00
|
|
|
#cr.arc(0, 0, 5, 0, 2*math.pi)
|
|
|
|
#cr.fill()
|
|
|
|
wresiduo=wresiduo-lw
|
|
|
|
|
|
|
|
cr.line_to(wresiduo,0)
|
|
|
|
cr.stroke()
|
|
|
|
cr.restore()
|
2020-09-29 08:51:34 +02:00
|
|
|
|
|
|
|
def main():
|
|
|
|
|
|
|
|
app = Example()
|
|
|
|
Gtk.main()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|