Example #1
0
def draw_line(input_pixels, input_size, value, p1, p2):
    input_width = input_size[0]
    input_height = input_size[1]

    tmp = BresenhamLineGenerator([p1[0], p1[1]], [p2[0], p2[1]])
    while not tmp.done:
        point = tmp.get_next_point()
        col = point[0]
        row = point[1]
        input_pixels[row * input_width + col] = value
def draw_convex_polygon(vertices, input_pixels, input_size, value):
    vertices = sorted(vertices, key=lambda x:x[0])
    vertices = sorted(vertices, key=lambda x:x[1])
    input_height = input_size[1]
    input_width = input_size[0]

    s1 = 0
    d1 = 1

    s2 = 0
    d2 = len(vertices)-1

    line_1 = BresenhamLineGenerator(vertices[s1], vertices[d1])
    line_2 = BresenhamLineGenerator(vertices[s2], vertices[d2])

    point_1 = vertices[0]
    point_2 = vertices[0]

    new_point_1 = point_1
    new_point_2 = point_2

    done = False
    
    while not done:
        while True:
            new_point_1 = line_1.get_next_point()

            if new_point_1 == None:
                #print "line 1 consumed with s1:"+str(s1)+" d1:"+str(d1)
                if d1 == d2:
                    done = True
                    break;
                else:
                    s1 = d1
                    d1 += 1
                    line_1 = BresenhamLineGenerator(vertices[s1], vertices[d1])
                    continue

            input_pixels[new_point_1[1]*input_width+new_point_1[0]] = value

            if new_point_1[1] == point_1[1]:
                prev_point_1 = new_point_1             
                continue
            point_1 = prev_point_1
            prev_point_1 = new_point_1
            break

        while True:
            new_point_2 = line_2.get_next_point()

            if new_point_2 == None:
                #print "line 2 consumed with s2:"+str(s2)+" d2:"+str(d2)
                if d1 == d2:
                    done = True
                    break;
                else:
                    s2 = d2
                    d2 -= 1
                    line_2 = BresenhamLineGenerator(vertices[s2], vertices[d2])
                    continue

            input_pixels[new_point_2[1]*input_width+new_point_2[0]] = value


            if new_point_2[1] == point_2[1]:
                prev_point_2 = new_point_2
                continue

            point_2 = prev_point_2
            prev_point_2 = new_point_2
            break

        y = point_1[1]
        x1 = point_1[0]
        x2 = point_2[0]
        if x1 > x2:
            tmp = x2
            x2 = x1
            x1 = tmp

        #print "scanline y:"+str(y)+" from "+str(x1)+" to "+str(x2)

        for i in range(x1+1, x2):
            input_pixels[y*input_width+i] = value


        point_1 = new_point_1
        point_2 = new_point_2