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 › Looking for input on my sketch
Page Index Toggle Pages: 1
Looking for input on my sketch (Read 742 times)
Looking for input on my sketch
May 29th, 2009, 3:16pm
 
Hello all,

I was wandering if I could get some advice on how to do some things for this processing sketch

Code:
int bloodsugar = 25;

float dayx;
float monthy;
float bsSize;
float bsAlpha;

void setup(){
 size (400,400);
 noLoop();
 
}


void draw(){
 dayx=map(day(),1,31,0,width);
 monthy=map(month(),1,12,0,height);
 bsSize = map(bloodsugar,1.0,25.0,10,width/4);
 bsAlpha = map(bloodsugar,1.0,25.0,0,255);
 
 
 fill(255,0,0,bsAlpha);
 
 rect(dayx,monthy,bsSize,bsSize);

}


So what i'd like to do is load in a text file and use the floats contained in it to create the squares and, depending on the amount, 1.0 being low and 25.0 being high adjust the alpha.

The rect x and y is following the day and month using system time, ideally i'd like to contain that information in the text file(i'm assuming) so it would allow for multiple rects.

I really hope i'm making sense, I've been following the the Processing Handbook which is great but the loadStrings doesn't seem to work so any advice on how to go about it and even some sample code would be great.

Thanks,

BeefyC

Re: Looking for input on my sketch
Reply #1 - May 29th, 2009, 7:13pm
 
loadStrings() should definitely work.  Try experimenting with the sample projects that come with Processing to see how it works.  Go to File > Examples > Topics > File IO.  Open up LoadFile1 and LoadFile2 for some good examples.  loadStrings() is definitely the easiest way to get a text file into your sketch.
Re: Looking for input on my sketch
Reply #2 - May 30th, 2009, 9:13am
 
Thanks for the reply Scott,

I've played around with the loadStrings and it is working well, However i have a few questions;

Currently I'm using four elements for use in a rect but I'm not sure how I can get the rect to alter it's alpha depending on the number in the Width and Height of the rect.

Code:
String[] lines;
int index = 0;

void setup(){
 size (1000,1000);
 lines = loadStrings("blood.txt");
}


void draw(){
if (index < lines.length) {
 String[] pieces = split(lines[index], '\t');
 if (pieces.length == 4) {
   int x = int(pieces[0]) * 2;
   int y = int(pieces[1]) * 2;
   int w = int(pieces[2]) * 2;
   int h = int(pieces[3]) * 2;      
   fill(255,0,0);
 rect(x,y,w,h);
 
 }
 index =index +1;

}
}



and the txt file

Code:
1	1	6	6
3 1 10 10
3 1 5 5
4 1 9 9
5 1 19 19


so in my last sketch I had mapped the values of 1.0(low) and 25.0(high) to 0 - 255 for the alpha but that was for just one variable for the "bloodsugar" result whereas this is reading alot of them.

Thanks,

BeefyC
Re: Looking for input on my sketch
Reply #3 - May 30th, 2009, 9:29am
 
Sounds like you are on the right track.


Do you want wider, taller boxes to be more opaque?  Looks like you're setting fill(255,0,0), which is red.  So the easiest thing you could do is the the alpha to w + h, or some normalized value derived from that, like you mentioned.  So you could do:

Code:

float alpha = w + h;
fill(255, 0, 0, alpha);
rect(x, y, w, h);


If you want to normalize the values, as you mentioned, but don't know what the min/max range of the incoming values are in advance, you will have to loadStrings and loop through all lines in the file once first (to determine min/max values), then a second time to draw the rects.

Does that make sense?  I'm not sure if that's what you're trying to do.
Re: Looking for input on my sketch
Reply #4 - May 31st, 2009, 7:16am
 
Thanks for the code you provided, it worked and I'm getting the results I was looking for.

However, I'm having trouble using the map function from the first sketch. Because I'm using month and days for the x and y coordinates respectively, and also defining a variable for the X,Y,W,H I'm not sure how to include the map function within it. Does that make sense?

to illustrate;

Code:
float x = float(pieces[0]) * 2;
   float y = float(pieces[1]) * 2;

the x coords are very close to the side: in my previous sketch they were mapped to the width and height but had their own variable. How would I incude the map function within that? if that is the way it should go of course.

Thanks for your help
Re: Looking for input on my sketch
Reply #5 - May 31st, 2009, 1:55pm
 
I guess I don't understand what you're trying to do.  Maybe you could post a rough image illustrating how you want it to look?
Re: Looking for input on my sketch
Reply #6 - May 31st, 2009, 2:54pm
 
Replace hxxp with http***
hxxp://img269.imageshack.us/img269/4396/sketchv.jpg

Code:
5	1	2.4	2.4
5 1 18.3 18.3
5 1 6.4 6.4
5 1 8.5 8.5
5 1 13.4 13.4
5 2 4.6 4.6
5 2 6.4 6.4
5 2 3.8 3.8
5 2 9.1 9.1
5 2 19.6 19.6
5 2 15.7 15.7
5 3 3.5 3.5
5 3 13.5 13.5
5 3 3.6 3.6
5 3 16.3 16.3
5 4 12.0 12.0
……(more)



Thats the txt file i'm loading in, obviously it's using the month(5) for the x coord....thats just 5 pixels in and it's hugging the side. Now, looking at my previous sketch I was using the system time which may have let me get the position so far along.

anyway, i'll stop confusing both of us!

main issue is to make it more aesthetically pleasing but retain the month and day data for the x and y coords.
Re: Looking for input on my sketch
Reply #7 - Jun 1st, 2009, 4:40pm
 
So it looks like all the circles are clustered in the top left corner, around 0,0.

You should be able to just use day for x and month for y.

I'm not sure what you mean when you say you want it to be more aesthetically pleasing.  I'd start by getting the circles to plot in the correct location first.
Re: Looking for input on my sketch
Reply #8 - Jun 2nd, 2009, 1:05am
 
In this section:

Code:
    int x = int(pieces[0]) * 2;
   int y = int(pieces[1]) * 2;
   int w = int(pieces[2]) * 2;
   int h = int(pieces[3]) * 2;  


replace the 'magic numbers' with variables, one for position and one for scale.  You'll then need to figure out the values required to scale your results to your table (should involve some fairly basic mathematics Wink
You may also need to add an offset to get what you want:

Code:
    int x = offsetX + int(pieces[0]) * multiplier;
   int y = offsetY + int(pieces[1]) * multiplier;
   int w = int(pieces[2]) * scaler;
   int h = int(pieces[3]) * scaler;


The multiplier determines the spread across the table - basically each increment of x/y will be placed the multiplier number of pixels to the right/down (hint: not necessarily the figure you want but I tried it with 100 - i.e. 2 is way too small) - the offset will determine where it starts from.
Page Index Toggle Pages: 1