class KochLine{
constructor(context, start, end){
this.context = context;
this.start = start;
this.end = end;
}
show(){
this.context.beginPath();
this.context.moveTo(this.start[0], this.start[1]);
this.context.lineTo(this.end[0], this.end[1]);
this.context.stroke();
}
kochA(){
return this.start;
}
kochB(){
let v = [this.end[0] - this.start[0], this.end[1] - this.start[1]];
v = v.map(d => d/3);
return this.start.map((d,i) => d + v[i]);
}
kochC(){
let v = [this.end[0] - this.start[0], this.end[1] - this.start[1]];
v = v.map(d => d/3);
const rotated = this.rotate(v, Math.PI/3);
return this.start.map((d,i) => d + v[i] + rotated[i]);
}
kochD(){
let v = [this.end[0] - this.start[0], this.end[1] - this.start[1]];
v = v.map(d => d/3);
return this.start.map((d,i) => d + 2 * v[i]);
}
kochE(){
return this.end;
}
rotate(vector, angle){
const cos = Math.cos(angle),
sin = Math.sin(angle);
return [vector[0]*cos - vector[1]*sin, vector[0]*sin + vector[1]*cos];
}
}