From f97d517961f4d4a3cd7575a2a7f6aabffb91ab81 Mon Sep 17 00:00:00 2001 From: Felix Pankratz Date: Mon, 10 Jan 2022 16:41:29 +0100 Subject: [PATCH] moooooon --- moon.py | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ utils.py | 8 ++++++++ 2 files changed, 65 insertions(+) create mode 100755 moon.py diff --git a/moon.py b/moon.py new file mode 100755 index 0000000..5fced55 --- /dev/null +++ b/moon.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python3 + +import cairo +import math +import random +from utils import circle_fill +from utils import draw_crater +from utils import random_color + +WIDTH, HEIGHT = 1000, 1000 + +def main(): + + surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, WIDTH, HEIGHT) + ctx = cairo.Context(surface) + + ctx.scale(WIDTH, HEIGHT) # Normalizing the canvas + ctx.set_source_rgb(0.9, 0.9, 0.9) + # draw "full" moon + circle_fill(ctx, 0.5, 0.5, 0.5) + + # make sure we only draw "inside" + ctx.set_operator(cairo.Operator.ATOP) + + + + # draw craters + ctx.set_source_rgb(0.5, 0.5, 0.5) + for c in range(30): + #c_col = random.uniform(0.4, 0.6) + #ctx.set_source_rgb(c_col, c_col, c_col) + c_x = random.uniform(0.0, 1.0) + c_y = random.uniform(0.0, 1.0) + c_r = random.uniform(0.01, 0.1) + draw_crater(ctx, c_x, c_y, c_r) + + # draw "aura" + a_r, a_g, a_b = random_color() + p = cairo.RadialGradient(0.5, 0.5, 0.25, 0.5, 0.5, 0.5) + #p.add_color_stop_rgba(0.0, 0.9, 0.9, 0.9, 0.0) + p.add_color_stop_rgba(0.0, a_r, a_g, a_b, 0.0) + p.add_color_stop_rgba(0.3, a_r, a_g, a_b, 0.0) + p.add_color_stop_rgba(1.0, a_r, a_g, a_b, 0.8) + ctx.set_source(p) + ctx.arc(0.5, 0.5, 0.5, 0, math.pi*2) + #ctx.arc(0.5, 0.5, 0.3, 0, math.pi*2) + ctx.fill() + + # draw "shade" of the phase + ctx.set_source_rgba(0.1, 0.1, 0.1, 0.95) + phase_pos = random.uniform(-0.5, 1.5) + circle_fill(ctx, phase_pos, 0.5, 0.5) + + surface.write_to_png("out/moon.png") # Output to PNG + +if __name__ == '__main__': + main() diff --git a/utils.py b/utils.py index 48dca58..3469ac3 100644 --- a/utils.py +++ b/utils.py @@ -10,6 +10,14 @@ def circle_fill(ctx,x,y,r): ctx.arc(x,y,r,0,math.pi*2.) ctx.fill() +def draw_crater(ctx,x,y,r): + for n in range(3): + off_x = random.uniform(-0.01, 0.01) + off_y = random.uniform(-0.01, 0.01) + ctx.arc(x + off_x, y + off_y, r, 0, math.pi*2.) + ctx.fill() + + def random_color(): red = random.uniform(0, 1)