Re: How to get pixel values along a line?





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


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