Skip to content

Instantly share code, notes, and snippets.

View ES-Alexander's full-sized avatar

ES-Alexander

View GitHub Profile
@ES-Alexander
ES-Alexander / point_in_polygon.py
Last active March 30, 2024 14:29
Some tools to (reasonably efficiently) fill shapes with infill patterns, as is common in 3D printing
import numpy as np
try:
# If available, import numba to enable compiling the functions for extra speed
from numba import njit, boolean
except ImportError:
print("Numba compilation not available - falling back to pure Python.")
def njit(func, *args, **kwargs):
@ES-Alexander
ES-Alexander / projective_perspective.py
Last active March 24, 2024 11:45
Projective Perspective
from fullcontrol.visualize.tube_mesh import FlowTubeMesh, go, np
# Display parameters
COLOURS = '#666' # 'black' / '#C0FFEE' / None
SHOW = True # if False, saves animation frames instead
# Geometry parameters
np.random.seed(sum(b'gcode'))
SEGMENTS_PER_CURVE = 31
BASE_THICKNESS = 0.6 # 0.2
@ES-Alexander
ES-Alexander / tube_mesh.py
Last active June 9, 2023 10:04
Generates a tubular triangle mesh that follows a path of points.
# relevant types
from collections.abc import Sequence
from numbers import Real
import pathlib
# functionality
from itertools import chain, pairwise
import plotly.graph_objects as go
import numpy as np
@ES-Alexander
ES-Alexander / random_drops.py
Created September 25, 2022 23:10
Random events with an average frequency
''' Written in response to u/thisisjusttofindajob's Reddit post
https://www.reddit.com/r/learnpython/comments/xnq9bv/how_to_render_objects_randomly_in_time/
about random timings with an average frequency.
'''
from time import perf_counter
import random
@ES-Alexander
ES-Alexander / process_drawing.py
Last active August 13, 2022 03:41
Clean up a drawing and add a transparent background
'''
Cleans up a drawing, and creates grey, blue, and red variants.
Blue and red are saved with a transparent background.
Grey is saved as single channel.
Author: ES-Alexander
License: MIT (a.k.a. free use, but not my problem if it doesn't work for you)
Created in response to:
https://www.reddit.com/r/opencv/comments/wmyuyh/question_remove_paper_background_from_sketches/
@ES-Alexander
ES-Alexander / Mandelbulb.py
Last active December 23, 2022 08:23
A Python-based 3D Mandelbulb Ray-Marcher, with phase shifting
''' Mandelbulb.py
Adapted from
https://github.com/AstroKriel/Mandelbulb/blob/main/python/main.py
EXAMPLE OUTPUT:
https://youtube.com/shorts/f8ek83Z9SFo
Original author: AstroKriel
- Mandelbulb raymarcher
@ES-Alexander
ES-Alexander / course_monitor.py
Created June 21, 2022 07:18
Minimal example for course tracking of a GPS-enabled MAVLink vehicle, using a low-pass IIR filter for averaging values over time.
#!/usr/bin/env python3
from pymavlink import mavutil, mavlink
class CourseAverager:
MSG_TYPE = 'GLOBAL_POSITION_INT'
def __init__(self, mavlink_connection, request_rate=1, decay_rate=0.95, initial_course=(0, 0)):
self.master = mavlink_connection
self.request_message(request_rate)
@ES-Alexander
ES-Alexander / voronoi.py
Last active March 25, 2022 17:17
image-based radial voronoi diagram with rectangular bounds
from scipy.spatial import KDTree
import matplotlib.pyplot as plt
import numpy as np
# SETUP
points = np.array([[1,2],[5,9],[-8,-2],[5,-5],[-4,-1]])
xmin, xmax, ymin, ymax = -10, 10, -10, 10
x_resolution = y_resolution = 300
pixel_size = max((xmax-xmin)/x_resolution, (ymax-ymin)/y_resolution)
radius = 3
@ES-Alexander
ES-Alexander / whalesong.py
Created February 14, 2022 11:27
Very rough initial analysis of some sound recordings to identify whale songs
from pathlib import Path
from scipy.io import wavfile
from scipy.fft import fft, fftfreq
import matplotlib.pyplot as plt
import numpy as np
path = Path('.') # path to whale song files
files = list(path.glob('*.wav'))
#!/usr/bin/env python3
from itertools import combinations_with_replacement
from typing import List, Tuple, Generator
VALID_E_SERIES = (3, 6, 12, 24, 48, 96, 192)
def E(m: int) -> Generator[float, None, None]:
""" A generator of IEEE E-series values.