Skip to content

Instantly share code, notes, and snippets.

Avatar
💭
🌒

Stephan Hügel urschrei

💭
🌒
View GitHub Profile
@urschrei
urschrei / bresenham.rs
Last active Oct 10, 2021
Return evenly-spaced samples from an array using Bresenham's line algorithm
View bresenham.rs
/// Return evenly-spaced samples from an array using [Bresenham's line algorithm](https://www.cs.helsinki.fi/group/goa/mallinnus/lines/bresenh.html)
/// Adapted from https://observablehq.com/@mbostock/evenly-spaced-sampling
///
/// # Explanation
/// This works because the problem is equivalent to drawing a line on a `num_samples` x `arr` grid of
/// discrete pixels:
/// `x` coordinates are in the range `0…arr - 1` and `y` coordinates are in the range `0…num_samples - 1`
/// We then proceed as if we were drawing a line from the origin to `arr - 1, num_samples - 1`:
/// Whenever the `y` coordinate changes we choose a new element from `x`.
///
@urschrei
urschrei / horror.py
Created Sep 8, 2019 — forked from sbma44/horror.py
Lovecraft word pairs by rarity of vocab
View horror.py
# sample invocation:
# pdftotext -f 5 /tmp/The_Complete_Works_of_H.P._Lovecraft.pdf - | python3 horror.py | uniq > lovecraft_word_pairs_sorted.txt
import re
import sys
import nltk
import wordfreq
re_word = re.compile(r'[^\-\w]')
View dump.py
#!/usr/bin/env python
# needs Pandas, Geopandas, Shapely. Should work on 2.7.x and 3.6.x
# this will only dump properties that are open or partially open today
# this can be adjusted by commenting out or altering the values given to isin()
from datetime import date
import geopandas as gp
import pandas as pd
from shapely.geometry import Point, LineString, Polygon, MultiPolygon, MultiPoint, box
@urschrei
urschrei / lib.rs
Last active Sep 25, 2018
Detect whether an (extended) ASCII string is a palindrome anagram using Rust
View lib.rs
// Given a string, how do you determine whether it's an anagram of a palindrome?
// Solution: Given the unique set of chars, at most 1 should appear in the string
// an odd number of times.
// chars() returns Unicode Scalar Values, and these might not match up
// with grapheme clusters, so this can't be assumed to work correctly
// for anything other than (extended) ASCII
fn unique_chars(s: &str) -> String {
let mut v: Vec<char> = s.chars().collect();
// dedup removes consecutive identical elements, so we need a sort first
View LSP_prefs.json
// Settings in here override those in "LSP/LSP.sublime-settings",
{
"clients":
{
"rls":
{
"auto_complete_triggers": [ {"selector": "source.rust", "characters": ".:"} ],
"diagnostics_highlight_style": "box",
"command": ["rustup", "run", "nightly", "rls"],
@urschrei
urschrei / minimum_polygon_distance.md
Last active Nov 29, 2021
An algorithm for determining the minimum distance between two non-convex polygons
View minimum_polygon_distance.md

Adapted from https://www.quora.com/How-do-you-compute-the-distance-between-two-non-convex-polygons-in-linear-time/answer/Tom-Dreyfus

Calculating the Minimum Distance Between Two Non-Convex Polygons

See Amato, Nancy M (1994) for details of the difference between separation (sigma: σ) and closest visible vertex (CVV).

Refer to P and Q as the two polygons with n and m vertices, respectively.
For the purposes of this discussion, a key insight is that it is enough to find the closest edge to each vertex in order to compute the minimum separation between P and Q.
This means iterating over all vertices, and finding a nearest neighbour. Thus, a time complexity in O((m + n) * log(m * n)) should be expected.

@urschrei
urschrei / png_to_polygon_to_geojson.py
Last active Nov 16, 2022
Read a PNG into a numpy array, convert it to a Shapely Polygon, and dump it as GeoJSON
View png_to_polygon_to_geojson.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created by Stephan Hügel on 2017-03-02
The MIT License (MIT)
Copyright (c) 2017 Stephan Hügel
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
View dumpbin_cdylib.txt
Microsoft (R) COFF/PE Dumper Version 14.00.24215.1
Copyright (C) Microsoft Corporation. All rights reserved.
Dump of file target/release/lonlat_bng.dll
File Type: DLL
Section contains the following exports for lonlat_bng-f8cb032e62ae16ed.dll
@urschrei
urschrei / hexagram.py
Last active Oct 4, 2019
Generate and dump a hexagram in PNG format
View hexagram.py
# -*- coding: utf-8 -*-
# Requires PIL (pillow) and NumPy
# Copyright (C) Stephan Hügel, 2016
# License: MIT
import sys
from PIL import Image
import numpy as np
@urschrei
urschrei / playground.rs
Last active Feb 2, 2016 — forked from anonymous/playground.rs
Threaded send/receive using channels in Rust
View playground.rs
use std::thread;
use std::sync::mpsc::{ channel, Sender };
enum Wrapped {
A(f64),
B(f64),
}
fn produce_a(sender: Sender<Wrapped>) {
for i in 0 .. 5 {