FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Topics & Contributions
   Beyond Categories
(Moderator: REAS)
   first code
« Previous topic | Next topic »

Pages: 1 2 
   Author  Topic: first code  (Read 6135 times)
adrien

WWW
first code
« on: Aug 3rd, 2002, 12:57am »

post your first experiments with P5!
 

Signature: Signatures are displayed at the bottom of each post...
adrien

WWW
hello world
« Reply #1 on: Aug 3rd, 2002, 1:01am »

my hello world program is a scribbler. it prints hello world to the screen, but in a very verbose alien language resembling what earthlings might draw on crack.
 
// scribble
// by Adrien Cater
// --my first P5 code--
 
int width = 412;
int height = 256;
int freak = 32;
float x1 = width / 2;
float y1 = height / 2;
float x2 = x1 + random(freak * 1, freak * -1);
float y2 = y1 + random(freak * 1, freak * -1);
float mycolor = 153;
 
 
 
void setup()  
{
  size(width, height);
  background(51);
}
 
void loop()  
{
  noBackground();
  stroke(mycolor);
  noFill();
  line(x1, y1, x2, y2);
  x1 = x2;
  y1 = y2;
  mycolor += random(freak * 1, freak * -1);
  x2 += random(freak * 1, freak * -1);
  y2 += random(freak * 1, freak * -1);
 
  if(x2 < 0){x2 += freak;}
  if(x2 > width){x2 -= freak;}
 
  if(y2 < 0){y2 += freak;}
  if(y2 > height){y2 -= freak;}
   
  if(mycolor < 0){mycolor += freak;}
  if(mycolor > 255){mycolor -= freak;}
}
 

Signature: Signatures are displayed at the bottom of each post...
eviltyler

WWW
Re: first code
« Reply #2 on: Aug 3rd, 2002, 2:02am »

// simple cube rotation
// by tyler weir
// a simple cube that you can rotate with the keys, w, // a, s, and d.
 
// globals
int windowHeight = 200;
int windowWidth = 200;
float a;
float b;
 
 
// setup
void setup()
{
  size(windowHeight, windowWidth);
  background(255, 204, 0);
  lights();
}
 
 
// loop
void loop()
{
  translate((windowWidth/2), (windowHeight/2), 0);
   
  if(keyPressed)
  {  
    if (key == 'd' || key == 'D')
    {
 b += goingRight();
    }
     
    if (key == 'a' || key == 'A')
    {
 b += goingLeft();
    }
     
    if (key == 'w' || key == 'W')
    {
 a += goingUp();
    }
     
    if (key == 's' || key == 'S')
    {
 a += goingDown();
    }
  }
 
  rotateX(a);
  rotateY(b);
  box(40);
 
}
 
 
// util functions
float goingUp()
{
  return 0.1;
}
 
float goingDown()
{
  return -0.1;
}
 
float goingRight()
{
  return 0.1;
}
 
float goingLeft()
{
  return -0.1;
}
 
 
 
 
//EDIT: added header comments
« Last Edit: Aug 3rd, 2002, 1:34pm by eviltyler »  
ik0

2028856820288568 WWW
Re: first code
« Reply #3 on: Aug 3rd, 2002, 3:04am »

Took the liberty to play around with some values etc  
in the example motion02 code.
Not sure if this is usefull to anybody
 
original
http://www.proce55ing.net/learning/examples/motion02.html
 
alternated
http://www.prac.net/proce55ing/motion02ik1/
http://www.prac.net/proce55ing/motion02ik2/
 
Processing

WWW Email
Re: first code, Please Comment
« Reply #4 on: Aug 3rd, 2002, 9:33am »

Please follow Adrien's example and write a few lines of comments at the beginning of your programs. If you are basing your new code on previous code please make a reference. For example:  
 
// Example 001  
// by Proce55ing
// based on Scribble by Adrien Cater  
// This program ...
 
Also, commenting your code well is really helpful to everyone. (We'll try to do a better job with this in the future as well)
 
Mike Davis

WWW
Re: first code
« Reply #5 on: Aug 3rd, 2002, 11:24am »

// Conway's Game of Life
// by Mike Davis
// This program is a simple version of Conway's
// game of Life.  A lit point turns off if there
// are fewer than two or more than three surrounding
// lit points.  An unlit point turns on if there
// are exactly three lit neighbors.  The 'density'
// parameter determines how much of the board will
// start out lit.
 
// set canvas size, 'on' cell density
 
int sx = 100, sy = 100;
float density = 0.1;
 
int[][][] world = new int[sx][sy][2];
 
void setup()
{
  size(sx,sy);
  background(0);
  stroke(255);
  
  // Set random cells to 'on'
  for (int i = 0; i < sx * sy * density; i++) {
    world[(int)random(sx)][(int)random(sy)][1] = 1;
  }
}
 
void loop()
{
  // Drawing and update cycle
  for (int x = 0; x < sx; x=x+1) {
    for (int y = 0; y < sy; y=y+1) {
      if (world[x][y][1] == 1)
      {
        world[x][y][0] = 1;
        point(x, y);
      }
      if (world[x][y][1] == -1)
      {
        world[x][y][0] = 0;
      }
      world[x][y][1] = 0;
    }
  }
  // Birth and death cycle
  for (int x = 0; x < sx; x=x+1) {
    for (int y = 0; y < sy; y=y+1) {
      int count = neighbors(x, y);
      if (count == 3 && world[x][y][0] == 0)
      {
        world[x][y][1] = 1;
      }
      if ((count < 2 || count > 3) && world[x][y][0] == 1)
     {
        world[x][y][1] = -1;
      }
    }
  }
}
 
// Count the number of adjacent cells 'on'
int neighbors(int x, int y)
{
  return world[(x + 1) % sx][y][0] +
         world[x][(y + 1) % sy][0] +
         world[(x + sx - 1) % sx][y][0] +
         world[x][(y + sy - 1) % sy][0] +
         world[(x + 1) % sx][(y + 1) % sy][0] +
         world[(x + sx - 1) % sx][(y + 1) % sy][0] +
         world[(x + sx - 1) % sx][(y + sy - 1) % sy][0] +
         world[(x + 1) % sx][(y + sy - 1) % sy][0];
}
« Last Edit: Aug 6th, 2002, 9:11pm by Mike Davis »  
eviltyler

WWW
Re: first code
« Reply #6 on: Aug 3rd, 2002, 1:37pm »

mike, your game of life is pretty badass.  nice work.
 
//edit: corrected terrible grammar mistake
« Last Edit: Aug 5th, 2002, 4:13pm by eviltyler »  
tomek

WWW Email
Re: first code
« Reply #7 on: Aug 3rd, 2002, 6:02pm »

Is there a way to showcase directly on this board examples produced in proce55ing? Or just still images? That would make the 'examples' section more interesting to browse through. What I mean the code is interesting..., but the end result is what is really interesting! Actually, this post probably should go into improvements topics.
 
If there was a feature that would let you make 'attachements' to the messages on the board it would be ideal. Those would contain either source code or stills or actual application that is posted on the author's own website. It would make it more readable and browsable. Alternatively, can we use HTML tags to manually insert the links?
 
The programs will become longer and more complex and with larger community the source code will fill lots of space hiding the discussion. I would also hope that bringing the (final) work more to the focus it would redirect the discussion also towards the visual arts, design, composition, colors, etc... topics, not keeping it only around the programming and technical issues.
 
Any thoughts?
 
Takachin

WWW
Re: first code
« Reply #8 on: Aug 4th, 2002, 8:41am »

//Stripe One
//by Takachin
// based on Response 03 by Casey Reas (http://proce55ing.net/learning/examples/int03.html)
//This is my 1st code.
 
float xpos1 = 0.0;
void setup()  
{
  size(200, 200);
  //background(117,147,19;
  noBackground();  
}
 
void loop()  
{
  float mx = mouseX * 0.4 - width/5;
 
  xpos1 += mx/10;
   
 line(xpos1,0,xpos1,200);
 if (xpos1 > width) {
   xpos1 = 0;
   mx = mouseX * 0.2 - width/5;
   stroke(204, 102, 0);
 } else if (xpos1 < 0) {
   xpos1 = width;
   mx = mouseX * 0.4 - width/5;
   stroke(98, 0, 255);
 }
 
}
 
 
 
jlee

Email
Re: first code
« Reply #9 on: Aug 7th, 2002, 7:26pm »

// AlphaPaint
// by john lee
//
// The unpack method is from Ken Perlin's
// http://mrl.nyu.edu/perlin/bucky/PixApplet.java  
// The blend method is from from Jim Blinn's book
// Dirty Pixels.
//
// Simple painting program that paints squares of red,
// green or blue which are
// blended with the background.
//
// Move the mouse to "paint" and click the mouse to  
// change the "brush."
//
 
int w = 512;
int h = 512;
 
// the size of the square being painted.
int s = h/16;
 
// the opacity of the current color
float a = 0.125;
 
color[] brush = new color[3];
 
// current color
int c;
 
int r = 0;
 
void setup() {
  size(w, h);
  noBackground();
   
  brush[0] = color(255, 0, 0);
  brush[1] = color(0, 255, 0);
  brush[2] = color(0, 0, 255);
 
  for (int i = 0; i < w*h; i++) {
    // set the canvas to black
    pixels[i] = 0;
     
    // set the canvas to white
    //pixels[i] = 0xffffffff;
  }
}
 
void loop() {
  if (mouseX != pmouseX && mouseY != pmouseY) {
    for (int i = 0; i < s; i++) {
 for (int j = 0; j < s; j++) {
 
   // simple bounds checking
   if (mouseX+i >= 0 && mouseX+i < w && mouseY+j >= 0 && mouseY+j < h) {
     c = blend(brush[r], getPixel(mouseX+i, mouseY+j), a);
     setPixel(mouseX + i, mouseY + j, c);
   }
 }
    }
  }
}
 
void mouseReleased() {
  // change the current color.
  r = int(random(3));
}
 
int[] fg = new int[3];
int[] bg = new int[3];
 
int blend(color f, color b, float a) {
  unpack(fg, f);
  unpack(bg, b);
 
  for (int i = 0; i < 3; i++) {
    // F over B alpha blending
    fg[i] = int(bg[i] + a * (fg[i] - bg[i]));
  }
 
  return color(fg[0], fg[1], fg[2]);
}
 
// decomposes a color into an int array
void unpack(int[] rgb, color c) {
  rgb[0] = (c >>    16) & 255;
  rgb[1] = (c >> (8+0)) & 255;
  rgb[2] = (c    ) & 255;
}
 
Bertrand Saint-Guillain

170736171170736171super_satori WWW Email
Re: first code
« Reply #10 on: Aug 9th, 2002, 6:47pm »

// dots/grey
// by Bertrand Saint-Guillain
//
// hold space to keep scale
// hold w to keep B/W value
// very first code on prc55ng
// --> very dirty  
 
// --- global
int w=200; // width
int h=200; // height
float diam=w/2; // distance between dots - starts at half width
float posY=h/2; // let's say it's kind of an expression of the value
void setup()
{
  size(w, h);
  background(255);
}
void loop()
{
  // test for space
  if (!(keyPressed && key==' ')){
    // not pressed ? then diam tends towards mouse X/2  
    diam+=(min(max(mouseX/2,1),w)-diam)/15;
  }
  noStroke();
  float maxD; // max circle diameter
  float diameter; // current diameter
//  float mDia=sqrt(sq(diam*1.5)*2);  
  mDia=diam*2;  
  if (!keyPressed || !(key=='w')) {
    // w is not pressed ? then posY tends towards mouseY
    posY+=(mouseY-posY)/20;
  }
  if (posY<(h/2)) {
    // value < 0.5
    // big black dots on white
    background(255);
    fill(0);
    diameter=mDia-mDia*posY/h;
  }
  else {
    // value > 0.5
    // big white dots on black
    background(0);
    fill(255);
    diameter=mDia*posY/h;
  }
  float step=diam;
  ellipseMode(CENTER_DIAMETER);
  // if drawing white dots
  // we'll start in the corner
  int pair=(posY<h/2) ? 0 : 1 ;
  int xp;
  for (int cx=1;cx<=(w+diam);cx+=step) {
     xp=0;
     for (int  cy=0;cy<=(h+diam);cy+=step) {
       //  draw a circle out of two steps.
       xp++;
       if ((xp%2)==pair) {
           ellipse(cx,cy,diameter,diameter);
       }
     }
     pair= (pair+1)%2;
  }
}
« Last Edit: Aug 9th, 2002, 8:08pm by Bertrand Saint-Guillain »  
ezdac


Re: first code
« Reply #11 on: Sep 1st, 2002, 12:13pm »

hello,
here's one of my first codes.
it makes the screen like like a broken TV.
some others are at
http://ezdac.free.fr/ezp55/
if you want codes just e-mail me.  
 
//TVnaz
//by ezdac
//is like a broken TV
//you can change color and "shapes" by moving the mouse around
//slowly move from left to right
 
int r = 128;
int v = 128;
int b = 128;
int amp ;
float colr;
float colv;
float colb;
 
 
void setup()
{
size(200,200);
colorMode(RGB,255);
}
 
 
void loop()
{
  if(mousePressed)
  {delay(1000);}
   
  amp=mouseX/12;
  colr=(200-mouseY)/200.;
  colb=(mouseY)/200.;
  colv=abs(100-mouseY)/100.;
  for(int y=0;y<200;y++)
  {
    for(int x=0;x<200;x++)
    {
 r = r+ int(random(amp)-2);
    if ( r<0  ){r=255;}
    if ( r>255){r=0;}
 v = v+ int(random(amp)-2);
    if ( v<0  ){v=255;}
    if ( v>255){v=0;}
 b = b+ int(random(amp)-2);
    if ( b<0  ){b=255;}
    if ( b>255){b=0;}
     
   
    setPixel(x,y,color(r*colr,v*colv,b*colb));
    }
  }
}
 
anagma


Re: first code
« Reply #12 on: Oct 16th, 2002, 5:09pm »

Hi.this is my first code.
and this is preview_ http://www.iamas.ac.jp/~anagma00/proce55ing/
 
-----___=-;////////////////////////
 
int lineAmount = 80;
float[] rads = new float[360];
float[] sins = new float[360];
float[] coss = new float[360];
int[][] pos = new int[lineAmount][3];
int[] colors = new int[lineAmount];
int[] temppos = new int[4];
int offset = lineAmount;
boolean mode = false;
 
void setup()
{
  size(400, 400);
  colorMode(RGB, 255);
  background(0);
  stroke(255);
  ellipseMode(CENTER_DIAMETER);
  noFill();
  float cols = 200/lineAmount;
  for(int i = 0; i < 360; i++){
    rads[i] = radians(i);
    sins[i] = sin(rads[i]);
    coss[i] = cos(rads[i]);
  }
  for(int i = 0; i < lineAmount; i++){
    for(int j = 0; j < 3; j++){
      pos[i][j] = 0;
    }
    colors[i] = (int)(255-(cols*i));
  }
}
 
void loop()
{
  next();
  for(int i = 0; i < lineAmount; i++){
    temppos = pos[i];
    push();
    translate(width/2,height/2);
    rotateX(rads[temppos[0]]);
    rotateY(rads[temppos[1]]);
    rotateZ(rads[temppos[2]]);
    stroke(colors[i]);
    if(mode){
      ellipse(0,0,i*5+100,i*5+100);
    }else{
      line(-300,i*2-offset,300,i*2-offset);
    }
    pop();
  }
}
void mousePressed(){
  if(mode){
    mode = false;
  }else{
    mode = true;
  }
}
void next(){
  int[] temp = new int[3];
  temp[0] = (int)(((float)(mouseX)/width)*360);
  temp[1] = ((int)(((float)(mouseY)/width)*360)+90)%360;
  temp[2] = 179-(int)(((float)(mouseX)/width)*180);
  for(int i=lineAmount-1; i>0; i--){
    pos[i] = pos[i-1];
  }
  for(int i = 0; i<3; i++){
    if(pos[0][i]==temp[i]){
      temp[i] = pos[0][i];
    }else if(pos[0][i]>temp[i]){
      temp[i] = pos[0][i]-1;
    }else{
      temp[i] = pos[0][i]+1;
    }
    if(temp[i]>359){
      temp[i]=0;
    }
    if(temp[i]<0){
      temp[i] = 359;
    }
  }
  pos[0] = temp;
  
}
« Last Edit: Oct 16th, 2002, 5:14pm by anagma »  
Glen Murphy

WWW Email
Re: first code
« Reply #13 on: Oct 30th, 2002, 5:44am »

Going for extremely simple here. I'm sure you'll all be caught aghast by my coding style.
 
View at: http://bodytag.org/nav.php?u=ptest1/
 
Source:
--------------------
 
// PTEST1
// by Glen Murphy
 
// Extends the 'Combinging Transforms' example
// given on the Proce55ing site.
 
float a = 0.0f;
 
void setup() {
  size(320, 320);
  background(#eeeeee);
  stroke(#ffffff);
  fill(#cccccc);
  }
 
void loop() {
  a += 0.001;
  translate(230, 170, -100);
  for(float i = 120; i > 20; i--) {
    translate(i*0.01, i*0.01, 0);
    float rot = a*(1+(0.00002*i));
    rotateX(rot);
    rotateY(rot);
    rotateZ(rot);
    rect(-i, -i, -i, i);
    }
  }
« Last Edit: Oct 30th, 2002, 5:49am by Glen Murphy »  
Andrew

WWW
Re: first code
« Reply #14 on: Nov 3rd, 2002, 9:38am »

it's rather embarrassing to post this, but what the hell. my first sketch with proce55ing.
 
http://www.megatight.com/p5/1/
 
the source is at
http://www.megatight.com/p5/1/source.txt
 
Pages: 1 2 

« Previous topic | Next topic »