Skip to content

Instantly share code, notes, and snippets.

@mikaelbr mikaelbr/draw.re
Created Jun 13, 2017

Embed
What would you like to do?
external pi : float = "Math.PI" [@@bs.val];
let drawLine len ctxt => {
Canvas.lineTo ctxt len 0.0;
Canvas.translate ctxt len 0.0;
};
let start ctxt (w, h) => {
Canvas.moveTo ctxt 50.0 h;
Canvas.translate ctxt 50.0 h;
};
let deg_to_rad deg => (float_of_int deg) *. pi /. 180.0;
let rotate ctxt deg => {
let degs = (deg_to_rad deg);
Canvas.rotate ctxt degs;
};
let string_of_char = String.make 1;
let move = drawLine 20.0;
let push = Canvas.save;
let pop = Canvas.restore;
let rec lsystem ctxt size deg sys => {
let next = lsystem ctxt size deg;
let rot = rotate ctxt;
let neg_deg = (-deg);
start ctxt size;
switch sys {
| ['[', ...tl] => {
push ctxt;
next tl;
}
| [']', ...tl] => {
pop ctxt;
next tl;
}
| ['+', ...tl] => {
rot deg;
next tl;
}
| ['-', ...tl] => {
rot neg_deg;
next tl;
}
| [_, ...tl] => {
move ctxt;
next tl;
}
| [] => Canvas.stroke ctxt;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.