Skip to content

Instantly share code, notes, and snippets.

@picasso250
Last active January 3, 2016 11:09
Show Gist options
  • Save picasso250/8454294 to your computer and use it in GitHub Desktop.
Save picasso250/8454294 to your computer and use it in GitHub Desktop.
var c=document.getElementById("XcCanvas");
var ctx=c.getContext("2d");
ctx.drawPoint = function (point) {
var x, y, size;
if (arguments.length === 2) {
x = arguments[0];
y = arguments[1];
} else {
x = point.x;
y = point.y;
if (point.color !== undefined) {
this.fillStyle = point.color;
};
if (point.size === undefined) {
x = Math.floor(x);
y = Math.floor(y);
size = 1;
} else {
x = Math.floor(x - point.size/2);
y = Math.floor(y - point.size/2);
size = point.size;
}
}
this.fillRect(x, y, size, size);
};
ctx.translate(10, 10);
ctx.fillStyle="#FF0000";
var Point = function (x, y) {
this.x = x;
this.y = y;
};
Point.prototype.setColor = function (color) {
this.color = color;
return this;
};
Point.prototype.setSize = function (size) {
this.size = size;
return this;
};
var getTriangleTreePoint = function(a) {
return [
new Point(a/2, 0).setColor('#FF0000').setSize(5),
new Point(0, a*Math.sqrt(3)/2).setColor('#00FF00').setSize(5),
new Point(a, a*Math.sqrt(3)/2).setColor('#0000FF').setSize(5)
];
};
var drawTreePoint = function (a) {
var PointList = getTriangleTreePoint(a);
for (var i = 0; i < PointList.length; i++) {
ctx.drawPoint(PointList[i]);
};
};
var getRandomPoint = function(a) {
var sqrt3 = Math.sqrt(3);
var getRandomPointIter = function () {
return {x: Math.random()*a, y: Math.random()*a};
};
var point;
point = getRandomPointIter(a);
return point;
}
var a = 400;
drawTreePoint(a);
var StartPoint = getRandomPoint(a);
StartPoint.color = "#000000";
StartPoint.size = 8;
ctx.drawPoint(StartPoint);
var getMidPoint = function (p1, p2) {
return {x: (p1.x + p2.x) / 2, y: (p1.y + p2.y) / 2, color: p2.color};
};
var chooseOnePointFromTree = function (a) {
var index = Math.floor(Math.random()*3);
var point_list = getTriangleTreePoint(a);
return point_list[index];
};
var p;
var MidPoint;
var i = 0;
var iter = function () {
p = chooseOnePointFromTree(a);
MidPoint = getMidPoint(StartPoint, p);
ctx.drawPoint(MidPoint);
StartPoint = MidPoint;
if (i < 40000) {
setTimeout(iter, 1);
};
i++;
};
iter();
console.log('end');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment