Skip to content

Instantly share code, notes, and snippets.

@eqs
Created November 16, 2021 11:21
Show Gist options
  • Save eqs/bf63d4d07fdaf131ab513555c4362cc8 to your computer and use it in GitHub Desktop.
Save eqs/bf63d4d07fdaf131ab513555c4362cc8 to your computer and use it in GitHub Desktop.
めも
final int N = 2048;
float[][] radii = new float[][] {
{0.3, 0.3}, {0.7, 0.3}, {0.7, 0.7}, {0.3, 0.7}
};
// offset用
float[][] signs = new float[][] {
{1.0, 1.0}, {-1.0, 1.0}, {-1.0, -1.0}, {1.0, -1.0}
};
void setup() {
size(800, 800);
}
void draw() {
background(220);
translate(width / 2.0, height / 2.0);
scale(0.85); // 全体をちょっとだけ小さくする
rotate(-PI); // CSSのborder-radius設定時と見た目を合わせる
fill(255);
stroke(0);
strokeWeight(2.0);
beginShape();
for (int k = 0; k < N; k++) {
float t = TWO_PI / N * k;
int i = findVertexIndex(t, 4);
float a = width * radii[i][0];
float b = height * radii[i][1];
float x = a * cos(t);
float y = b * sin(t);
float offsetX = signs[i][0] * (width / 2.0 - a);
float offsetY = signs[i][1] * (height / 2.0 - b);
vertex(x + offsetX, y + offsetY);
}
endShape(CLOSE);
}
int findVertexIndex(float t, int nVertices) {
for (int k = 0; k < nVertices; k++) {
float s1 = TWO_PI / nVertices * k;
float s2 = TWO_PI / nVertices * (k + 1);
if (s1 <= t && t < s2) {
return k;
}
}
return -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment