Fractals
by Sébastien Lorentz
JavaScript
function fractale(liste, nbr) {
if (nbr > 0) {
temp = []
liste.forEach(function(line) {
temp = temp.concat(fractale(line.split4(), nbr - 1))
});
return temp
} else {
return liste
}
}
function drawline(line) {
console.log(line)
ctx.beginPath();
ctx.moveTo(line.debut.x, line.debut.y);
ctx.lineTo(line.fin.x, line.fin.y);
ctx.stroke();
}
// ----- Classes -----
function Point(x, y) {
this.x = x;
this.y = y;
}
function Ligne(debut, fin) {
this.debut = debut;
this.fin = fin;
var self = this;
// ----- Methods -----
self.tier = (debut, fin) => {
tierX = Math.abs(debut.x - fin.x) / 3 + Math.min(debut.x, fin.x)
tierY = Math.abs(debut.y - fin.y) / 3 + Math.min(debut.y, fin.y)
return new Point(tierX, tierY)
}
self.milieu = (debut, fin) => {
middleX = Math.abs(debut.x - fin.x) / 2 + Math.min(debut.x, fin.x)
middleY = Math.abs(debut.y - fin.y) / 2 + Math.min(debut.y, fin.y)
return new Point(middleX, middleY)
}
self.sommet = (premierPoint, secondpoint, bool) => {
dx = secondpoint.x - premierPoint.x
dy = secondpoint.y - premierPoint.y
longueur = Math.sqrt((dx * dx) + (dy * dy))
dirX = dx / longueur
dirY = dy / longueur
hauteur = (Math.sqrt(3) / 2) * longueur
cx = premierPoint.x + dx * 0.5
cy = premierPoint.y + dy * 0.5
pDirX = -dirY
pDiry = dirX
if (bool) {
rx = cx + hauteur * pDirX
ry = cy + hauteur * pDiry
} else {
rx = cx - hauteur * pDirX
ry = cy - hauteur * pDiry
}
return new Point(rx, ry)
}
self.split4 = () => {
middle = this.milieu(this.debut, this.fin);
pointPremierQuart = this.milieu(this.debut, middle);
pointSecondQuart = this.milieu(middle, this.fin);
middle = this.sommet(pointPremierQuart, pointSecondQuart, false);
premierQuart = new Ligne(this.debut, pointPremierQuart);
secondQuart = new Ligne(pointPremierQuart, middle);
troisiemeQuart = new Ligne(middle,...