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 › Display depth question
Page Index Toggle Pages: 1
Display depth question (Read 923 times)
Display depth question
Sep 19th, 2008, 2:07am
 
Is it possible to change and/or set a 2D display depth?  I am using a for loop to plot ellipses that overlap, but I want to randomize their apparent depths. Any thoughts?

Thanks,
E
Re: Display depth question
Reply #1 - Sep 19th, 2008, 8:28pm
 
Say you have ten levels of depth. You can create 10 PGraphics instances and draw on them depending on the wanted depth. At draw() time, you can combine them on display in the needed order. That's actually a simulation of layers seen in graphics programs like Photoshop or Gimp.
Re: Display depth question
Reply #2 - Sep 19th, 2008, 10:21pm
 
Very nice. Thanks. How about on a larger scale? Lets say I want to create 100,000 ellipses? This approach probably wouldn't be as feasible. Am I SOL?
Re: Display depth question
Reply #3 - Sep 20th, 2008, 7:08pm
 
Not sure if you are "SOL", whatever that mean.
Another approach is at object level. You can create a simple class to wrap the ellipse object: you store there the coordinates, dimensions, colors, etc.
Then at creation time, you put the created ellipse in a list, for example ArrayList (perhaps others will have a better idea of Collection), since you can insert an element in the middle, depending on its level.

I will try and show an implementation, so see if my idea is viable.
Re: Display depth question
Reply #4 - Sep 20th, 2008, 7:29pm
 
Here is an implementation:
Code:
class Ellipse implements Comparable
{
int x, y, w, h;
color fc, sc;
int dl; // 0 to 10

Ellipse(int x, int y, int w, int h, int dl)
{
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.dl = dl;
// Can add a setter for fc, sc, and/or put them in constructor,
// or leave direct access (not a very good idea, OK for quick test
fc = color(255 * dl / maxColorNb);
sc = color(255 * (maxColorNb - dl) / maxColorNb);
}

public int compareTo(Object o)
{
if (!(o instanceof Ellipse))
throw new ClassCastException();
int odl = ((Ellipse) o).dl;
if (odl < dl)
return 1;
if (odl > dl)
return -1;
return 0;
}

public String toString()
{
return "Ellipse[" + x + ", " + y + ", " + w + ", " + h + ", " + dl + "]";
}

public void draw()
{
fill(fc);
stroke(sc);
ellipse(x, y, w, h);
}
}

ArrayList al = new ArrayList();
int mn = 50, mx = 200;
int maxColorNb = 20;

void setup()
{
size(800, 800);
for (int i = 0; i < 100; i++)
{
Ellipse e = new Ellipse(
int(random(mx, width - mx)),
int(random(mx, height - mx)),
int(random(mn, mx)),
int(random(mn, mx)),
int(random(0, maxColorNb))
);
al.add(e);
}
Collections.sort(al);
println(al.size());
}

void draw()
{
background(128, 200, 255);
Iterator iter = al.iterator();
while (iter.hasNext())
{
Ellipse e = (Ellipse) iter.next();
e.draw();
}
}

Another approach, if you need to add ellipses in real time, is to make a class extending ArrayList, with an add method inserting the object in its right place, thus avoiding to re-sort each time.
Re: Display depth question
Reply #5 - Sep 21st, 2008, 8:05pm
 
wow, thanks for your help. I'll give this a shot and let you know how I make out.

SOL - s#!+ outta luck
Re: Display depth question
Reply #6 - Sep 21st, 2008, 9:05pm
 
Nice! This works great! You've been a huge help. Thank you. Since I am fairly new to Processing there are a couple lines of code I have questions about.

Collections.sort(al);
- Obviously, this sorts the array but what is the syntax 'Collections' in reference to?

Also, I'm not sure what role the toString() function plays. Is this for debugging purposes?

Again, thanks for your help.
Re: Display depth question
Reply #7 - Sep 23rd, 2008, 12:39am
 
Collections is a class made of static methods (ie. a polite way of saying it is a set of utility functions!) useful on a Collection (List, Map, Set, etc.) class. An equally useful class is Arrays.

And the toString() is indeed useful for debugging, I didn't used it. With this method, you can do println(ellipseInstance); for example.
Page Index Toggle Pages: 1