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 › Simple curves question
Page Index Toggle Pages: 1
Simple curves question (Read 204 times)
Simple curves question
Mar 20th, 2009, 11:18am
 
In the processing book there is a section about simple curves on page 82, 83. I get non of the examples of page 82 working can someone help me.

this is the code

for (int x = 0; x < 100; x++) {
 float n = norm(x, 0.0, 100.0);
 float y = pow(n, 0.4);
 y *= 100;
 point(x, y);
}

and on page 82 there are things like

y = 1-1(1-x)2
but where and how do I put that line, I tried serveral things but it always ends in a empty screen or just one point.
Re: Simple curves question
Reply #1 - Mar 20th, 2009, 2:59pm
 
Unfortunately I do not have the book with me at this moment.
Though the only thing you have to change in code you posted, is the part where y is set:

float y = pow(n, 0.4);

The only problem is that I do not know which formula you want to use, since the one you gave is not completely clear to me.
If it is: y = 1 - (1-x)^2, the solution should look like:
float y = 1-pow((1-n), 2);  

Since you use square and since   sq(a) = pow(a,2)   you could also use:
float y = 1-sq(1-n);  

Code:

for (int x = 0; x < 100; x++) {
 float n = norm(x, 0.0, 100.0);
 
// Option 1:
 float y = 1-pow((1-n), 2);
 
// Or option 2:
 // float y = 1-sq(1-n);
 
y *= 100;
 point(x, y);
}



Re: Simple curves question
Reply #2 - Mar 20th, 2009, 5:46pm
 
Yes.  TVS has it basically right.  The underlying issue is that you need to build up your skills in translating algebraic expressions into code.  For whatever reason, the history of compilers and parsers for computer languages does not include the ability to work with math expressions in their native form.  We have to translate them into code first.

So when you see y = x^2 + 3x + 7, you have to do some basic symbolic expansions.   x^2 is any of x*x, sq(x), or pow(x,2).  3x has to be expanded to 3*x.

Similarly, with fractions you often have to be careful about using parentheses to group the terms in the numerator and denominator so that you get what you want.  For example, if you encounter:

x + y
------
2 * PI

and you translate it as "x + y / 2 * PI" you won't get what you expect.  The * and / signs have "higher precedence" than + and -, and operators with equivalent precedence evaluate left-to-right.  So the computer will see that as x + (y/2) * PI.  To correctly translate that fraction, you need "(x+y) / (2*PI)".

Thankfully, the rules for translating math expressions into code follow very naturally from the order-of-operations rules you learned back in high school algebra.  With a little practice, you'll get the knack of it!
Page Index Toggle Pages: 1