Skip to content

Instantly share code, notes, and snippets.

@shayanjm
shayanjm / haversine.clj
Created April 12, 2015 20:15
Haversine Formula implementation in Clojure
; Haversine formula
; a = sin²(Δφ/2) + cos φ1 ⋅ cos φ2 ⋅ sin²(Δλ/2)
; c = 2 ⋅ atan2( √a, √(1−a) )
; d = R ⋅ c
; where φ is latitude, λ is longitude, R is earth’s radius (mean radius = 6,371km);
(defn haversine
"Implementation of Haversine formula. Takes two sets of latitude/longitude pairs and returns the shortest great circle distance between them (in km)"
[{lon1 :lng lat1 :lat} {lon2 :lng lat2 :lat}]
(let [R 6378.137 ; Radius of Earth in km
@shayanjm
shayanjm / reverse-haversine.clj
Created April 12, 2015 22:01
"Reverse Haversine" Formula implementation in Clojure
; "Reverse haversine" to derive lat/long from start point, bearing (degrees clockwise from north), & distance (km)
; φ2 = asin( (sin φ1 ⋅ cos δ) + (cos φ1 ⋅ sin δ ⋅ cos θ) )
; λ2 = λ1 + atan2( sin θ ⋅ sin δ ⋅ cos φ1, cos δ − sin φ1 ⋅ sin φ2 )
; where φ is latitude, λ is longitude, θ is the bearing (clockwise from north), δ is the angular distance d/R; d being the distance travelled, R the earth’s radius
(defn reverse-haversine
"Implementation of the reverse of Haversine formula. Takes one set of latitude/longitude as a start point, a bearing, and a distance, and returns the resultant lat/long pair."
[{lon :lng lat :lat bearing :bearing distance :distance}]
(let [R 6378.137 ; Radius of Earth in km
lat1 (Math/toRadians lat)
@shayanjm
shayanjm / test.clj
Created February 2, 2016 22:52
Kafka + Transit = Niceness
(ns project.core
(:require [cognitect.transit :as transit])
(:use kafka-clj.client :reload)
(:import [java.io ByteArrayInputStream ByteArrayOutputStream]))
;; Set up connector here
(let [data (ByteArrayOutputStream. 4096)
writer (transit/writer data :json)]
(transit/write writer link-difference)