Script: Double Polygon - Poyo-SSB/osu-playground GitHub Wiki

function start() {
    var baseCenter = Playground.AddVector2('baseCenter', new Vector2(Playground.PLAYFIELD_WIDTH / 2, Playground.PLAYFIELD_HEIGHT / 2));
    var baseRadius = Playground.AddFloat('baseRadius', 100);
    var rotation = Playground.AddFloat('rotation', 180);
    var ngon = Playground.AddInt('ngon', 10);
    var majorSideCount = Playground.AddInt('majorSideCount', 5);

    var radiusMultiplier = Playground.AddFloat('radiusMultiplier', 0.75);
    var minorSideCount = Playground.AddInt('minorSideCount', 5);

    Playground.AddOptionVector2(baseCenter, 'Base center');
    Playground.AddOptionFloat(baseRadius, 'Base radius', 0, 40, 150);
    Playground.AddOptionFloat(rotation, 'Rotation', 1, 0, 360);
    Playground.AddOptionInt(ngon, 'N-gon', 3, 16);
    Playground.AddOptionInt(majorSideCount, 'Major side count', 1, ngon);

    Playground.AddOptionFloat(radiusMultiplier, 'Radius multiplier', 2, 0, 2);
    Playground.AddOptionInt(minorSideCount, 'Minor side count', 1, ngon);
}

function update() {
    var baseCenter = Playground.GetValueVector2('baseCenter');
    var baseRadius = Playground.GetValueFloat('baseRadius');
    var rotation = Playground.GetValueFloat('rotation');
    var ngon = Playground.GetValueInt('ngon');
    var majorSideCount = Playground.GetValueInt('majorSideCount');
    var radiusMultiplier = Playground.GetValueFloat('radiusMultiplier');
    var minorSideCount = Playground.GetValueInt('minorSideCount');

    var points = [];

    var basePoint = add(baseCenter, new Vector2(baseRadius, 0));
    var offset = rotation / 180 * Math.PI;
    for (var i = 0; i < majorSideCount + 1; i++) {
        points.push(rotate(basePoint, baseCenter, offset + i * 2 * Math.PI / ngon));
    }

    basePoint = points[points.length - 1];
    var previousPoint = points[points.length - 2];

    var radiusAngle = (Math.PI - (Math.PI / ngon)) / 2;
    var sideAngle = Math.atan2(basePoint.y - previousPoint.y, basePoint.x - previousPoint.x);

    var newRadius = baseRadius * radiusMultiplier;

    var newCenter = subtract(basePoint, new Vector2(newRadius, 0));
    newCenter = rotate(newCenter, basePoint, sideAngle);
    newCenter = rotate(newCenter, basePoint, radiusAngle);

    for (var i = 0; i < minorSideCount + 1; i++) {
        points.push(rotate(basePoint, newCenter, i * 2 * Math.PI / -ngon));
    }

    Playground.AddSlider(CurveType.Linear, points);
}