Skip to content

Instantly share code, notes, and snippets.

getHexGrid <- function(spdf, cellsize){
boundingBox <- bbox(spdf)
boundingBox[1,1] <- boundingBox[1,1] - cellsize
boundingBox[2,1] <- boundingBox[2,1] - cellsize
boundingBox[2,2] <- boundingBox[2,2] + cellsize
cols = ceiling(((boundingBox[1,2] - boundingBox[1,1]) + 3 * cellsize) / cellsize)
rows = ceiling((boundingBox[2,2] - boundingBox[2,1]) / cellsize)
@mthh
mthh / README.md
Last active November 24, 2016 16:09
d3-custom-build

This is a demonstration of building a custom D3 v4 bundle using ES2015 modules and Rollup, following the example from https://gist.github.com/mbostock/bb09af4c39c79cffcde4.
This example exposes many field from the "regular" d3 object (but not all) and take into account some plugins (d3-selection-multi and d3-geo-projection).

To build:

npm install
npm run prepublish
@mthh
mthh / main.rs
Last active January 4, 2017 22:24
Maximal rectangle problem resolution in rust
struct Matrix(Vec<Vec<i32>>);
struct Pt {
x: i32,
y: i32
}
#[derive(Debug)]
struct Rect {
x: i32,
@mthh
mthh / pip_raycasting.rs
Last active January 12, 2017 21:02
Example using ray casting algorithm for point in polygon queries
// "Ray casting" ("crossing number" / "even-odd") algorithm for point in polygon queries
// Translation of https://rosettacode.org/wiki/Ray-casting_algorithm#Python
// Posted back on https://rosettacode.org/wiki/Ray-casting_algorithm#Rust
use std::f64;
use std::clone::Clone;
const _EPS: f64 = 0.00001;
const _MIN: f64 = f64::MIN_POSITIVE;
const _MAX: f64 = f64::MAX;
const BARS: &'static str = "▁▂▃▄▅▆▇█";
fn print_sparkline(s: &str){
let v = BARS.chars().collect::<Vec<char>>();
let line: String = s.replace(",", " ").split(" ")
.filter(|x| !x.is_empty())
.map(|x| v[x.parse::<f64>().unwrap().ceil() as usize - 1])
.collect();
println!("{:?}", line);
}
@mthh
mthh / cholesky.rs
Last active September 5, 2022 21:46
Rust Cholesky decomposition
// Cholesky decomposition
// Translation of http://rosettacode.org/wiki/Cholesky_decomposition#C
// Posted on http://rosettacode.org/wiki/Cholesky_decomposition#Rust
//
// 'mat' is the positive-definite matrix 'A'
// 'res' is the product of a lower triangular matrix and its conjugate transpose (LL*)
// cf. https://en.wikipedia.org/wiki/Cholesky_decomposition
fn cholesky(mat: Vec<f64>, n: usize) -> Vec<f64> {
let mut res = vec![0.0; mat.len()];
for i in 0..n {
@mthh
mthh / jenks.rs
Created January 17, 2017 23:18
Jenks "natural breaks" implementation in rust, for the fun
extern crate rustc_serialize;
extern crate time;
use time::PreciseTime;
use rustc_serialize::json;
use std::fs::File;
use std::io::Read;
use std::f64;
#[derive(RustcDecodable, RustcEncodable)]
@mthh
mthh / .block
Last active March 29, 2017 21:35
Gnome sort
license: gpl-3.0
@mthh
mthh / .block
Last active March 20, 2017 11:01
Reprojection
license: gpl-3.0
@mthh
mthh / .block
Last active March 24, 2017 15:35
Graham scan convex hull animation
license: gpl-3.0