Skip to content

Instantly share code, notes, and snippets.

@fjenett
Created March 31, 2010 06:56
Show Gist options
  • Save fjenett/350025 to your computer and use it in GitHub Desktop.
Save fjenett/350025 to your computer and use it in GitHub Desktop.
// dashed and dotted lines in Processing
// by Josh Nimoy, Based on Bresenham
void dashedLine ( float x0, float y0, float x1, float y1 )
{
dashedLine( (int)x0, (int)y0, (int)x1, (int)y1 );
}
void dashedLine ( int x0, int y0, int x1, int y1 )
{
int refX = x0; int refY = y0;
int dy = y1 - y0;
int dx = x1 - x0;
int stepx, stepy;
if (dy < 0) {
dy = -dy;
stepy = -1;
}
else {
stepy = 1;
}
if (dx < 0) {
dx = -dx;
stepx = -1;
}
else {
stepx = 1;
}
dy <<= 1;
dx <<= 1;
point(x0,y0);
if (dx>dy) {
int fraction=dy-(dx>>1);
//int ogx0 = x0;
//int ogy0 = y0;
double d;
while (x0!=x1){
if(fraction>=0){
y0 += stepy;
fraction -= dx;
}
x0 += stepx;
fraction += dy;
d = dist(refX,refY,x0,y0);
if(d%12 > 4)point(x0,y0);
}
}
else {
int fraction = dx - (dy>>1);
//int ogx0 = x0;
//int ogy0 = y0;
double d;
while (y0 != y1) {
if (fraction >= 0) {
x0 += stepx;
fraction -= dy;
}
y0 += stepy;
fraction += dx;
d = dist(refX,refY,x0,y0);
if(d%12 > 4)point(x0,y0);
}
}
}
void dotDashedLine ( float x0, float y0, float x1, float y1 )
{
dotDashedLine( (int)x0, (int)y0, (int)x1, (int)y1 );
}
void dotDashedLine ( int x0, int y0, int x1, int y1 )
{
int refX = x0; int refY = y0;
int dy = y1 - y0;
int dx = x1 - x0;
int stepx, stepy;
if (dy < 0) {
dy = -dy;
stepy = -1;
}
else {
stepy = 1;
}
if (dx < 0) {
dx = -dx;
stepx = -1;
}
else {
stepx = 1;
}
dy <<= 1;
dx <<= 1;
point(x0,y0);
if (dx>dy) {
int fraction=dy-(dx>>1);
//int ogx0 = x0;
//int ogy0 = y0;
double d;
while (x0!=x1){
if(fraction>=0){
y0 += stepy;
fraction -= dx;
}
x0 += stepx;
fraction += dy;
d = dist(refX,refY,x0,y0)%17;
if(d>9 || (d>3 && d<5))point(x0,y0);
}
}
else {
int fraction = dx - (dy>>1);
//int ogx0 = x0;
//int ogy0 = y0;
double d;
while (y0 != y1) {
if (fraction >= 0) {
x0 += stepx;
fraction -= dy;
}
y0 += stepy;
fraction += dx;
d = dist(refX,refY,x0,y0)%17;
if(d>9 || (d>3 && d<5))point(x0,y0);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment