Created
September 9, 2013 06:19
-
-
Save mbecica/6492066 to your computer and use it in GitHub Desktop.
Polar hyperbolic grid
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE HTML> | |
<head> | |
<script type="text/javascript" src="http://d3js.org/d3.v3.min.js"></script> | |
</head> | |
<body> | |
<script type="text/javascript"> | |
//Setup canvas | |
var canvas = document.createElement("canvas"); | |
document.body.appendChild(canvas); | |
canvas.width = 1000; | |
canvas.height = 600; | |
var xgrid = 10, | |
ygrid = 10, | |
k = 100; | |
var screen = canvas.getContext("2d"); | |
screen.translate(canvas.width/2, canvas.height/2); | |
d3.select("canvas").on("mousemove", draw); | |
function hyper(p) { | |
//transform to radial hyperbolic | |
var r = Math.sqrt(p[0]*p[0] + p[1]*p[1]), | |
x1 = p[0] / (r + k), | |
y1 = p[1] / (r + k); | |
return [x1, y1]; | |
} | |
function draw() { | |
screen.fillStyle = "rgba(255,255,255,1)"; | |
screen.fillRect(-canvas.width/2,-canvas.height/2,canvas.width,canvas.height); | |
screen.fillStyle = "rgb(0,0,0)"; | |
var mouse = d3.mouse(this); | |
var i = canvas.width; | |
while (i -= xgrid) { | |
var j = canvas.height; | |
while (j -= ygrid) { | |
var p = hyper([i-mouse[0],j-mouse[1]]); | |
screen.fillRect(p[0]*canvas.width*.5, p[1]*canvas.height*.5, 1, 1); | |
} | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment