Re: How to get pixel values along a line?



Somehow this thread slipped off the list...

Thank you Matt! Very useful!

I've been given great advice on my question, thank you all.

/Fredrik

On Wed, 2007-04-11 at 08:49 -0500, Matt Fischer wrote:
> 
> 
> On 4/11/07, Fredrik Persson <fredrik p persson gmail com> wrote:
>         
>         
>         On 4/11/07, Matt Fischer <mattfischer84 gmail com> wrote:
>                 
>                 
>                 On 4/10/07, Andrew Sobala <aes gnome org> wrote: 
>                         That's a simple maths question, not GTK. If
>                         the user picks (x1,y1) and
>                         (x2,y2), then the points along that line
>                         conform to y = (y2 - y1)/(x2 - 
>                         x1) * (x - x1) + y1. (I think!)
>                         
>                         So plug in values for x from x1 to x2, and
>                         pull out y (with appropriate
>                         whole-number rounding.)
>                  
>                 One more wrinkle...you'll probably want to see whether
>                 your line is x-major or y-major first, that is, in
>                 which dimension it is the longest, and iterate across
>                 that dimension.  Otherwise you run the risk of
>                 skipping pixels (for an exteme example, imagine
>                 performing the above technique on a vertical line.)
>                 So, if your line is longer in X than in Y, then use
>                 the equation as listed above, and if it's longer in Y
>                 than it is in X, then swap around all the X's and Y's
>                 and iterate along Y instead.  Once you have the pixel
>                 coordinates, you should be able to pull individual
>                 pixel colors out of a GdkPixbuf as Michael suggests.
>                  
>                 --Matt
>         
>         Yes, this sounds clever, thank you.
>         
>         Do you have any idea about what to do with the fractions of a
>         pixel? For example, the line x1,y2 = 2,1 and x2,y2 =10,6 will,
>         if we iterate over x, take on y values such as 1.625. Is it
>         wise, in that case, to round that off to 2, or to have a
>         combination of pixels y = 1 and y = 2 in accordance with the
>         fraction 0.625? (I hope you understand what I mean.)
>         
>         /Fredrik
>         
>         
> Yeah, that's the next problem you'll run into.  If you want it to
> really look nice, you'll want to do some filtering on it, somewhat as
> you suggest.  However, to do that correctly, you'll probably want to
> filter it in both directions, i.e., if you want to scan along a line
> that has a net length of 10, but you want to draw the resulting graph
> as 20 pixels wide, you'll want it to be smooth.  What I would probably
> do is do it parametrically:
> 
> Assuming you want your resulting line G to essentially be an array of
> g_max pixel values 
> Iterate g from 0 to g_max
>   Let t = g / g_max
>   x = x1 + (x2 - x1) * t
>   y = y1 + (y2 - y1) * t
>   xa = round_down(x)
>   xb = round_up(x)
>   ya = round_down(y)
>   yb = round_up(y)
>   Get all four pixels surrounding the fractional point: (xa, ya), (xb,
> ya), (xa, yb), and (xb, yb) 
>   Average them together based on the fractional components of x and y
>   G[g] = result of the average
> 
> That way it will be completely blended, and look nice and smooth
> regardless of how long you want the resulting line to be in relation
> to the original image.  It also means you don't need to worry about my
> earlier post about switching around the orientation based on x- or
> y-majorness....the parametric equation is completely symmetric, so it
> handles this all by itself. 
> 
> --Matt




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]