# Re: pixel coordinates of a line

```

Marc Schellens wrote:
>
> Oh, I forgot:
> I have the start and endpoint, but they might need to be clipped
> to the images boders...
>
> > I want to extract pixels along a line.
> > I know start and endpoint (ie. I draw the line in top of the
> > image).
> > How to get the pixel values? Do I have to do it 'by hand'?
> >
> > thanks,
> > marc

Working in pixel coordinates you have the endpoints (sx,sy,ex,ey) and the clipping
rectangle (x0,y0, x1, y1), so you should trivially be able to get the equation
of the line

y = m x + b    [ m= (ey-sy)/(ex-sx), b = sy - m sx ]

Turn this into IDL...

x = lindgen(x1-x0) + x0
y = floor (m * x + b + 0.5)    [add 0.5 to get appropriate rounding]
w = where (y ge y0 and y le y1)
if (w[0] ne -1) then begin
x = x(w)
y = y(w)
endif else begin
.... line is outside clipping rectangle
endelse

Here I'm assuming integral values for x0,x1, y0,y1 but not necessarily the
end points.

There are probably subtleties I've missed but  this seems like a reasonable
start.  Whether it will exactly match the pixels that IDL will draw on
is hard to say...  Guess we'd need to know exactly how IDL draws a line.

Tom McGlynn
NASA/GSFC
tam@lheapop.gsfc.nasa.gov
[Mailed and posted]
```