Skip to content

Commit

Permalink
Finalização da coloração das figuras
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinícius Aiala committed Jun 15, 2021
1 parent 1b64d31 commit 35a2d25
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 24 deletions.
Binary file added Gato invisível malha.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Gato invisível.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
84 changes: 61 additions & 23 deletions ImageGenerator.py
Original file line number Diff line number Diff line change
@@ -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')
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 35a2d25

Please sign in to comment.