Skip to content

Instantly share code, notes, and snippets.

import csv
def MergeUpdate(dataDict, update):
for li in update:
#print (li['ElectorNumber'], li['ElectorCreatedMonth'], li['ElectorChangedMonth'], li['ElectorDeletedMonth'])
electNum = li['ElectorNumber'].strip()
if int(li['ElectorCreatedMonth']) or int(li['ElectorChangedMonth']):
@TimSC
TimSC / LineLineIntersect.cpp
Last active March 18, 2024 02:47
2D Line-line intersection using determinants in C++
//2D Line-line intersection using determinants
//by Tim Sheerman-Chase, 2016
//Released under CC0
#include <iostream>
#include <cmath>
#include <assert.h>
using namespace std;
/** Calculate determinant of matrix:
@TimSC
TimSC / stats.py
Last active February 13, 2024 03:00
Python statistics and matrices without numpy
#By Tim Sheerman-Chase 2016
#Released under the CC0 license
from __future__ import print_function
import itertools, copy
class RunningAverage(object):
def __init__(self):
self.Val = 0.0
self.Count = 0
@TimSC
TimSC / image-to-v4l2loopback.py
Created September 12, 2013 02:00
Send image data to v4l2loopback using python. Remember to do "sudo modprobe v4l2loopback" first! Released under CC0 by Tim Sheerman-Chase, 2013
#Send image data to v4l2loopback using python
#Remember to do sudo modprobe v4l2loopback first!
#Released under CC0 by Tim Sheerman-Chase, 2013
import fcntl, sys, os
from v4l2 import *
import time
import scipy.misc as misc
import numpy as np
@TimSC
TimSC / RamerDouglasPeucker.cpp
Created April 5, 2016 11:22
2D implementation of the Ramer-Douglas-Peucker algorithm in C++
//2D implementation of the Ramer-Douglas-Peucker algorithm
//By Tim Sheerman-Chase, 2016
//Released under CC0
//https://en.wikipedia.org/wiki/Ramer%E2%80%93Douglas%E2%80%93Peucker_algorithm
#include <iostream>
#include <cmath>
#include <utility>
#include <vector>
#include <stdexcept>
@TimSC
TimSC / contact_form.php
Created September 28, 2015 21:59
Generic php contact form with UTF-8 encoding of email
<!-- You may use this under the terms of https://creativecommons.org/publicdomain/zero/1.0/ -->
<?php
if (isset($_POST["action"]))
{
$ok = true;
$errorMsg = Null;
$from_email = $_POST["email"];
if (!filter_var($from_email, FILTER_VALIDATE_EMAIL)) {
$ok = false;
$errorMsg = "Invalid email format";
@TimSC
TimSC / line-plane-collision.py
Last active April 7, 2023 11:00
Line-Plane collision in 3D, Python 2 or 3
#Line-plane intersection
#By Tim Sheerman-Chase
#This software may be reused under the CC0 license
#Based on http://geomalgorithms.com/a05-_intersect-1.html
from __future__ import print_function
import numpy as np
def LinePlaneCollision(planeNormal, planePoint, rayDirection, rayPoint, epsilon=1e-6):
@TimSC
TimSC / pyellipse.py
Last active March 31, 2023 17:45
Calculate circumference of an ellipse in python (both exact and approximate approaches)
#pyellipse by Tim Sheerman-Chase (C) 2021
#Calculate circumference of an ellipse in python (both exact and approximate approaches)
#This source code may be used under the CC0 license https://creativecommons.org/publicdomain/zero/1.0/
#
#For the best exact circumference, use EllipseCircumAdlaj2012
#
#For good approximations that are faster to compute, see EllipseCircumRamanujan2ndApprox and EllipseCircumJacobsenWaadeland1985
#
#Incidentally, scipy has a function to exactly calculate it:
# C = 4.0*a*special.ellipe(e*e)
@TimSC
TimSC / clusteraddress.py
Last active February 22, 2023 20:59
A script to group delivery areas into routes of roughly similar size.
# A script to group delivery areas into routes of roughly similar size.
import csv
from sklearn import cluster
from pyproj import Proj, transform
import numpy as np
from matplotlib import pyplot as plt
class EqualWeightClustering(object):
@TimSC
TimSC / eertree.py
Last active February 9, 2023 01:12
Implementation of eertree, a palindromic tree in python.
#!/bin/python
#Based on: EERTREE: An Efficient Data Structure for Processing Palindromes in Strings
#by Mikhail Rubinchik and Arseny M. Shur https://arxiv.org/pdf/1506.04862.pdf
#Useful: https://medium.com/@alessiopiergiacomi/eertree-or-palindromic-tree-82453e75025b#.ofgt2r7xn
#Port of https://github.com/zimpha/algorithmic-library/blob/master/string-utility/eertree.cc
#to python by TimSC. zimpha kindly allowed me to release this code under the CC0 license.
from __future__ import print_function