How can I create a set of pixel coordinates for a line in Python?

I’ve got some code that’s searching pixel by pixel from a starting point, at a certain angle, until it finds a pixel (a non-white pixel)

It only has to search around 300 pixels: I’m currently using polar to Cartesian function 300 times…

def pol2cart(rotdist, cwangle):
    x = rotdist * numpy.cos(cwangle)
    y = rotdist * numpy.sin(cwangle)
    return(x, y)

… but it’s very slow, up to 68 milliseconds per search (if it goes the whole 300 without finding anything) and I’ve got quite a lot of these searches to do… which is the main cause of my overall function taking about 20 seconds to run (I’d be happy with <2 seconds)

My theory is: if I can create a list of coordinates that describe a line from the centre point to the 300px distance end point, I only need to run pol2cart once (go get the end point) and then I can just iterate over my “line list”…

So I could do something like (psuedo):

If line == UppyDowny:
    for Y in line:
        Solve X 

elseIf line == LeftyRighty:
    for X in line:
        Solve Y 


… I’m not asking for someone to code that for me, but I’m wondering if there’s an existing common library function that achieves that? (as it’ll no doubt be faster than how I would code the above and also a lot faster than 600 cos/sin calls)


Digging around I found an Eye Diagram which includes a pure Python Bresenham implementation and a Cython one, HTH 🙂

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *