views:

68

answers:

0

I used the pseudocode from wikipedia for wu's antialiasing algorithm, but when i run this code with draw function. i can draw a point when i click on next point everything disappears. It seems there is problem with color issue. Can you please tell what is going on, also this homework requires me to do manual antialiasing without using open gl commands (except for drawing the point).

Note: self.tempList will be populated with x, y when user clicks on drawing window. So it is a list of [x1,y1,x2,y2......]

def  ipart(self,x):
   return x - math.trunc(x)

def round(self,x):
    return self.ipart(x + 0.5)

def fpart(self,x):
    return x - math.floor(x)

def rfpart(self,x):
    return 1 - self.fpart(x)


def renderLinePoint(self):

    if len(self.tempList) == 2:
        glPointSize(2)
        glBegin(GL_POINTS)  # Note, a point
        glColor3f(0,0,0)
        glVertex2i(self.tempList[0], self.tempList[1])
        glEnd()
        glPointSize(1)
    else:
         global k
         k = 0
         while k < len(self.tempList):
            if k < len(self.tempList): 
               x1 = self.tempList[k]
               k = k + 1
            else:
                 break

            if k < len(self.tempList): 
               y1 = self.tempList[k]
               k = k + 1
            else:
                 break

            if k < len(self.tempList): 
               x2 = self.tempList[k]
               k = k + 1
            else:
                 break

            if k < len(self.tempList): 
               y2 = self.tempList[k]
            else:
                 break


            dx = x2 - x1
            dy = y2 - y1
            if abs(dx) < abs(dy):
                temp = y1 #swap x1, y1
                y1 = x1
                x1 = temp
                temp2 = y2  #swap x2, y2
                y2 = x2
                x2 = temp2
                temp3 = dy #swap dx, dy
                dy = dx
                dx = temp3

            if x2 < x1:
                temp4 = x2 #swap x1, x2
                x2 = x1
                x1 = temp4
                temp5 = y2 #swap y1, y2
                y2 = y1
                y1 = temp5

            gradient = dy / dx

            # handle first endpoint
            xend = round(x1)
            yend = y1 + gradient * (xend - x1)
            xgap = self.rfpart(x1 + 0.5)
            xpxl1 = xend  # this will be used in the main loop
            ypxl1 = self.ipart(yend)
            glPointSize(2)
            glBegin(GL_POINTS)  # Note, a point
            glColor3f(0 + self.rfpart(yend) * xgap, 0 + self.rfpart(yend) * xgap, 0 + self.rfpart(yend) * xgap)
            glVertex2i(int(xpxl1), int(ypxl1))
            glEnd()
            glPointSize(1)
            #plot(xpxl1, ypxl1, rfpart(yend) * xgap)
            glPointSize(2)
            glBegin(GL_POINTS)  # Note, a point
            glColor3f(0 + self.fpart(yend) * xgap, 0 + self.fpart(yend) * xgap, 0 + self.fpart(yend) * xgap)
            glVertex2i(int(xpxl1), int(ypxl1 + 1))
            glEnd()
            glPointSize(1)
            #plot(xpxl1, ypxl1 + 1, fpart(yend) * xgap)
            intery = yend + gradient # first y-intersection for the main loop

            # handle second endpoint
            xend = round (x2)
            yend = y2 + gradient * (xend - x2)
            xgap = self.fpart(x2 + 0.5)
            xpxl2 = xend  # this will be used in the main loop
            ypxl2 = self.ipart(yend)
            glPointSize(2)
            glBegin(GL_POINTS)  # Note, a point
            glColor3f(0 + self.rfpart(yend) * xgap, 0 + self.rfpart(yend) * xgap, 0 + self.rfpart(yend) * xgap)
            glVertex2i(int(xpxl2), int(ypxl2))
            glEnd()
            glPointSize(1)
            #plot (xpxl2, ypxl2, rfpart (yend) * xgap)
            glPointSize(2)
            glBegin(GL_POINTS)  # Note, a point
            glColor3f(0 + self.fpart(yend) * xgap, 0 + self.fpart(yend) * xgap, 0 + self.fpart(yend) * xgap)
            glVertex2i(int(xpxl2), int(ypxl2 + 1))
            glEnd()
            glPointSize(1)
            #plot (xpxl2, ypxl2 + 1, fpart (yend) * xgap)

            # main loop
            x = int(xpxl1 + 1)
            for x in range(int(xpxl2 - 1)):
                #print intery
                glPointSize(2)
                glBegin(GL_POINTS)  # Note, a point
                glColor3f(0 + self.rfpart(intery), 0 + self.rfpart(intery), 0 + self.rfpart(intery))
                glVertex2i(int(x), int(self.ipart(intery)))
                glEnd()
                glPointSize(1)
                #plot (x, ipart (intery), rfpart(intery))
                glPointSize(2)
                glBegin(GL_POINTS)  # Note, a point
                glColor3f(0 + self.fpart(intery), 0 + self.fpart(intery), 0 + self.fpart(intery))
                glVertex2i(int(x), int(self.ipart(intery) + 1))
                glEnd()
                glPointSize(1)
                #plot (x, ipart (intery) + 1, fpart (intery))
                intery = intery + gradient


            if k != len(self.tempList)-1:
                k = k - 1
            else:
                 break