I am having a problem with my code. It runs well, does the line fractal, does the first side of the hexagonal flake, turns 60 degrees, but then it goes on forever. The next side doesn’t stop, it just continues till I hit the edge of the map.
Here's my code.
function degreesToRadians(degrees) {
return Math.PI / 180 * degrees;
}
this.line= function(start,end){
var full = Vector.subtract(end, start);
var distance = full.magnitude();
if (distance < 4) {
this.toggleFlowers(false);
this.moveXY(start.x, start.y);
this.toggleFlowers(true);
this.moveXY(end.x, end.y);
return;
}
var half = Vector.divide(full, 2);
var A = Vector.add(half, start);
var rotate = Vector.rotate(half, degreesToRadians(90));
rotate = Vector.multiply(rotate, 2 / 3);
var B = Vector.add(rotate, A);
this.line(start, A);
this.line(A, B);
this.line(B, A);
this.line(A, end);
};
this.flake = function(start, end) {
var a = start;
var b = end;
for (var i=0;i<6;++i){
var side = Vector.subtract(b,a);
this.line(a, b);
var rotate = Vector.rotate(side,degreesToRadians(60));
var magnitude = rotate.magnitude();
a=this.pos;
b=Vector.limit(rotate,12);
}
};
var whiteXs = [new Vector(12, 10), new Vector(60, 10)];
var redXs = [new Vector(64, 52), new Vector(52, 52)];
this.toggleFlowers(false);
this.moveXY(12,10);
this.toggleFlowers(true);
this.setFlowerColor("blue");
this.line(whiteXs[0],whiteXs[1]);
this.toggleFlowers(false);
this.moveXY(64,52);
this.toggleFlowers(true);
this.setFlowerColor("purple");
this.flake(redXs[0],redXs[1]);