Skip to content

Instantly share code, notes, and snippets.

@Dioarya
Dioarya / program1.py
Last active December 1, 2022 03:47
1. Write a program, which given a number n = 1, 2, 3, ..., 10, creates a file named `table_n.txt` with the multiplication table of n 2. Write a program, which given a number n = 1, 2, 3, ..., 10, reads the file named `table_n.txt` and prints the numbers
# Take in input from the user.
while True:
num = None
# Check if the input is a valid integer.
try:
# Take in input from the user.
num = input("Input a number: ")
int(num) # Try to turn input into an integer
except ValueError:
print("Please input a correct integer.")
def heart(n):
s = ""
for i in range(n):
s0 = " " * (n - i - 1)
s1 = "*" * (i * 2 + 1)
s2 = " " * (2 * (n - i) - 1)
s3 = "*" * (i * 2 + 1)
s4 = " " * (n - i - 1)
s += s0 + s1 + s2 + s3 + s4 + "\n"
for i in range(1, n * 2):
@Dioarya
Dioarya / animator.py
Last active May 27, 2022 20:33
Animation broilerplate code
import time
fps = 60
animation_start = time.perf_counter()
skip_frames = 0
for framecount, frame in enumerate(animation):
if skip_frames > 0:
skip_frames -= 1
continue
frame_start = time.perf_counter()
@Dioarya
Dioarya / color.py
Last active April 4, 2022 11:35
Color conversions
def hsv_to_rgb(h, s, v) -> tuple[float, float, float]:
# 0 <= h <= 1
# 0 <= s <= 1
# 0 <= v <= 1
c = v * s
x = c * (1 - abs(h*6 % 2 - 1))
m = v - c
rgb = (
(c, x, 0),
@Dioarya
Dioarya / perlin.py
Last active March 6, 2022 20:44 — forked from eevee/perlin.py
Perlin noise in Python
from itertools import product
import math
import random
def smoothstep(t):
return t * t * (3. - 2. * t)
@Dioarya
Dioarya / rmod.py
Last active December 28, 2021 06:48
ranged modulo
def rmod(v: float, minimum: float, maximum: float) -> float:
"""modulo with minimum and maximum range
Args:
v (float): value
minimum (float): minimum
maximum (float): maximum
Returns:
float: result
@Dioarya
Dioarya / lerp.py
Last active July 24, 2022 04:36
Linear Interpolation
def lerp(v0, v1, t):
return (1 - t) * v0 + t * v1
def inv_lerp(a, b, v):
return (v - a) / (b - a)
def remap(min0, max0, min1, max1, value):
return lerp(min1, max1, inv_lerp(min0, nax1, value))
def remap(v, a0, a1, b0, b1):