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 & HelpPrograms › best way to draw lines with alphas
Page Index Toggle Pages: 1
best way to draw lines with alphas? (Read 1249 times)
best way to draw lines with alphas?
May 11th, 2005, 5:34pm
 
i'd like to update a little app I wrote in eclipse using the 0069 processing libraries to perform better - perhaps using the latest libraries instead.

In my app I need to draw lines between various points and because I want the opacity of the line to vary and for the edges to be nicely anti aliased I have been drawing my lines a dot at a time using a 3x3 image until the line extends fully from point to point. the method i'm currently using is this:

Code:
// numsegs is set to the number of
// dots needed to span the gap
// between the two points
int numsegs = some_integer;
int x_step = x_gap / numsegs;
int y_step = y_gap / numsegs;

int a_alpha = some_integer;
int b_alpha = some_integer;
int c_alpha = some_integer;

int [] alphaArray = {c,b,c,b,a,b,c,b,c};

//mypennib is a black 3x3 BImage;
mypennib.alpa(alphaArray);

for(int m=0;m<=numsegs;m++) {
   // dotx and doty are calculated
   // at each iteration as :
   // location_of_point1+(m*(x or y step))
   image(mypennib,dotx,doty);
}

--

I am wondering if this is the best(fastest) way of drawing variable opacity, variable width, anti-aliased lines or if I would be better off using a different method?

any advice much appreciated.

maybe it would be usefull for there to be a code optimization section on this board??

james




Re: best way to draw lines with alphas?
Reply #1 - May 11th, 2005, 7:37pm
 
rather than it being a code optimization board, that's generally the sort of thing that the "programs" section is for, meaning that you have a question about a particular program you're working on, rather than something having to do with a specific function.

the fastest way to implement this would be to

1) use P3D, it'll be faster, and P2D and JAVA2D don't support gradients in alpha or color
2) draw a quad shape for the line, so that it can change its thickness along the way

something like this:

(not tested but adapted from the old BGraphics, should be close if not working)

Code:

void thickFlatLine(float ox1, float oy1,
float r1, float g1, float b1, float a1,
float ox2, float oy2,
float r2, float g2, float b2, float a2,
float thickness) {
float dX = ox2-ox1 + EPSILON;
float dY = oy2-oy1 + EPSILON;
float len = sqrt(dX*dX + dY*dY);

float rh = thickness / len;

float dx0 = rh * dY;
float dy0 = rh * dX;
float dx1 = rh * dY;
float dy1 = rh * dX;

beginShape(QUADS);

fill(r1, g1, b1, a1);
vertex(ox1+dx0, oy1-dy0);
vertex(ox1-dx0, oy1+dy0);

fill(r2, g2, b2, a2);
vertex(ox2-dx1, oy2+dy1);
vertex(ox2+dx1, oy2-dy1);

endShape();
}
Re: best way to draw lines with alphas?
Reply #2 - May 12th, 2005, 11:23am
 
thanks ben,

I tried your code, had to add noStroke(), but it works very well, the line is variable opacity as I wanted.

Surprisingly however, despite the fact that it draws the line between each pair of points in one go rather than with a series of dot images as i was doing before, it doesn't seem to be any faster. Perhaps a little slower even.

I guess i am pushing the limit of java performance asking for so many lines to be drawn in each frame. maybe it will render faster using the openGL renderer??

anyway, many thanks for yor help!!

james
Page Index Toggle Pages: 1