diff --git "a/Gato invis\303\255vel malha.png" "b/Gato invis\303\255vel malha.png" new file mode 100644 index 0000000..323a149 Binary files /dev/null and "b/Gato invis\303\255vel malha.png" differ diff --git "a/Gato invis\303\255vel.png" "b/Gato invis\303\255vel.png" new file mode 100644 index 0000000..808f10a Binary files /dev/null and "b/Gato invis\303\255vel.png" differ diff --git a/ImageGenerator.py b/ImageGenerator.py index e61c717..ffa0341 100644 --- a/ImageGenerator.py +++ b/ImageGenerator.py @@ -1,42 +1,80 @@ from PIL import Image, ImageColor +import math def gerar_imagem(cenario, largura, altura): im = Image.new('RGB', (largura, altura)) # create the Image of size 1 pixel - color = ImageColor.getrgb('#634525') + colors = [ImageColor.getrgb('#8F6332'), ImageColor.getrgb('#79C0DB')] - for objeto in cenario.cena: + for i in range(len(cenario.cena)): + objeto = cenario.cena[i] + color = colors[i] for face in objeto.faces: p1_idx, p2_idx, p3_idx = face p1 = objeto.vertices[p1_idx - 1] p2 = objeto.vertices[p2_idx - 1] p3 = objeto.vertices[p3_idx - 1] + rasteriza_face(im, p1, p2, p3, color) - inc_line(im, int(p1[0]), int(p1[1]), int(p2[0]), int(p2[1]), color) - inc_line(im, int(p2[0]), int(p2[1]), int(p3[0]), int(p3[1]), color) - inc_line(im, int(p1[0]), int(p1[1]), int(p3[0]), int(p3[1]), color) + im.save('Gato invisível malha.png') - im.save('teste.png') # or any image format +def rasteriza_face(im, p1, p2, p3, color): + lista_x = [p1[0], p2[0], p3[0]] + min_x = int(min(lista_x)) -def inc_line(image, x1, y1, x2, y2, color): + if min_x < 0: + min_x = 0 + + max_x = int(max(lista_x)) + + """0 pro maior, 1 pro menor""" + lista_y = [[0] * (max_x + 1 - min_x), [math.inf] * (max_x + 1 - min_x)] + + inc_line(im, int(p1[0]), int(p1[1]), int(p2[0]), int(p2[1]), color, lista_y, min_x) + inc_line(im, int(p2[0]), int(p2[1]), int(p3[0]), int(p3[1]), color, lista_y, min_x) + inc_line(im, int(p3[0]), int(p3[1]), int(p1[0]), int(p1[1]), color, lista_y, min_x) + + for i in range(max_x + 1 - min_x): + draw_line(im, min_x + i, lista_y[0][i], lista_y[1][i], color) + + +def inc_line(image, x1, y1, x2, y2, color, lista_y, min_x): dx = x2 - x1 dy = y2 - y1 - d = 2 * dy - dx - inc_e = 2 * dy - inc_ne = 2 * (dy - dx) + + fator = max(abs(dx), abs(dy)) + if fator == 0: + if y1 > lista_y[0][int(x1) - min_x]: + lista_y[0][int(x1) - min_x] = int(y1) + if y1 < lista_y[1][int(x1) - min_x]: + lista_y[1][int(x1) - min_x] = int(y1) + image.putpixel((int(x1), int(y1)), color) + return + + inc_x = dx / fator + inc_y = dy / fator + x = x1 y = y1 - if x > 0 and y > 0: - image.putpixel((x, y), color) - while x < x2: - if d <= 0: - d = d + inc_e - x = x + 1 - else: - d = d + inc_ne - x = x + 1 - y = y + 1 - - if x > 0 and y > 0: - image.putpixel((x, y), color) + + while round(x) != x2 or round(y) != y2: + if int(x) >= 0 and int(y) >= 0: + if y > lista_y[0][int(x) - min_x]: + lista_y[0][int(x) - min_x] = int(y) + if y < lista_y[1][int(x) - min_x]: + lista_y[1][int(x) - min_x] = int(y) + image.putpixel((int(x), int(y)), color) + x = x + inc_x + y = y + inc_y + + +def draw_line(image, x, y1, y2, color): + for i in range(int(abs(y1 - y2)) + 1): + image.putpixel((int(x), int(y2 + i)), color) + + +if __name__ == '__main__': + im = Image.new('RGB', (91, 91)) + rasteriza_face(im, [7, 6], [13, 11], [85, 76], ImageColor.getrgb('#8F6332')) + im.save('teste.png') diff --git a/main.py b/main.py index 0df409a..de59f90 100644 --- a/main.py +++ b/main.py @@ -13,7 +13,7 @@ cenario = Cenario() cenario.cena = [mesa, vaso] -cenario.setCamera(e=[21, 10, 15], g=[21, 4.625, 3.85], t=[0, 1, 0]) +cenario.setCamera(e=[21, 12, 15], g=[21, 4.625, 3.85], t=[0, 1, 0]) cenario.setProjecao(fov=60, ratio=1, z_near=0, z_far=15) cenario.translacao(1, 1, 1) cenario.escala(500, 500)