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 › noob - beginners guide
Page Index Toggle Pages: 1
noob - beginners guide? (Read 548 times)
noob - beginners guide?
Jul 27th, 2008, 5:07pm
 
Hello all -

I'm new to processing, and I'm looking for a good beginner's guide. Specifically, the issues I'm having trouble with are when to call the setup() and draw() functions, and how to return a value from the function.

I've looked through the references, and they're great for a reference, but they don't explain how and when to use something. The examples are great examples, but they don't help me understand how to properly constuct or troubleshoot my own code. I guess what I'm saying is that I'm looking for a significant amount of hand-holding at this point Wink

Here's a specific example of something I'm having problems with: I want to make a function that calculates the length of a line. The example for functions doesn't give an example function that returns a value. I couldn't find 'functions' in the reference. I only happened upon a reference by running into the keyword 'return'. So, I write this:

float lineLength( float x1, float y1, float x2, float y2 ){
 return sqrt( abs(x1-x2)^2 + abs(y1-y2)^2 ) );
}

When I run this in some code, I get 'unexpected token: float'. The project where I'm trying to add this runs otherwise, without my function, so I don't think it's a syntax error elsewhere in the code. I don't really have any idea why I'm getting this error.



Re: noob - beginners guide?
Reply #1 - Jul 27th, 2008, 5:35pm
 
use pow() for exponents:

return sqrt(pow(abs(x1-x2),2) + pow(abs(y1-y2),2)));
Re: noob - beginners guide?
Reply #2 - Jul 27th, 2008, 5:57pm
 
You can only use functions, if you use the setup() and draw() mode of programming.

And ^ isn't the power operator.. it's bitwise XOR.
Re: noob - beginners guide?
Reply #3 - Jul 27th, 2008, 9:11pm
 
You should also visit Help -> Getting Started, which covers setup(), draw(), and functions:
http://processing.org/reference/environment/index.html#Programming_modes
Re: noob - beginners guide?
Reply #4 - Jul 28th, 2008, 4:19am
 
Quote:
You can only use functions, if you use the setup() and draw() mode of programming.

And ^ isn't the power operator.. it's bitwise XOR.



Thanks John, that's helpful. Where can I find more information about the setup() and draw() mode?
Re: noob - beginners guide?
Reply #5 - Jul 28th, 2008, 6:53am
 
it's pretty straight forward:
setup() runs first..
here you type in the size of your sketch
preload any data / images / fonts

draw() is what runs continuously (if noLoop() is not set)
here you enter whatever needs to draw each time the screen should update

ie

void setup()
{
 size(300,300);
 // change default colors and strokethickness
 strokeWeight(5);
 stroke(40);
 fill(230);
}

void draw()
{
 ellipse(random(width),random(height),random(10),random(10));
}

This code gives you a new random ellipse every time a new frame is drawn.

Hope this explains some things
Re: noob - beginners guide?
Reply #6 - Jul 28th, 2008, 9:33pm
 
you can also define objects before setup if you want them to be usable throughout the program.. i.e. if you declare 'int x = 50;" in the setup, then "int x" is only available in setup. Whereas if you declare it before setup it is available in the setup class as well as draw and any other class in the program.

Check out the 'Learning' section of the page and look at how others use the code, lots of useful stuff in there.. and if you are really serious about Processing I'd recommend getting  "Processing: A Programming Handbook for Visual Designers and Artists" by Casey Reas and Ben Fry. It has a lot of useful information in a very readable format.
Page Index Toggle Pages: 1