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 › Variables problem
Page Index Toggle Pages: 1
Variables problem (Read 1348 times)
Variables problem
May 27th, 2010, 2:11pm
 
Hi, im currently working though the Processing book and have just tried to use variables so that I can change a shape in proportion to changing the canvas size. I can get this technique to work for the ellipse (as in the example) and also for a rectangle. However I just cant get my triangle to work in the same way.

I write out my code and the 6 parameters for the triangle, I run this to check that my triangle is positioned where I want it and that its the correct size. I then go through changing the parameters to width*,height* (code numbers shown below) etc.  however when I run the script I dont get a good result, the triangle has moved or changed shape.

I think my main problem is that I can understand how to keep the same co ordinates for the shape whilst using the width* and height* commands. If I kept the numbers that I initially used to create the triangle (when I was checking to see that it was how I wanted it) then the * would just scale it up too big.

I start with this:

size(400,400);
 stroke(255);
 ellipse(width*0.5,height*0.5,width*0.5,height*0.5);
 triangle(200,20,100,100,300,100);
 
then because im happy that the triangle appears as i want it I try changing the parameters using width*, height* etc however I cant get the shape to stay in the same position and also, it changes shape. I dont know how to work out the numbers that I need to use to keep it the same and so that it scales when I change the size() values.
 

does this make sense?

is there an easier way to scale everything up within the scene?

hope you can help out,
thanks
G
Re: Variables problem
Reply #1 - May 27th, 2010, 2:32pm
 
Well...  There is scale(); however this shouldn't be too hard to figure out: just some fairly basic math...

Code:
void setup() {
size(200,200);
stroke(255);
ellipse(width*0.5,height*0.5,width*0.5,height*0.5);
triangle(width*0.5,height*0.1,width*0.25,height*0.25,width*0.75,height*0.25);
}
Re: Variables problem
Reply #2 - May 28th, 2010, 12:27am
 
Thanks thats great, im almost embarrassed to ask, since its 'basic' math but how did you figure out which numbers to use?

how does triangle(200,20,100,100,300,100); translate into the following?

triangle(width*0.5,height*0.1,width*0.25,height*0.25,width*0.75,height*0.25);
}

Hope you dont mind my asking and I look forward to hearing from you,
G
Re: Variables problem
Reply #3 - May 28th, 2010, 12:52am
 
hmm, i see the pattern I think,
width*0.5 is equal to 200 which is half or 50% of the size (400)
height*0.1 is equal to 20....why?
width*0.25 is equal to 1 fourth of 100, but 100 is 1 fourth or 25% of 400 (size) right?
height*0.25 is equal to 1 fourth of 100, but 100 is 1 fourth or 25% of 400 (size) right?
width*75 is equal to 3 fourths of 400 which is 300 correct?
height*0.25 width*0.25 is equal to 1 fourth of 100, but 100 is 1 fourth or 25% of 400 (size) right?

Did I get that or have I missed the point?
G
Re: Variables problem
Reply #4 - May 28th, 2010, 2:10am
 
Grin
After saying it was basic maths I managed to make a mistake:

Quote:
height*0.1 is equal to 20....why?


Sorry - that should have been height * 0.05...  But it didn't really matter so much as that's the y position of the top centre point.  It's simply a matter of dividing your original dimensions by the width/height (as applicable) to calculate the relevant factor...  e.g.
20/400=0.05
200/400=0.5
300/400=0.75
etc...
Re: Variables problem
Reply #5 - May 28th, 2010, 2:49am
 
Yes, thanks for your help i understand it now  Smiley
Page Index Toggle Pages: 1