def __init__(self) -> None: super().__init__() self.setupUi(self) self.canvas = Canvas(size=self.cell_size.value(), parent=self.centralwidget) self.horizontalLayout.addWidget(self.canvas) self.play.clicked.connect(self.start_game) self.pause.clicked.connect(self.pause_game) self.clear.clicked.connect(self.canvas.clear) self.new_btn.clicked.connect(self.new_game) self.spawn_btn.clicked.connect(self.canvas.spawn) self.max_age.valueChanged.connect(self.control_aging) self.end_breeding_age.valueChanged.connect(self.control_aging) self.enable_aging_btn.stateChanged.connect( lambda x: self.control_aging(x)) self.color_scheme.currentIndexChanged.connect( lambda x: self.set_color_scheme(x)) self.upd_time_slider.valueChanged.connect( lambda x: self.set_upd_time(x)) self.canvas.next_generation_signal.generation_update.connect( lambda x: self.generationUpdateEvent(x))
def run() -> None: STEPS = 180 SIZE = 600 MIDDLE = SIZE // 2 canvas = Canvas(SIZE, SIZE) color = Color(1, 0, 0) color_increment = Color(0, 0, 1.0 / STEPS) position = point(0, 1, 0) rotate = rotation_z(-2 * math.pi / STEPS) translate = translation(MIDDLE, MIDDLE, 0) scale = scaling(SIZE // 3, SIZE // 3, 1) for i in range(STEPS): canvas_position = translate * scale * position assert isinstance(canvas_position, Point) canvas.write_pixel( int(round(canvas_position.x)), SIZE - int(round(canvas_position.y)), color, ) position = rotate * position color += color_increment ppm = PPM(canvas) file_name = "clocks.ppm" ppm.save_to_file(file_name) print(f"Output stored to {file_name}")
def test_canvas_to_ppm_header(self): c = Canvas(5, 3) outfile = 'test_canvas_to_ppm_header.ppm' c.to_ppm(outfile) with open(outfile, 'r') as infile: assert 'P3' in infile.readline() assert '5 3' in infile.readline() assert '255' in infile.readline() try: os.remove(outfile) except FileNotFoundError: pass
def test_canvas_to_ppm_body(self): c = Canvas(5, 3) c1 = Color(1.5, 0, 0) c2 = Color(0, 0.5, 0) c3 = Color(-0.5, 0, 1) c.write_pixel(0, 0, c1) c.write_pixel(2, 1, c2) c.write_pixel(4, 2, c3) outfile = 'test_canvas_to_ppm_body.ppm' c.to_ppm(outfile) with open(outfile, 'r') as infile: [infile.readline() for i in range(3)] # skip header body = list(map(str.strip, infile.readlines())) l1 = "255 0 0 0 0 0 0 0 0 0 0 0 0 0 0" l2 = "0 0 0 0 0 0 0 128 0 0 0 0 0 0 0" l3 = "0 0 0 0 0 0 0 0 0 0 0 0 0 0 255" assert body[0] == l1 assert body[1] == l2 assert body[2] == l3 try: os.remove(outfile) except FileNotFoundError: pass
def render_triangle(triangle: Triangle, color: Color, canvas: Canvas) -> Canvas: # find rectangle containing the considered triangle x_min: float = min(triangle.v1.x, triangle.v2.x, triangle.v3.x) x_max: float = max(triangle.v1.x, triangle.v2.x, triangle.v3.x) y_min: float = min(triangle.v1.y, triangle.v2.y, triangle.v3.y) y_max: float = max(triangle.v1.y, triangle.v2.y, triangle.v3.y) (px_min, py_min) = canvas.vector_to_index(Vector(x_min, y_min)) (px_max, py_max) = canvas.vector_to_index(Vector(x_max, y_max)) px_min: int = max(px_min, 0) px_max: int = min(px_max, canvas.width - 1) py_min: int = max(py_min, 0) py_max: int = min(py_max, canvas.height - 1) for px in range(px_min, px_max): for py in range(py_min, py_max): vector: Vector = canvas.index_to_vector(px, py) if triangle.contains(vector): canvas.draw(px, py, color) return canvas
def run() -> None: # Eye is at (0,0, 5) origin = point(0, 0, 5) shape = Sphere() # shape.set_transform(scaling(0.5, 1, 1)) shape.material.color = Color(0.9, 0.2, 1) light = PointLight(point(-10, 10, 10), Color(1, 1, 1)) canvas = Canvas(CANVAS_SIZE, CANVAS_SIZE) for i in range(CANVAS_SIZE): for j in range(CANVAS_SIZE): target = canvas_to_world(point(i, j, 0)) ray = Ray(origin, normalize(target - origin)) hit = find_hit(shape.intersect(ray)) if hit is not None: hit_point = position(ray, hit.t) normal = hit.shape.normal_at(hit_point) pixel_color = lighting(hit.shape.material, light, hit_point, -ray.direction, normal) canvas.write_pixel(i, j, pixel_color) PPM(canvas).save_to_file("sphere.ppm")
zpos = observed["ZPos"] pitch = observed["Pitch"] yaw = observed["Yaw"] return (is_alive, life, xpos, ypos, zpos, pitch, yaw) def getState(observed): damage_dealth = observed["DamageDealt"] damage_taken = observed["DamageTaken"] mobs_killed = observed["MobsKilled"] return (mobs_killed, damage_dealth, damage_taken) canvas = Canvas(mobsize=8).init_canvas() # if sys.version_info[0] == 2: # sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) # flush print output immediately # else: # import functools # print = functools.partial(print, flush=True) def run(): # Create default Malmo objects: agent_host = MalmoPython.AgentHost() try: agent_host.parse(sys.argv) except RuntimeError as e:
def test_canvas_init(self): c = Canvas(10, 32) assert c.width == 10 and c.height == 32 and all( [pixel == Color(0, 0, 0) for pixel in c.pixels])
def test_canvas_get_pixel_outofbounds(self): c = Canvas(10, 10) with self.assertRaises(IndexError): c[100, 100]
def test_canvas_write_pixel(self): c = Canvas(32, 32) col = Color(12, 3, 5) c.write_pixel(2, 3, col) assert c[2, 3] == col
from src.canvas import Canvas from src.color import Color from src.tupl import Vector, Point def tick(p, v): p += v v += Vector(0, -0.1, 0) + Vector(-0.01, 0, 0) return p, v c = Canvas(900, 550) col = Color(255, 255, 255) if __name__ == '__main__': p = Point(0, 1, 0) v = Vector(1, 1.8, 0).normalize() * 11.25 for i in range(100): X = int(p.x) Y = int(p.y) if p.y <= 0: break if (0 <= X < c.width) and (0 <= c.height - Y < c.height): c.write_pixel(X, c.height - Y, col) p, v = tick(p, v) c.to_ppm('trajectory.ppm')
from src.canvas import Canvas from src.color import Color from src.primitives import Sphere from src.ray import Ray from src.transformations import scaling, rotation_z from src.tupl import Point s = Sphere() t = rotation_z(pi / 4) @ scaling(0.5, 1, 1) s.set_transform(t) r_origin = Point(0, 0, -5) wall_z = 10 wall_size = 7 N = 100 c = Canvas(N, N) pixel_size = wall_size / N half = wall_size / 2 red = Color(255, 0, 0) for y in range(c.height): world_y = half - pixel_size * y for x in range(c.width): world_x = -half + pixel_size * x position = Point(world_x, world_y, wall_z) r = Ray(r_origin, (position - r_origin).normalize()) X = r.intersects(s) if X.hit is not None: c.write_pixel(x, y, red) c.to_ppm('circle.ppm')
def assign_canvas(context, var, width, height): context.variables[var] = Canvas(width, height)
class App(Ui_MainWindow, QMainWindow): def __init__(self) -> None: super().__init__() self.setupUi(self) self.canvas = Canvas(size=self.cell_size.value(), parent=self.centralwidget) self.horizontalLayout.addWidget(self.canvas) self.play.clicked.connect(self.start_game) self.pause.clicked.connect(self.pause_game) self.clear.clicked.connect(self.canvas.clear) self.new_btn.clicked.connect(self.new_game) self.spawn_btn.clicked.connect(self.canvas.spawn) self.max_age.valueChanged.connect(self.control_aging) self.end_breeding_age.valueChanged.connect(self.control_aging) self.enable_aging_btn.stateChanged.connect( lambda x: self.control_aging(x)) self.color_scheme.currentIndexChanged.connect( lambda x: self.set_color_scheme(x)) self.upd_time_slider.valueChanged.connect( lambda x: self.set_upd_time(x)) self.canvas.next_generation_signal.generation_update.connect( lambda x: self.generationUpdateEvent(x)) def new_game(self) -> None: self.canvas.size = self.cell_size.value() self.canvas.start() def start_game(self) -> None: if self.play.isChecked(): upd_time = self.upd_time_slider.value() self.canvas.continue_game(upd_time) self.pause.setChecked(False) self.play.setChecked(True) def pause_game(self) -> None: if self.pause.isChecked(): self.canvas.pause() self.play.setChecked(False) self.pause.setChecked(True) def set_upd_time(self, update_interval: int) -> None: self.upd_time.setText(f'{update_interval}ms') if self.play.isChecked(): self.canvas.continue_game(update_interval) def set_color_scheme(self, index: int) -> None: self.canvas.color_scheme = index self.canvas.redraw(next_generation=False) def control_aging(self, state: bool = True) -> None: max_age = self.max_age.value() end_breeding_age = self.end_breeding_age.value() self.groupBox_2.setEnabled(state) self.canvas.set_aging(state, max_age, end_breeding_age) def generationUpdateEvent(self, generation: int) -> None: self.generation_value.setText(str(generation)) def showEvent(self, event: QEvent) -> None: self.new_game()
from src.primitives import Sphere from src.ray import Ray from src.tupl import Point s = Sphere() r_origin = Point(0, 0, -5) wall_z = 10 wall_size = 7 light_position = Point(-10, 10, 10) light_color = Color(1, 1, 1) light = PointLight(light_position, light_color) N = 200 c = Canvas(N, N) pixel_size = wall_size / N half = wall_size / 2 for y in range(c.height): world_y = half - pixel_size * y for x in range(c.width): world_x = -half + pixel_size * x position = Point(world_x, world_y, wall_z) r = Ray(r_origin, (position - r_origin).normalize()) X = r.intersects(s) if X.hit is not None: point = r.position(X.hit.t) normal = X.hit.object.normal_at(point) eye = -r.direction color = X.hit.object.material.lighting(light, point, eye, normal)
from math import pi from src.canvas import Canvas from src.color import Color from src.transformations import rotation_z from src.tupl import Point c = Canvas(400, 400) origin = Point(0, 0, 0) r = pi / 6 hand = Point(0, 1, 0) * c.height * (3 / 8) t = rotation_z(r) for i in range(12): X, Y = int(hand.x + c.width / 2), int(hand.y + c.height / 2) c.write_pixel(X, Y, Color(255, 255, 255)) hand = t * hand c.to_ppm('clock.ppm')