Skip to content

Instantly share code, notes, and snippets.

@TimSC
TimSC / RunningAverage.h
Last active November 27, 2019 01:43
Calculate running average in C++
#ifndef _RUNNING_AVERAGE_H
#define _RUNNING_AVERAGE_H
#include <vector>
#include <iostream>
#include <cmath>
using namespace std;
class RunningAverage
{
@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 / import.txt
Created October 15, 2015 23:31
Useful osm/osmosis import commands
sudo -u postgres createuser gisuser
sudo -u postgres dropdb egypt-osm
sudo -u postgres createdb --encoding=UTF8 --owner=gisuser egypt-osm
sudo pluma /etc/postgresql/9.3/main/pg_hba.conf
change to use md5 authentication for local connections
sudo service postgresql restart
@TimSC
TimSC / insertionlist.py
Last active October 17, 2015 14:12
Insertion sorted python list
#By Tim Sheerman-Chase
#Released under CC0 license
class InsertionSortedList():
#Yay! I have re-invented insertion sort!
def __init__(self):
self._vals = []
@TimSC
TimSC / TriTri2D.py
Last active March 27, 2016 17:44
Collision detection of triangles in 2D implemented in python 2/3
#2D Triangle-Triangle collisions in python
#Release by Tim Sheerman-Chase 2016 under CC0
import numpy as np
def CheckTriWinding(tri, allowReversed):
trisq = np.ones((3,3))
trisq[:,0:2] = np.array(tri)
detTri = np.linalg.det(trisq)
if detTri < 0.0:
@TimSC
TimSC / TriTri2D.cpp
Created March 28, 2016 10:41
Collision detection of triangles in 2D implemented in C++
//2D Triangle-Triangle collisions in C++
//Release by Tim Sheerman-Chase 2016 under CC0
#include <vector>
#include <iostream>
#include <stdexcept>
using namespace std;
typedef std::pair<double, double> TriPoint;
@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 / 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 / pngtojpg.py
Created July 13, 2016 11:11
Python 2, convert pngs in a folder to jpgs
from PIL import Image
import os
for fina in os.listdir("."):
finas = os.path.splitext(fina)
if finas[1] != ".png": continue
img = Image.open(fina)
img.save(finas[0]+".jpg")
@TimSC
TimSC / perlin.py
Last active July 20, 2016 13:58
Generate perlin noise in python 2 and 3
import random
import math
#https://gamedev.stackexchange.com/questions/23625/how-do-you-generate-tileable-perlin-noise
dirs = [(math.cos(a * 2.0 * math.pi / 256),
math.sin(a * 2.0 * math.pi / 256))
for a in range(256)]
def surflet(gridX, gridY, x, y, hashfunc):
distX, distY = abs(x-gridX), abs(y-gridY)