We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › code understanding
Page Index Toggle Pages: 1
code understanding ? (Read 574 times)
code understanding ?
Apr 11th, 2010, 5:25am
 
hey, im new to processing and was just wondering if any one could explain a few things to me:

1. what does adding a minus sign to your code mean e.g

"if ( y > height - img.height/2 ) { y = height - img.height/2; }"

what are the - for in this code

"void up() {
   y -= speed;"

and this code

2.  could anyone explain to me what this code is doing breifly (mainly the code highlighted with a * )


public class field
private Start start[];
private int count;
 
public Start( int count )
this.count = count;*
start = new Start[count];
for ( int b =0; b < count; b++) *
start[b] = new Start( random( width ), random( height ), random( 10 ));*
   
 
public void draw()
strokeWeight( 2 );
or ( int b =0; b < count; b++) *
stroke( start[b].z * 25 );*
point( start[b].x, start[i].y );*
   
start[b].x -= start[b].z;*
if (start[b].x < 0)  *
start[b] = new Start( width, random( height ), start(random( 100 )));
     
class Start
 float x, y, z;
 Start( float x, float y, float z )
   this.x = x;
   this.y = y;
   this.z = z;
 


3. and finally whats the differece between private and public ?

Many thanks in advance, if you can only answer one question please still post and anything at this stage would be helpful.  Cheesy

Re: code understanding ?
Reply #1 - Apr 11th, 2010, 5:39am
 
Quote:
1. what does adding a minus sign to your code mean e.g

"if ( y > height - img.height/2 ) { y = height - img.height/2; }"


Here minus does what you would expect - so you're comparing y against the value of height - half the image height.

Quote:
y -= speed;


This is a shortcut to writing:

y = y - speed;

See comments below for some explanations:

Code:
// this defines a class
public class Starfield {
 private Star stars[];
 private int count;
 
 //  this is the class constructor called when an object of this class is created
 public Starfield( int count ) {
   // This sets the class property 'count' to the value of the parameter in the constructor which is also referenced as 'count'.  This is perfectly valid, but you need to include 'this.' to reference the class count property rather than the parameter.
   this.count = count;*
   stars = new Star[count];
   // a for loop is usually repeated the number of times set by the value of limit - in this case 'count'.
   for ( int i =0; i < count; i++) {*
       // a new star object is created each time the loop is repeated
stars[i] = new Star( random( width ), random( height ), random( 10 ));*
   }
 }
 
 public void draw() {
   strokeWeight( 2 );
   for ( int i =0; i < count; i++) {*
stroke( stars[i].z * 25 );*
point( stars[i].x, stars[i].y );*
   
stars[i].x -= stars[i].z;*
if (stars[i].x < 0) { *
 stars[i] = new Star( width, random( height ), sqrt(random( 100 )));
}
   }
 }
}

class Star {
 float x, y, z;
 Star( float x, float y, float z ) {
   this.x = x;
   this.y = y;
   this.z = z;
 }
}


Quote:
3. and finally whats the differece between private a public


In a standard processing tab these don't actually have an effect: in a normal Java class a private property or method can only be accessed from within the class.

I'd suggest you look through the learning section and reference to get a better understanding of these concepts Wink
Re: code understanding ?
Reply #2 - Apr 11th, 2010, 5:48am
 
WOW THANK YOU SO MUCH.

is there any chance you could give a quick explanation as to what this code is doing:

public void draw()
strokeWeight( 2 );
for ( int b =0; b < count; b++)
stroke( start[b].z * 25 );
point( start[b].x, start[b].y );
   
start[b].x -= start[b].z;
if (start[b].x < 0)  
start[b] = new Start( width, random( height ), sqrt(random( 100 )));

And also when you say SPEED = - SPEED  is a quicker way to type what do you mean ?

Many, many thanks for your help  Smiley Smiley Smiley
Re: code understanding ?
Reply #3 - Apr 11th, 2010, 8:44am
 
elsamwell wrote on Apr 11th, 2010, 5:48am:
WOW THANK YOU SO MUCH.

is there any chance you could give a quick explanation as to what this code is doing:

public void draw()
strokeWeight( 2 );
for ( int b =0; b < count; b++)
stroke( start[b].z * 25 );
point( start[b].x, start[b].y );
   
start[b].x -= start[b].z;
if (start[b].x < 0)  
start[b] = new Start( width, random( height ), sqrt(random( 100 )));


I'd recommend you follow my suggestion: if you don't know what a function does (e.g. 'stroke' or 'point') look it up in the Reference (there's a link in the site navigation).  That should give you some indication of what the code actually does...

Quote:
And also when you say SPEED = - SPEED  is a quicker way to type what do you mean


What I actually wrote was:

Quote:
y -= speed;

...is a shortcut to writing:

y = y - speed;


This is one of those slightly odd constructions you see a lot in programming that seems counter-intuitive to what you might have learnt in maths lessons.  The thing to remember is that the equal sign is the assignment operator and does not imply equality.  In this case you're changing the value of y to be equal to its last known value minus speed.  If y is then used to position an object, that object will move up the screen each frame.

Since you're clearly starting out you really should go through the Learning section and get a better understanding of the basics...
Page Index Toggle Pages: 1