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

Re: [pygame] Re: Keeping a sprite on screen with rect.clamp



Well the Bresenham's algorithm is not more eficient than the usual diferencial method in today's computers (I wrote both in python for a test of pyopengl and consume much more time in drawing 5000 lines bresenham than diferential, yes, I know, it shouldn't ocurr, and I think that it should be faster than beziers looking at its amount of multiplication an divisions).

I reach to this conclusion: (I asumme that I can be wrong, and if I am I would like to know the truth)

Difference between diferential and bresenham methods came from that diferential use floats and bresenham ints but bresenham does more comparations so, on today's computers (with procesors of, at least, 32 bits) the float calculation is much more efficient than when bresenham wrotes it's line algorithm

Diferential algorithm its, also, much more understable. I will paste both I did


def lineDDA(xo, yo, xf, yf):
    dx = xf - xo
    dy = yf - yo
    Ax = 0.0 # Increments of x and y
    Ay = 0.0
    x = xo
    y = yo
    steps = max(abs(dx), abs(dy))
    steps = max(steps,1) # for te case of initial = final
    Ax = float(dx) / steps
    Ay = float(dy) / steps
    glBegin(GL_POINTS) # Start using openGL
    glColor3f(0.0,1.0,0.0) # set default color to green
    glVertex2f(x, y) # draw a point at xo, yo
    for k in range (0, steps):
        x += Ax # calculate next point (it a float)
        y += Ay
        glVertex2f(x, y)# draw the point (rounded to pixels)
    glEnd()





def lineBresenham(x0, y0, x1, y1):
    if abs(y1 - y0) > abs(x1 - x0):
        steep = True
    else:
        steep = False

    if steep:
        x0, y0 = y0, x0
        x1, y1 = y1, x1

    # Must go from left to rigth
    if x0 > x1:
        x0, x1 = x1, x0
        y0, y1 = y1, y0

    deltax = x1 - x0
    deltay = abs(y1 - y0) * 2
    error = deltax
    DobleDeltax = deltax * 2
    y = y0
    if y0 < y1:
        ystep = 1
    else:
        ystep = -1

    glBegin(GL_POINTS)
    glColor3f(1.0,0.0,0.0)

    for x in range(x0,x1+1):
        # Si hemos cambiado x por y debemos deshacer el cambio a la hora de pintar
        if steep:
            glVertex2f(y, x) # I know, it draws a point from 2 floats, but using glVertex2i there weren't diferences in time.
        else:
            glVertex2f(x, y)
        error = error - deltay
        if error < 0:
            y = y + ystep
            error = error + DobleDeltax

    glEnd()