I have written generic Point classes, line/line intersection algorithms and Layer administration code more times than I can remember. I don't like menial programming, so this is effectively my code library. But you can use it too!
The language used is Processing, which is a mostly-Java-syntax language. I hardly ever use Java-specific ninja code, so rewriting any of these snippets to your favourite language is less than a minute's worth of work, typically.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | final float CIRCLE_ARC = 4.0 * (sqrt(2)-1)/3.0;
void setup() {
size(600,600);
noLoop();
}
void draw() {
roundrect(10,10,width-20,height-20,50,CIRCLE_ARC);
}
/**
* Draw a rounded rect using bezier corners.
* k controls the tightness of the rounded corner.
*/
void roundrect(float x, float y, float w, float h, float r, float k) {
float xw=x+w, yh=y+h, xr=x+r, yr=y+r, mk=1-k, km=k-1;
beginShape();
vertex(xr, y);
vertex(xw-r, y);
bezierVertex(xw + km*r, y, xw, y + mk*r, xw, yr);
vertex(xw, yh-r);
bezierVertex(xw, yh + km*r, xw + km*r, yh, xw-r, yh);
vertex(xr, yh);
bezierVertex(x + mk*r, yh, x, yh + km*r, x, yh-r);
vertex(x, yr);
bezierVertex(x, y + mk*r, x + mk*r, y, xr, y);
endShape(CLOSE);
} |
Written and maintained by Mike "Pomax" Kamermans. All code snippets on this site are "do whatever you want with it" licensed.