ascii drawing application
an application that works not unlike illustrator, with the exception that it renders it s shapes as ASCII images.
this could be useful for example for markdown documents.
ArrayList<PVector> mLineSegments = new ArrayList();
PVector mSegmentElementA = new PVector();
final float mScale = 25;
void settings() {
size(1024, 768);
}
void setup() {
textFont(createFont("Courier", 10));
textAlign(CENTER);
rectMode(CENTER);
mLineSegments.add(mSegmentElementA);
}
void draw() {
mSegmentElementA.set(transformMouse());
background(255);
noStroke();
translate(width/2, height/2);
scale(mScale);
fill(255, 127, 0);
for (int i=1; i < mLineSegments.size(); i++) {
PVector a = mLineSegments.get(i-1);
PVector b = mLineSegments.get(i);
bresenham(a.x, a.y, b.x, b.y);
}
}
void mousePressed() {
mSegmentElementA = new PVector();
mLineSegments.add(mSegmentElementA);
}
PVector transformMouse() {
float mX = ( mouseX - width/2 ) / (float)mScale;
float mY = ( mouseY - height/2 ) / (float)mScale;
return new PVector(mX, mY);
}
void plot(int x, int y) {
pushStyle();
noFill();
stroke(127);
strokeWeight(1.0/mScale);
rect(x, y, 1, 1);
popStyle();
pushMatrix();
translate(x, y);
scale(1.0/mScale);
scale(10 * mScale / 100);
translate(-1, 0.265 * (textAscent()+textDescent()));
text("X", 0, 0);
popMatrix();
}
// from https://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#Java
void bresenham(float x1, float y1, float x2, float y2) {
bresenham((int)x1, (int)y1, (int) x2, (int)y2);
}
void bresenham(int x1, int y1, int x2, int y2) {
// delta of exact value and rounded value of the dependent variable
int d = 0;
int dx = Math.abs(x2 - x1);
int dy = Math.abs(y2 - y1);
int dx2 = 2 * dx; // slope scaling factors to
int dy2 = 2 * dy; // avoid floating point
int ix = x1 < x2 ? 1 : -1; // increment direction
int iy = y1 < y2 ? 1 : -1;
int x = x1;
int y = y1;
if (dx >= dy) {
while (true) {
plot(x, y);
if (x == x2)
break;
x += ix;
d += dy2;
if (d > dx) {
y += iy;
d -= dx2;
}
}
} else {
while (true) {
plot(x, y);
if (y == y2)
break;
y += iy;
d += dx2;
if (d > dy) {
x += ix;
d -= dy2;
}
}
}
}