const scale = 18;//min=5,max=200,step=1
const density = 0.01;//min=0.005,max=1,step=0.005
const loops = 20;//min=5,max=1000,step=1
const a = 2;//min=0,max=50,step=1
const k = 4;//min=1,max=20,step=1
const p = 5;//min=1,max=20,step=1
const shift = 24;//min=5,max=100,step=1
const skew = 1;//min=0.1,max=10,step=0.1
const rotateButterfly = 0;//min=0,max=360,step=1

const flowerScale = 60;//min=10,max=150,step=1
const flowerDensity = 0.01;//min=0.005,max=0.05,step=0.005
const petals = 6;//min=3,max=20,step=1
const inner = 0.5;//min=0.1,max=2,step=0.1
const twist = 2;//min=0,max=10,step=0.1
const wave = 0.3;//min=0,max=2,step=0.1

const butterflyX = 0;//min=-120,max=120,step=1
const butterflyY = 0;//min=-120,max=120,step=1
const flowerX = 0;//min=-120,max=120,step=1
const flowerY = 0;//min=-120,max=120,step=1

Canvas.setpenopacity(1);

function rotate(x, y, angle) {
    let rad = angle * Math.PI / 180;
    return {
        x: x * Math.cos(rad) - y * Math.sin(rad),
        y: x * Math.sin(rad) + y * Math.cos(rad)
    };
}

function drawButterfly(cx, cy) {
    const t = new Turtle();
    t.penup();

    let first = true;

    for (let i = 0; i < loops * Math.PI / density; i++) {

        let tt = i * density;

        let r =
            Math.exp(Math.sin(tt)) -
            a * Math.cos(k * tt) +
            Math.pow(Math.sin((2 * tt - Math.PI) / shift), p);

        let x = Math.cos(tt) * r * scale;
        let y = -Math.sin(tt) * r * scale * skew;

        let rotated = rotate(x, y, rotateButterfly);

        let finalX = cx + rotated.x;
        let finalY = cy + rotated.y;

        if (first) {
            t.goto(finalX, finalY);
            t.pendown();
            first = false;
        } else {
            t.goto(finalX, finalY);
        }
    }
}

function drawFlower(cx, cy) {
    const t = new Turtle();
    t.penup();

    let first = true;

    for (let tt = 0; tt < Math.PI * 2; tt += flowerDensity) {

        // ⭐ 多层结构(比普通花复杂很多)
        let base = Math.sin(petals * tt);
        let mod = Math.cos(tt * twist) * wave;
        let r = base * (inner + mod);

        let x = r * Math.cos(tt) * flowerScale;
        let y = r * Math.sin(tt) * flowerScale;

        if (first) {
            t.goto(cx + x, cy + y);
            t.pendown();
            first = false;
        } else {
            t.goto(cx + x, cy + y);
        }
    }
}

drawFlower(flowerX, flowerY);
drawButterfly(butterflyX, butterflyY);