10 PRINT CHR$(205.5+RND(1)); : GOTO 10
Can this be done in Processing?
Sure, but itâs a little different because reasons.
size(600,400);int x=0,y=0;while(y<height){while(x<width){if(random(2)<1) line(x,y,x+20,y+20);else line(x+20,y,x,y+20);x+=20;}x=0;y+=20;}
Thank you!!!
size(600, 400);
int x=0, y=0;
while (y < height) {
while (x < width)
{
if (random(2) < 1) line(x, y, x+20, y+20);
else line(x+20, y, x, y+20);
x+=20;
}
x=0;
y+=20;
}
And of course then you can tinker with it to keep it from being boring.
size(1000, 1000);
background(0);
int x=0, y=0;
while (y<height) {
while (x<width) {
stroke(
map(x,0,width,0,255),
map(y,0,height,0,255),
255-map(x+y,0,width+height,0,255)
);
if (random(2)<1){
line(x, y, x+20, y+20);
} else {
line(x+20, y, x, y+20);
}
x+=20;
}
x=0;
y+=20;
}
Wheee!
class Lino {
int x, y, ttb;
boolean b;
Lino(int ix, int iy) {
x = ix;
y = iy;
ttb = 0;
b = random(2)<1;
}
void draw() {
stroke(
map(x, 0, width, 0, 255),
map(y, 0, height, 0, 255),
255-map(x+y, 0, width+height, 0, 255)
);
if (b) {
line(x, y, x+20, y+20);
} else {
line(x+20, y, x, y+20);
}
}
void swap(){
b = !b;
}
}
ArrayList<Lino> linos;
void setup() {
size(600, 600);
linos = new ArrayList();
int x=0, y=0;
while (y<height) {
while (x<width) {
linos.add(new Lino(x,y));
x+=20;
}
x=0;
y+=20;
}
}
void draw() {
background(0);
for( Lino lin : linos){
lin.draw();
}
for( int t = 0; t < 10; t++){
linos.get(int(random(linos.size()))).swap();
}
}
Wow, wow and more wow! @TfGuy44
I think your first example is better because part of the aesthetic is to get the complexity into one line of code (or nearly).
What a good old time. I want it back!
Is there a way to make it draw really slowly, line by line, turn by turn so we can see the maze being formed?
Replace the while with if and youâve got it slower (though the Speed depends on frameRate).
Like this :
void setup() {
size(600, 400);
}
int x=0, y=0;
void draw() {
if (y < height) {
if (x < width)
{
if (random(2) < 1) line(x, y, x+20, y+20);
else line(x+20, y, x, y+20);
//x+=20;
}
//x=0;
y+=20;
} else {
x+=20;
y = 0;
}
}
@paulstgeorge â great question!
In the 10 PRINT book, check out the section on Variations in Processing (105-118). We discuss using the console, basic output, and different methods of visual styling â with full code examples.
The one-liner is: EDIT
void draw() { print((random(1) < 0.5) ? '/' : '\\'); }
âŠalthough the result is different from a Commodore 64 display!
If you donât have a copy of the book handy the full PDF is freely available from http://10print.org
@TfGuy44 gives a long âone-linerâ for visual output that is really compelling â a related way to approach that would be to create a char and then use text() rather than line().
@Lexyth gives a nice example of a typewriter-like display. Another method is to add a characters / bit each frame to a buffer, then loop over the whole buffer. This would lets you more easily mimic the line advance behavior when the screen fills.
Um, is
void draw() {print((random(1) < 0.5 ? '/' : '\\'));}
better? Even number of brackets, and ().
Excellent! I added frameRate(1); to your code @Lexyth and it works perfectly.
Ah, yesâI fixed the typo in my post above.
Kids: donât post ternary one-liners from your cell phone. Smokey the Bear says: test code first.
void draw(){print(random(2)<1?'/':'\\');}
def draw():this.print(random(2)<1and'/'or'\\')
10 Excellent book, thank you. Have just read it in one sitting.
20 GOTO 10
This makes we want to fire up my C64!
This was my go at this:
size(500, 500);
int x = 0, y = 0;
int s = 25;
int r;
while (y < height)
{
r = int(random(2))*s;
line(x+r, y, x+s-r, y+s);
x += s;
if (x > width)
{
y+=s;
x=0;
}
//println(x, y);
}
My attempt at this came out pretty much the same as others.
I did the line a bit differently; tried to make it short and not use if() else().
I looked at examples after and removed setup() and draw().
That was fun!
Very elegant. Yours also works with fewer lines:
size(500, 500);
int x = 0, y = 0;
int s = 25;
int r;
while (y < height) {r = int(random(2))*s; line(x+r, y, x+s-r, y+s); x += s;
if (x > width) {y+=s; x=0;} }
A bit leaner:
size(500,500);int x=0,y=0;while(y<height){int r=int(random(2))*25;line(x+r,y,x+25-r,y+25);x+=25;if(x>width){y+=25;x=0;}}
size(500, 500);
int x=0, y=0;
while (y<height) {
int r=int(random(2))*25;
line(x+r, y, x+25-r, y+25);
x+=25;
if (x>width) {
y+=25;
x=0;
}
}
Touché Truchet! @glv
OK, shall we make a group effort for a curved maze?
The result should look something like this.
Each tile is randomly this:
noFill();
arc(25, 25, 50, 50, 0, HALF_PI);
arc(75, 75, 50, 50, PI, PI+HALF_PI);
or this:
noFill();
arc(25, 75, 50, 50, PI+HALF_PI, TWO_PI);
arc(75, 25, 50, 50, HALF_PI, PI);
The origins of each tile can be pulled from a 2D array (except I need help with that!). Or, there might be a better way to orientate and position the tiles.
int originX = 50;
int originY = 50;
noFill();
//this:
arc(originX-25, originY+25, 50, 50, PI+HALF_PI, TWO_PI);
arc(originX+25, originY-25, 50, 50, HALF_PI, PI);
// or this:
//arc(originX-25, originY-25, 50, 50, 0, HALF_PI);
//arc(originX+25, originY+25, 50, 50, PI, PI+HALF_PI);
Letâs do it!
So farâŠ
I have an arcDraw() function for drawing the arcs:
int y = 50;
int x = 50;
int[] z = { -1, -1, +1, -1, +1, +1, -1, +1};
void setup()
{
size(640, 360);
noFill();
}
void draw()
{
arcs2();
}
void arcs2()
{
int originX = 50;
int originY = 50;
int a = 0;
//Original from paulstgeorge
//this:
arc(originX-25, originY+25, 50, 50, PI+HALF_PI, TWO_PI);
arc(originX+25, originY-25, 50, 50, HALF_PI, PI);
// or this:
arc(originX-25, originY-25, 50, 50, 0, HALF_PI);
arc(originX+25, originY+25, 50, 50, PI, PI+HALF_PI);
//Arcs rearranged above to find a pattern and used TAU for TWO_PI
translate(100, 0);
arc(originX-25, originY-25, 50, 50, 0*TAU/4, 1*TAU/4);
arc(originX+25, originY-25, 50, 50, 1*TAU/4, 2*TAU/4);
arc(originX+25, originY+25, 50, 50, 2*TAU/4, 3*TAU/4);
arc(originX-25, originY+25, 50, 50, 3*TAU/4, 4*TAU/4);
//Arcs wigh method()
translate(100, 0);
for (a = 0; a<4; a++)
{
//arc(originX+z[2*a]*25, originY+z[2*a+1]*25, 50, 50, a*TAU/4, (a+1)*TAU/4);
arcDraw(a, 50, 50);
}
}
void arcDraw(int a, int originX, int originY)
{
arc(originX+z[2*a]*25, originY+z[2*a+1]*25, 50, 50, a*TAU/4, (a+1)*TAU/4);
}