Skip to content

Instantly share code, notes, and snippets.

@kadamwhite
Last active April 24, 2019 02:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kadamwhite/bc66d34a7a8e2bb357191bf7c20012cb to your computer and use it in GitHub Desktop.
Save kadamwhite/bc66d34a7a8e2bb357191bf7c20012cb to your computer and use it in GitHub Desktop.
Dashed Rectangle (Processing)
import processing.pdf.*;
//boolean saveOneFrame = false;
int randomInt( int min, int max ) {
return floor( random( min, max ) );
}
void dashedHorizontalLine( int x1, int y1, int x2, int y2 ) {
if ( y1 != y2 ) {
// not vertical, move on silently.
return;
}
// Ensure x1 is less than x2
if ( x1 > x2 ) {
dashedVerticalLine( x2, y2, x1, y1 );
return;
}
// Start at or near x1; expand the range to values less than x1 so
// there's a ~25% chance of the line starting right at that value.
int x = randomInt( x1 - 10, x1 + 30 );
if ( x < x1 ) {
x = x1;
}
while ( x < x2 ) {
// Each segment is 10-50 long. Could be adapted later to be proportional
// to the length of the line being drawn, but this is easier for now.
int nextX = x + randomInt( 10, 50 );
if ( nextX > x2 ) {
// Arbitrary threshold: don't draw line if next point is > 15 past corner.
if ( ( nextX - x2 ) > 15 ) {
return;
}
nextX = x2;
}
line( x, y1, nextX, y1 );
// Each line is 15-30 awax from the next segment.
x = nextX + randomInt( 15, 30 );
}
}
void dashedVerticalLine( int x1, int y1, int x2, int y2 ) {
if ( x1 != x2 ) {
// not vertical, move on silently.
return;
}
// Ensure y1 is less than y2
if ( y1 > y2 ) {
dashedVerticalLine( x2, y2, x1, y1 );
return;
}
// Start at or near y1; expand the range to values less than y1 so
// there's a ~25% chance of the line starting right at that value.
int y = randomInt( y1 - 5, y1 + 15 );
if ( y < y1 ) {
y = y1;
}
while ( y < y2 ) {
// Each segment is 10-50 long. Could be adapted later to be proportional
// to the length of the line being drawn, but this is easier for now.
int nextY = y + randomInt( 10, 50 );
if ( nextY > y2 ) {
// Arbitrary threshold: don't draw line if next point is > 15 past corner.
if ( ( nextY - y2 ) > 15 ) {
return;
}
nextY = y2;
}
line( x1, y, x1, nextY );
// Each line is 15-30 away from the next segment.
y = nextY + randomInt( 15, 30 );
}
}
void dashedRect( int x, int y, int width, int height ) {
int x2 = x + width;
int y2 = y + height;
dashedHorizontalLine( x, y, x2, y );
dashedHorizontalLine( x, y2, x2, y2 );
dashedVerticalLine( x, y, x, y2 );
dashedVerticalLine( x2, y, x2, y2 );
}
void setup() {
background (255, 0);
frameRate(6);
size(400, 600);
//if (saveOneFrame == true) {
// beginRecord(PDF, "lines.pdf");
//}
}
void draw() {
background( 255 );
dashedRect( 50, 50, 300, 200 );
}
void mousePressed() {
//saveOneFrame = true;
endRecord();
}
//saveFrame("dashed_paths-####.png");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment