-
-
Save caiorss/84fb5eef31a58e9b2a33cdf63af9b525 to your computer and use it in GitHub Desktop.
Draw the Lorenz system in Python/GTK
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# lorenz.py | |
# 5 March 2016 | |
# | |
# Copyright 2016 Jason Milldrum | |
# | |
# Draw the Lorenz system in a GTK window | |
# See https://en.wikipedia.org/wiki/Lorenz_system | |
import math | |
import gi | |
gi.require_version('Gtk', '3.0') | |
from gi.repository import Gtk | |
class MyWindow(Gtk.Window): | |
def __init__(self): | |
Gtk.Window.__init__(self, title="Lorenz Attractor") | |
self.move(100, 100) | |
self.drawing = Gtk.DrawingArea() | |
self.drawing.set_size_request(400, 400) | |
self.drawing.connect("draw", self.draw) | |
self.add(self.drawing) | |
self.coords = [] | |
def draw(self, widget, cr): | |
center_x = self.get_size()[0] / 2 | |
center_y = self.get_size()[1] / 2 | |
scale_x = self.get_size()[0] / 60 | |
scale_y = self.get_size()[1] / 60 | |
offset_y = center_y * 0.9 | |
cr.set_line_width(1) | |
cr.set_source_rgb(0.0, 0.0, 0.0) | |
cr.move_to(center_x, center_y - offset_y) | |
for i in self.coords: | |
# Draw the X-Z view | |
cr.line_to((i[0] * scale_x) + center_x, (i[2] * scale_y) + center_y - offset_y) | |
cr.stroke() | |
def lorenz(self): | |
h = 0.01 | |
sigma = 10.0 | |
rho = 28.0 | |
beta = 8.0 / 3.0 | |
x0 = 0.1 | |
y0 = 0.0 | |
z0 = 0.0 | |
for i in range(10000): | |
x1 = x0 + h * sigma * (y0 - x0) | |
y1 = y0 + h * (x0 * (rho - z0) - y0) | |
z1 = z0 + h * (x0 * y0 - beta * z0) | |
x0 = x1 | |
y0 = y1 | |
z0 = z1 | |
self.coords.append([x0, y0, z0]) | |
win = MyWindow() | |
win.connect("delete-event", Gtk.main_quit) | |
win.show_all() | |
win.lorenz() | |
Gtk.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment