0

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 

linecoords.append((X,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)

  • Maybe this question should be a duplicate of this one: get all points of a straight line in python – ASalazar Aug 25 '16 at 21:14
  • Consider reading about line-drawing algorithms such as Bresenham's. – Lars Viklund Aug 25 '16 at 21:55
  • Cool, thanks. In expanding the above psuedo code in to Python code i ended up with a "DDA" algorithm... I see Bresenham's faster and more accurate: I'll give that a whirl! Cheers. – Simon Aug 26 '16 at 5:26
-1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.