Created
July 7, 2022 12:47
-
-
Save hclivess/bf35ecac243c131e0252c94dce8b675f to your computer and use it in GitHub Desktop.
bitmap quantum montecarlo dirty code
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
| import time | |
| import matplotlib.pyplot as plt | |
| import numpy as np | |
| import scipy.stats as stats | |
| import math | |
| import cairo | |
| class TexasRanger: | |
| def __init__(self, x, y, curve): | |
| self.x_position = x | |
| self.y_position = y | |
| self.weight = 10 | |
| self.alive = True | |
| self.pixel_steps_y = +1 | |
| self.pixel_steps_x = 0 | |
| self.curve_match = False | |
| self.curve = curve | |
| self.steps_walked = 0 | |
| self.path = [] | |
| self.exhausted = False | |
| def descend(self): | |
| self.y_position = self.y_position + self.pixel_steps_y | |
| self.x_position = self.x_position + self.pixel_steps_x | |
| self.path.append([self.x_position, self.y_position]) | |
| self.steps_walked += 1 | |
| if self.steps_walked > 1000: | |
| self.exhausted = True | |
| def check_found(self): | |
| for x, y in self.curve: | |
| if int(self.x_position) == int(x) and int(self.y_position) == int(y): | |
| self.curve_match = True | |
| self.paint_walker(ctx) | |
| # paint collision dot | |
| ctx.set_source_rgb(1, 1, 0) | |
| ctx.move_to(self.x_position, self.y_position) | |
| ctx.arc(x, y, 10, 0, 2 * math.pi) | |
| ctx.fill() | |
| # paint collision dot | |
| def paint_walker(self, ctx): | |
| previous_x = None | |
| previous_y = None | |
| for x, y in self.path: | |
| if previous_x and previous_y: | |
| ctx.move_to(previous_x, previous_y) | |
| ctx.set_source_rgb(1, 0, 0) #100 red | |
| ctx.set_line_width(5) | |
| ctx.line_to(x, y) | |
| ctx.stroke() | |
| previous_x = x | |
| previous_y = y | |
| offset_x = 0 | |
| offset_y = 250 | |
| mu = 0 | |
| variance = 5 | |
| sigma = math.sqrt(variance) | |
| x = np.linspace(mu - 3 * sigma, mu + 3 * sigma, 100) | |
| x = x * 100 + offset_x # crutch | |
| y = stats.norm.pdf(x, mu, sigma) | |
| y = -1 * y * 100000 + offset_y # crutch | |
| curve_float = zip(x, y) | |
| curve = [] | |
| for x,y in curve_float: | |
| curve.append([int(x),int(y)]) | |
| surface = cairo.ImageSurface(cairo.FORMAT_RGB24, | |
| 1000, | |
| 1000) | |
| ctx = cairo.Context(surface) | |
| ctx.translate(500, 500) | |
| # ctx.move_to(0, 0) | |
| previous_x = None | |
| previous_y = None | |
| # paint curve | |
| for x, y in curve: | |
| if previous_x and previous_y: | |
| ctx.move_to(previous_x, previous_y) | |
| print(int(x), int(y)) | |
| ctx.line_to(x, y) | |
| # ctx.line_to(250*x, 2500*y) | |
| ctx.set_source_rgb(1, 1, 1) | |
| ctx.set_line_width(5) | |
| ctx.stroke() | |
| previous_x = x | |
| previous_y = y | |
| # paint curve | |
| for x in range(-500,500): | |
| # walker | |
| walker = TexasRanger(x=x, y=-500, curve=curve) | |
| while not walker.curve_match and not walker.exhausted: | |
| walker.descend() | |
| walker.check_found() | |
| # walker | |
| surface.write_to_png('line.png') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment