|
Author: bla on June 11 2008
Viewed 2803 times. 9 people liked this blog. You can rate it below if you haven't already.
-->
i need someone to write a (probably) very simple program for me
its to generate a picture of black and white squares (pixels on or off)
i dont know shit about programming (except a bit of zx spectrum basic back in the day) but im pretty sure itd be easy to do for someone whos into coding
theres not much to it- ive been doing it 'by hand' but i always make mistakes so i need to get a computer to do it
anyway, it goes like this:
start in the middle square and go round in a spiral 1 square at a time (might as well be clockwise first going up then right then down, down, left ,left, up , up ,up, right.... etc)
the only rule to determine whether each square is black or white is if theres an even number of black squares adjacent to it then its black, if its an odd number then its white
thats it
i REALLY want to see what it looks like with 1000's of squares but doing it by hand is a real pain and its almost impossible not to make mistakes- ive done it on graph paper to about 100x100 but i need to see it much bigger to see what structures develop in it
if noone fancies it then i guess i could take suggestions for programming languages and tutorials but im a really slow learner
| |
Comments
06/11/08
+
PM |
QUOTE |
PERMALINK |
REPORT
lematt
i'm sure it would be easy to do with nodebox > link even if the spiral thingie is too tricky for me...
daswesen could code it in 2 minutes for sure.
06/11/08
+
PM |
QUOTE |
PERMALINK |
REPORT
bla
ahh -thats for osx
im using a windows xp machine
06/11/08
+
PM |
QUOTE |
PERMALINK |
REPORT
lematt
oh sorry... i guess then you have to try with processing > link but it's a bit harder IMO
06/11/08
+
PM |
QUOTE |
PERMALINK |
REPORT
bla
hmmmm i dont like the sound of 'harder'- im prone to logic blindness
but i check it out anyway- even if its just for long term learning possibilities
i might have to find my zx spectrum and see what i can do- actually no itd only be 192x192 at most so thats not enough
06/11/08
+
PM |
QUOTE |
PERMALINK |
REPORT
cbit
Absolutely check processing for this! there are a few folks round here who've used processing, and more who are good at programming in general so if/when you get stuck (after working through the tutorials) there are plenty of folks here who can help you with it.
A big advantage of doing it yourself is that you can then easily explore emergence further by exoerimenting with differnt 'rules' that determine whether the pixel flips or not. It'll be a lot of fun.
06/11/08
+
PM |
QUOTE |
PERMALINK |
REPORT
license
it smells to me like the results couldn't be totally accurate. I think you could use the algorithm per-pixel but it wouldn't hold for the whole thing. I certainly could be wrong, though and anyway I don't think accuracy is what you're going for here.
that said the actual setting of the pixels is pretty trivial except at the edges. not sure whether you want to count diagonals but that's trivial too.
not sure about the spiral alg, that seems a little harder. I could quickly whip up some sloppy pseudocode for going left-to-right, right-to-left like a dot matrix printer.
06/11/08
+
PM |
QUOTE |
PERMALINK |
REPORT
cbit
pseudo/skeleton code (not checked, not syntactically correct probably either), this is how i'd do it though:
function setup(){
sideLength=1 // determines how far away the next 'corner', or change of direction is
currentDirection=1 // 1,2,3,4 is up,right,down,left
currentPixelX=100
currentPixelY=100
targetPixelX=100
targetPixelY=100
}
// do this over and over
function mainloop(){
checkTargetPixel()
nextPixel()
colourPixel()
}
function checkTargetPixel(){
// are we at the target pixel yet?
// if so, set a new destination
if (currentPixelX==targetPixelX && currentPixelY==targetPixelY){
nextDirection()
nextTargetPixel()
}
}
function nextPixel(){
// go one step closer to target pixel
if(currentDirection==1){
currentPixelY=currentPixelY-1 //up
}
else if(currentDirection==2){
currentPixelX=currentPixelX+1 //right
}
else if(currentDirection==3){
currentPixelY=currentPixelY+1 //down
}
else if(currentDirection==4){
currentPixelX=currentPixelX-1//left
}
}
function colourThisPixel(){
// insert code here to check neighbouring pixels colours
// count black ones, and colour this one accordingly
}
function nextDirection(){
currentDirection++
if (currentDirection>4){
currentDirection=1
}
}
function nextTargetPixel(){
if(currentDirection==1){
sideLength++ // make sidelength one longer, to clear the existing spiral lines
targetPixelY=currentPixelY-sideLength //up
}
else if(currentDirection==2){
targetPixelX=currentPixelX+sideLength //right
}
else if(currentDirection==3){
sideLength++ // make sidelength one longer, to clear the existing spiral lines
targetPixelY=currentPixelY+sideLength //down
}
else if(currentDirection==4){
targetPixelX=currentPixelX-sideLength //left
}
}
06/11/08
+
PM |
QUOTE |
PERMALINK |
REPORT
jogn
Just offhand this is how you need to do it in processing. pseudo code alert!
xsize=3;
ysize=3;
boxes=1000;
setup(){
size(boxes*xsize,boxes*ysize);
}
startx=determine the middle box index number;
starty=determine the middle box index number;
draw(){
insert bunch of rules here;
drawbox(idx,idy);
}
drawbox(int idx,int idy){
absx=idx*boxsize;
absy=idy*boxsize;
use the processing shape primitive ?quad or ?rect, etc, and draw a box of boxsize dimensions
}
Or you could initialise an array of the size of your graphic, and use that to query which block is on or off and then draw the boxes based on that array.
06/11/08
+
PM |
QUOTE |
PERMALINK |
REPORT
jogn
hehehe
06/11/08
+
PM |
QUOTE |
PERMALINK |
REPORT
license
lol. damn you beat me :P
06/11/08
+
PM |
QUOTE |
PERMALINK |
REPORT
license
I'll post mine anyway cos I hate when my code goes to waste, lol. happens often enough at work...
this is for dot matrix style
//assumes 0 for white and 1 for black
var sizex = 256; //height & width
var sizey = 256;
var x = 0; //position, not size
var y = 0;
var xdirection = 1 //right
while(y < sizey){
do{
fillPoint(x,y);
x += xdirection; //move one left or right
}while((x < (sizex-1)) && (x > 0));//stop at edge
y++; //move down
xdirection = (0 - xdirection); //reverse x direction
}
function fillPoint(x, y){
left = getPixelValue(x-1,y);
right = getPixelValue(x+1,y);
up = getPixelValue(x,y-1);
down = getPixelValue(x,y+1);
totalAdjacent = left + right + up + down;
if((totalAdjacent % 2)==0){ //if even (modulus 2 is 0)
color = 1; //black
}else{ //otherwise, it's odd
color = 0;
}
noReallyFillPoint(x, y, color);
}
function getPixelValue(x, y){
global sizex, sizey;
if((x < 0) || (y < 0)){ //out of bounds
return 0; //white
}else if((x > sizex) || (y > sizey)){ //out of bounds
return 0; //white
}else{ //in bounds
return noReallyGetPixelValue(x, y);
}
}
06/11/08
+
PM |
QUOTE |
PERMALINK |
REPORT
bla
i havent checked processing yet so i dont really understand what all that means but i can kind of follow whats going on there, cbit
the direction and side length thing looks like how i imagined i would do it in spectrum basic
license- im going for totally accurate with this- otherwise id just do something random- the whole point of this is theres nothing random about it
its a one off calculation for each pixel- they dont get recalculate after the next layer of the spiral is added
it should look like it is in the blog pic (although its almost certain to have mistakes in it)
ill have a go with processing but im not confident- i get easily confused and stuck on stuff i dont understand
06/11/08
+
PM |
QUOTE |
PERMALINK |
REPORT
bla
license- i really dont know if doing it row by row will achieve the same kind of thing
06/11/08
+
PM |
QUOTE |
PERMALINK |
REPORT
license
yeah, it'll be totally different. I did it in a modular way though so you can ignore that first part: while (y < sizey)
you can plug in the getPixelValue and fillPoint functions into cbit's. luckily we did 2 different pieces of the problem so they complement each other.
06/11/08
+
PM |
QUOTE |
PERMALINK |
REPORT
bla
oh and diagonals do count
Register / login
|
^
EM411 is Copyright 2001-2008 EM411.com
All rights reserved. | Contact | RSS
|