Skip to content

Instantly share code, notes, and snippets.

@tmthrgd
Last active September 2, 2016 10:00
Show Gist options
  • Select an option

  • Save tmthrgd/4ca9cbbbde508c33f3a20e9f16981b10 to your computer and use it in GitHub Desktop.

Select an option

Save tmthrgd/4ca9cbbbde508c33f3a20e9f16981b10 to your computer and use it in GitHub Desktop.
<!doctype html>
<html lang=en>
<!--
(c) Tom Thorogood 2013
https://tomthorogood.co.uk
Copyright 2013 Tom Thorogood. All rights reserved.
Use of this source code is governed by a
Modified BSD License license that can be found in
the LICENSE file.
-->
<head>
<meta charset=utf-8>
<title>Regular Hyperbola</title>
</head>
<body>
<canvas id=canvas width=1024 height=1024 style=display:block></canvas>
<select id=values onchange=change()></select>
<div><p>&copy; <a href=https://tomthorogood.co.uk>Tom Thorogood</a> 2013</p></div>
<script>
var sets = [ ];
for (var i = 0; i <= 22; i++) {
sets.push([ ]);
}
for (var i = 1; i <= 10; i++) {
sets[0].push((function(a) { return function(x) { return 150* a/x; }; })(i));
sets[0].push((function(a) { return function(x) { return 150*-a/x; }; })(i));
}
for (var i = 1; i <= 10; i++) {
sets[1].push((function(a) { return function(x) { return 150* a/Math.floor(x/10)*10; }; })(i));
sets[1].push((function(a) { return function(x) { return 150*-a/Math.floor(x/10)*10; }; })(i));
}
for (var i = 1; i <= 10; i++) {
sets[2].push((function(a) { return function(x) { return Math.floor(150* a/Math.floor(x/10))*10; }; })(i));
sets[2].push((function(a) { return function(x) { return Math.floor(150*-a/Math.floor(x/10))*10; }; })(i));
}
for (var i = 1; i <= 10; i++) {
sets[3].push((function(a) { return function(x) { return 150* a/Math.floor(x/5)*5; }; })(i));
sets[3].push((function(a) { return function(x) { return 150*-a/Math.floor(x/5)*5; }; })(i));
}
for (var i = 1; i <= 10; i++) {
sets[4].push((function(a) { return function(x) { return Math.floor(150* a/Math.floor(x/5))*5; }; })(i));
sets[4].push((function(a) { return function(x) { return Math.floor(150*-a/Math.floor(x/5))*5; }; })(i));
}
for (var i = 1; i <= 1750; i++) {
sets[5].push((function(a) { return function(x) { return 150* a/x; }; })(i));
sets[5].push((function(a) { return function(x) { return 150*-a/x; }; })(i));
}
for (var i = 1; i <= 10; i++) {
sets[6].push((function(a) {
return function(x) {
if (x % 10 === 0) {
return 150* a/x;
} else {
return 150* a/Math.floor(x/10)*10;
}
};
})(i));
sets[6].push((function(a) {
return function(x) {
if (x % 10 === 0) {
return 150*-a/x;
} else {
return 150*-a/Math.floor(x/10)*10;
}
};
})(i));
}
for (var i = 1; i <= 10; i++) {
sets[7].push((function(a) {
return function(x) {
if (x % 5 === 0) {
return 150* a/x;
} else {
return 150* a/Math.floor(x/5)*5;
}
};
})(i));
sets[7].push((function(a) {
return function(x) {
if (x % 5 === 0) {
return 150*-a/x;
} else {
return 150*-a/Math.floor(x/5)*5;
}
};
})(i));
}
for (var i = 1; i <= 10; i++) {
sets[8].push((function(a) { return function(x) { return Math.floor(150* a/x/10)*10; }; })(i));
sets[8].push((function(a) { return function(x) { return Math.floor(150*-a/x/10)*10; }; })(i));
}
for (var i = 1; i <= 10; i++) {
sets[9].push((function(a) { return function(x) { return Math.floor(150* a/x/5)*5; }; })(i));
sets[9].push((function(a) { return function(x) { return Math.floor(150*-a/x/5)*5; }; })(i));
}
for (var i = 1; i <= 10; i++) {
sets[10].push((function(a) { return function(x) { return Math.floor(150* a/x/3)*3; }; })(i));
sets[10].push((function(a) { return function(x) { return Math.floor(150*-a/x/3)*3; }; })(i));
}
for (var i = 1; i <= 10; i++) {
sets[11].push((function(a) { return function(x) { return Math.floor(150* a/(x - (x % 10))); }; })(i));
sets[11].push((function(a) { return function(x) { return Math.floor(150*-a/(x - (x % 10))); }; })(i));
}
for (var i = 1; i <= 10; i++) {
sets[12].push((function(a) { return function(x) { return Math.floor(15* a/Math.floor(x/10))*10; }; })(i));
sets[12].push((function(a) { return function(x) { return Math.floor(15*-a/Math.floor(x/10))*10; }; })(i));
}
sets[13].push(function(x) { return 150*Math.sin(x); });
sets[14].push(function(x) { return 50* Math.sin(x/50); });
sets[14].push(function(x) { return 50*-Math.sin(x/50); });
sets[14].push(function(x) { return 50* Math.cos(x/50); });
sets[14].push(function(x) { return 50*-Math.cos(x/50); });
for (var i = -179; i <= 179; i++) {
sets[15].push({ step: canvas.width, fn: (function(a) { return function(x) { return Math.sin(a)*x; }; })(i) });
}
for (var i = -619; i <= 619; i++) {
sets[16].push({ step: canvas.width, fn: (function(a) { return function(x) { return Math.sin(a)*x; }; })(i) });
sets[16].push({ y: true, step: canvas.height, fn: (function(a) { return function(y) { return Math.sin(a)*y; }; })(i) });
}
for (var i = -180; i <= 180; i++) {
sets[17].push({ step: canvas.width, fn: (function(a) { return function(x) { return Math.cos(a)*x; }; })(i) });
sets[17].push({ y: true, step: canvas.height, fn: (function(a) { return function(y) { return Math.cos(a)*y; }; })(i) });
}
sets[18].push({ step: canvas.width/4, minX: -canvas.width/4, maxX: canvas.width/4, fn: function(x) { return canvas.height/4; } });
sets[18].push({ step: canvas.width/4, minX: -canvas.width/4, maxX: canvas.width/4, fn: function(x) { return -canvas.height/4; } });
sets[18].push({ y: true, step: canvas.height/4, minY: -canvas.height/4, maxY: canvas.height/4, fn: function(y) { return canvas.width/4; } });
sets[18].push({ y: true, step: canvas.height/4, minY: -canvas.height/4, maxY: canvas.height/4, fn: function(y) { return -canvas.width/4; } });
for (var i = 1; i <= 10; i++) {
sets[19].push((function(a) { return function(x) { return 50* a/Math.floor(x/5)*5; }; })(i));
sets[19].push((function(a) { return function(x) { return 50*-a/Math.floor(x/5)*5; }; })(i));
}
for (var i = 1; i <= 10; i++) {
sets[20].push((function(a) { return function(x) { return Math.floor(50* a/Math.floor(x/5))*5; }; })(i));
sets[20].push((function(a) { return function(x) { return Math.floor(50*-a/Math.floor(x/5))*5; }; })(i));
}
for (var i = 1; i <= 10; i++) {
sets[21].push((function(a) { return function(x) { return 50* a/Math.floor(x/3)*3; }; })(i));
sets[21].push((function(a) { return function(x) { return 50*-a/Math.floor(x/3)*3; }; })(i));
}
for (var i = 1; i <= 10; i++) {
sets[22].push((function(a) { return function(x) { return Math.floor(50* a/Math.floor(x/3))*3; }; })(i));
sets[22].push((function(a) { return function(x) { return Math.floor(50*-a/Math.floor(x/3))*3; }; })(i));
}
window.onload = function() {
var values = document.getElementById('values'),
canvas = document.getElementById('canvas'),
ctx = canvas.getContext('2d'),
_x = canvas.width/2,
_y = canvas.height/2,
idx;
for (var i = 0; i < sets.length; ) {
var option = document.createElement('option');
option.text = ++i;
values.appendChild(option);
}
if (window.location.hash && (idx = parseInt(window.location.hash.substr(1)))) {
values.selectedIndex = idx - 1;
}
window.change = function() {
var paths = sets[values.selectedIndex];
window.location.hash = '#' + (values.selectedIndex + 1);
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (var i = 0, opts; i < paths.length; i++) {
opts = { fn: function(){}, y: false, step: 1, minX: -_x, maxX: _x, minY: -_y, maxY: _y };
if (typeof paths[i] === 'function') {
opts.fn = paths[i];
} else {
paths[i].fn && (opts.fn = paths[i].fn);
paths[i].y && (opts.y = paths[i].y);
paths[i].step && (opts.step = Math.abs(paths[i].step));
paths[i].minX && (opts.minX = Math.max(paths[i].minX, -_x));
paths[i].maxX && (opts.maxX = Math.min(paths[i].maxX, _x));
paths[i].minY && (opts.minY = Math.max(paths[i].minY, -_y));
paths[i].maxY && (opts.maxY = Math.min(paths[i].maxY, _y));
}
ctx.beginPath();
if (opts.y) {
ctx.moveTo(_x + opts.fn(opts.minY), _y - opts.minY);
for (var y = opts.minY + opts.step; y <= opts.maxY; y += opts.step) {
var x = opts.fn(y);
if (isFinite(x)) {
ctx.lineTo(_x + x, _y - y);
} else {
ctx.stroke();
ctx.beginPath();
}
}
} else {
ctx.moveTo(_x + opts.minX, _y - opts.fn(opts.minX));
for (var x = opts.minX + opts.step; x <= opts.maxX; x += opts.step) {
var y = opts.fn(x);
if (isFinite(y)) {
ctx.lineTo(_x + x, _y - y);
} else {
ctx.stroke();
ctx.beginPath();
}
}
}
ctx.stroke();
}
};
change();
};
</script>
</body>
</html>
Copyright (c) 2013, Tom Thorogood.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Tom Thorogood nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@tmthrgd
Copy link
Author

tmthrgd commented Sep 2, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment