Skip to content

Instantly share code, notes, and snippets.

View panzi's full-sized avatar

Mathias Panzenböck panzi

View GitHub Profile
@panzi
panzi / new_google_maps_zoom.js
Last active February 15, 2017 13:59
The new Google Maps seems to use a diameter instead of a zoom value. You can use this function to get the old zoom value from the diameter. I tested it with the given list of values.
function diameterToZoom (diameter) {
var zoom = Math.floor(19 - Math.log(diameter / 1000) / Math.LN2);
return zoom < 0 ? 0 : zoom > 20 ? 20 : zoom;
}
// tested with these values:
// diameter = zoom
// 758 = 19
// 1515 = 18
// 3031 = 17
@panzi
panzi / conststruct.rb
Last active December 24, 2015 16:39
Very similar class to OpenStruct, except all instances are immutable. Use ConstStruct.derive("MyClass") to dynamically create a subclass (so instance.inspect gives a nicer/more specific class name).
class ConstStruct
def self.derive(name)
cls = Class.new(ConstStruct)
cls.instance_variable_set :@name, name
def cls.name; @name end
def cls.inspect; @name end
def cls.to_s; @name end
cls
end
@panzi
panzi / portable_endian.h
Last active April 18, 2024 20:59
This provides the endian conversion functions form endian.h on Windows, Linux, *BSD, Mac OS X, and QNX. You still need to use -std=gnu99 instead of -std=c99 for gcc. The functions might actually be macros. Functions: htobe16, htole16, be16toh, le16toh, htobe32, htole32, be32toh, le32toh, htobe64, htole64, be64toh, le64toh. License: I hereby put …
// "License": Public Domain
// I, Mathias Panzenböck, place this file hereby into the public domain. Use it at your own risk for whatever you like.
// In case there are jurisdictions that don't support putting things in the public domain you can also consider it to
// be "dual licensed" under the BSD, MIT and Apache licenses, if you want to. This code is trivial anyway. Consider it
// an example on how to get the endian conversion functions on different platforms.
#ifndef PORTABLE_ENDIAN_H__
#define PORTABLE_ENDIAN_H__
#if (defined(_WIN16) || defined(_WIN32) || defined(_WIN64)) && !defined(__WINDOWS__)
@panzi
panzi / cexpr.sh
Last active December 25, 2015 16:59
Run some C code. -p to simply pass a printf format string and args, -iFILE to include FILE, -I -L -l -E -S -D and -O will be passed as such to gcc.Examples: CFLAGS=-std=c99 cexpr.sh 'puts("hello world");'; cexpr.sh -imath.h -lm -p '%g' 'log2(8)'
#!/bin/bash
do_printf=off
incs=""
flags="$CFLAGS"
only_preproc=off
no_asm=off
if [ "$CC" = "" ]; then
CC=gcc
@panzi
panzi / cppexpr.sh
Last active December 25, 2015 16:59
Print some C++ expresions. -iFILE to include FILE, -I -L -l -E -D -S and -O will be passed as such to g++. More compiler flags can be passed via -f$FLAG or $CXXFLAGS environment variable. Example: cppexpr.sh -f-std=c++0x -imath.h -lm 'log2(8)'
#!/bin/bash
incs=""
flags="$CXXFLAGS"
only_preproc=off
no_asm=off
no_space=off
if [ "$CXX" = "" ]; then
CXX=g++
@panzi
panzi / mandelbrot.c
Created November 10, 2013 04:40
gcc -lm -Wall -Werror -Wextra -pedantic -std=c99 -O3 -o mandelbrot mandelbrot.c original: http://preshing.com/20110926/high-resolution-mandelbrot-in-obfuscated-python/
#include <stdio.h>
#include <complex.h>
#define dc double complex
dc
Y(dc
V,
dc B,dc c){
return
(cabs (V)<6)?(c?Y(V *V+
B,B,c-1):c):(2+c-4*cpow
@panzi
panzi / Makefile
Created November 10, 2013 17:22
Usage: visprim <width> <height> <output-filename> [number-of-threads]
CPP = g++
LIB = `Magick++-config --cppflags --cxxflags --ldflags --libs` -lboost_thread-mt
CPPFLAGS = -Wall -O3 `Magick++-config --cppflags --cxxflags`
LDFLAGS = $(LIB) -Wall -O3
BIN = visprim
.PHONY: all zip clean
all: $(BIN)
@panzi
panzi / visprim.py
Created November 10, 2013 17:24
Usage: python visprim.py <width> <height> <output-filename>
#!/usr/bin/env python
import sys
import Image
from itertools import islice
def primes():
yield False # 0
yield False # 1
yield True # 2
@panzi
panzi / gist:7551381
Created November 19, 2013 19:49
Download currently playing HTML5 audio/video bookmarklet.
Create a new bookmark and set it's URL to this:
javascript:(function(xs)%7Bfor(var%20i%3D0%3Bi%3Cxs.length%3B%2B%2Bi)%7Bif(xs%5Bi%5D.currentSrc)%7Breturn%20window.open(xs%5Bi%5D.currentSrc)%3B%7D%7D%7D)(document.querySelectorAll('audio%2Cvideo'))%3B
When media is playing using HTML5 audio/video you can click this bookmark to open a new tab/window with the media. Then to download it use the context menu action "Save As..." (right mouse button -> Save As...).
If you use Chrome/Safari/recent Opera (WebKit/Blink) you can do better(!) and immediately download the file using this bookmarklet instead:
javascript:(function(xs)%7Bfor(var%20i%3D0%3Bi%3Cxs.length%3B%2B%2Bi)%7Bif(xs%5Bi%5D.currentSrc)%7Bvar%20a%3Ddocument.createElement('a')%3Ba.target%3D'_blank'%3Ba.download%3D''%3Ba.href%3Dxs%5Bi%5D.currentSrc%3Ba.click()%3Breturn%3B%7D%7D%7D)(document.querySelectorAll('audio%2Cvideo'))%3B
@panzi
panzi / mkplaylist.py
Last active December 29, 2015 20:19
Find music files in a filesystem hierarchy and put them all numerically sorted into a playlist.
#!/usr/bin/env python
import os
import re
import sys
import argparse
from os.path import abspath, join as pathjoin, splitext
from stat import S_ISREG, S_ISDIR