Skip to content

Instantly share code, notes, and snippets.

@dayjaby
dayjaby / example.cpp
Created December 18, 2012 09:00
A modification to http://christopherschwaab.wordpress.com/2012/09/01/generating-switch-statements-in-c/ Added the possibility to define a default option for the switch statement.
#include <stdio.h>
#include <switch.hpp>
// Default option shall be one higher than the last
enum { ADD = 0, SUB = 1, MUL = 2, DIV = 3, MOD = 4, DEFAULT = 5 };
template <int Operation>
struct interpreter {
static inline int run(const int x, const int y);
};
let $content := file:read-text-lines("webapp/examples.utf")
let $n := count($content)
return
<sentences>{
for $i in (1 to $n)
where $i mod 2 = 1
let $tokens := fn:tokenize(substring($content[$i],4),"#|\t")
return
<record>
<jpn>{$tokens[1]}</jpn>
file:write(
"data.xml",
csv:parse(fetch:text("data.csv"),
map{"separator":"tab"}))
let $content := file:read-text-lines("webapp/examples.utf")
let $n := count($content)
for $i in (1 to $n)
where $i mod 2 = 1
return <record><jpn>{substring(substring-before($content[$i]," "),4)}</jpn><eng>{substring-before(substring-after($content[$i]," "),"#")}</eng></record>
let $db := db:open("sentences")//sentences/record
let $n := random:integer(count($db))+1
return $db[$n]
@dayjaby
dayjaby / distance.py
Last active November 2, 2018 09:12
Distance between two geographic coordinates
def calculate_distance(pt1, pt2):
lat1, lon1 = pt1
lat2, lon2 = pt2
r = 6371000.0
dlat = math.radians(lat2 - lat1)
dlon = math.radians(lon2 - lon1)
a = math.sin(dlat/2)**2
a +=math.cos(math.radians(lat1))*math.cos(math.radians(lat2))*math.sin(dlon/2)**2
c = 2*math.atan2(math.sqrt(a), math.sqrt(1-a))
@dayjaby
dayjaby / heading.py
Created November 2, 2018 09:13
Heading between two geographic coordinates
def calculate_heading(pt1, pt2):
lat1, lon1 = pt1
lat2, lon2 = pt2
y = math.sin(math.radians(lon2-lon1)) * math.cos(math.radians(lat2))
x = math.cos(math.radians(lat1))*math.sin(math.radians(lat2))
x-= math.sin(math.radians(lat1))*math.cos(math.radians(lat2))*math.cos(math.radians(lon2-lon1))
return math.degrees(math.atan2(y, x))
@dayjaby
dayjaby / move-along-direction.py
Created November 2, 2018 09:15
Move some distance along direction from a geographic coordinate
def distance_in_direction(origin, distance, direction):
lat1, lon1 = origin
lat1 = math.radians(lat1)
lon1 = math.radians(lon1)
r = 6371000.0
lat2 = math.asin(math.sin(lat1)*math.cos(distance/r)+math.cos(lat1)*math.sin(distance/r)*math.cos(math.radians(direction)))
lon2 = lon1 + math.atan2(math.sin(direction)*math.sin(distance/r)*math.cos(lat1), math.cos(distance/r)-math.sin(lat1)*math.sin(lat2))
return math.degrees(lat2), math.degrees(lon2)
@dayjaby
dayjaby / kalman.py
Created December 3, 2018 12:34
Python+OpenCV Kalman Filter example for positioning
r'''
==================================
Kalman Filter tracking a sine wave
==================================
This example shows how to use the Kalman Filter for state estimation.
In this example, we generate a fake target trajectory using a sine wave.
Instead of observing those positions exactly, we observe the position plus some
random noise. We then use a Kalman Filter to estimate the velocity of the
@dayjaby
dayjaby / myrosdep.yaml
Last active April 1, 2019 14:09
My ros dependencies
libqt4:
debian: [libqtcore4]
python-neo4j:
debian:
pip:
packages: [neo4j]
ubuntu:
pip:
packages: [neo4j]