PLAYGROUND (experimental)

triangle

triangle(x1, y1, x2, y2, x3, y3, [stroke=#000000], [fill=#FFFFFF], [weight=1.00])

Draw a triangle

Syntaxe

triangle(x1=10, y1=10, x2=5, y2=5, x3=5, y3=5, stroke="#000000", fill="#FFA000", weight=4);

Arguments

NameRequiredDefaultDescription
x1yes-x-coordinate of the first point
y1yes-y-coordinate of the first point
x2yes-x-coordinate of the second point
y2yes-y-coordinate of the second point
x3yes-x-coordinate of the third point
y3yes-y-coordinate of the third point
stroke-#000000Stroke color
fill-#FFFFFFFill color
weight-1.00Stroke weight (thickness)

Full example

canvas(800, 800, "#020410", center=true);

def Sierpinski(sz, ratio, depth, palette, pointUp, startX, startY) {
    var h = sz * ratio * sqrt(3) / 2;
    var dir;
    if (pointUp) {
        dir = 1;
    } else {
        dir = -1;
    }
    
    if (depth == 0) {
        var th = sz * ratio * sqrt(3) / 2;
        fill(randlist(palette));
        if (pointUp) {
            triangle(startX, startY + th * 2/3, startX - sz/2, startY - th/3, startX + sz/2, startY - th/3);
        } else {
            triangle(startX, startY - th * 2/3, startX - sz/2, startY + th/3, startX + sz/2, startY + th/3);
        }
        return ;
    }
    
    var th = sz * ratio * sqrt(3) / 2;
    
    Sierpinski(sz/2, ratio, depth-1, palette, pointUp, startX, startY - th/2 * dir);
    Sierpinski(sz/2, ratio, depth-1, palette, pointUp, startX - sz/4, startY + th/4 * dir);
    Sierpinski(sz/2, ratio, depth-1, palette, pointUp, startX + sz/4, startY + th/4 * dir);
}

randseed(PHI);

var palette1 = palette_blend("#FFF8E1","#FF6F00",10);
var palette2 = palette_blend("#E3F2FD","#0D47A1",10);
var palette3 = palette_blend("#FFEBEE","#B71C1C",10);

coord_translate(0,120);

stroke("transparent");

var ratio = 0.8;
Sierpinski(700, ratio, 6, palette1, true, 0,0);
Sierpinski(250, ratio, 5, palette3, false, 0,0);

var offsetX = 230 ;
var offsetY = -300 ;
Sierpinski(290, ratio, 5, palette2, false, -offsetX, offsetY);
Sierpinski(290, ratio, 5, palette2, false,  offsetX, offsetY);

save("triangle.png");