Skip to content

Instantly share code, notes, and snippets.

@sspboyd
Last active August 29, 2015 14:24
Show Gist options
  • Save sspboyd/b9dfc7f48bdb97b9cb5a to your computer and use it in GitHub Desktop.
Save sspboyd/b9dfc7f48bdb97b9cb5a to your computer and use it in GitHub Desktop.
Drawing a dashed line on an angle - when seemingly simple things turn out to be hard, math to the rescue!
// Processing is a fantastic prototyping tool for me, but every once in a while I come across something that I think should be
// super simple and turns out to be surprisingly hard. For example, drawing a dashed line. Or, drawing a dashed line on an angle!!
PGraphics angDashes;
float boxW = 300;
float boxH = boxW;
float angLineLen, angLineBuff;
int angledDashCount = 12;
void setup() {
size(500, 500);
int angDW = int(sqrt(pow(boxW, 2) + pow(boxH, 2)));
angDashes = createGraphics(angDW, angDW);
angLineLen = angDW/(angledDashCount + ((angledDashCount-1) * 0.25));
angLineBuff = angLineLen * 0.25;
angDashes.beginDraw();
angDashes.background(255,50);
angDashes.stroke(0);
// angDashes.strokeWeight(2);
for (int i=0; i<angledDashCount+1; i++) {
float lineX = lerp(0, (angDW+angLineBuff), i/(angledDashCount*1.0));
angDashes.line(lineX, angDW/2, lineX+angLineLen, angDW/2);
}
angDashes.endDraw();
noFill();
rect(0,0,boxW, boxH);
pushMatrix();
translate(boxW/2,boxH/2);
rotate(HALF_PI/2);
image(angDashes, -angDW/2, -angDW/2);
popMatrix();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment