-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Finalização da coloração das figuras
- Loading branch information
Vinícius Aiala
committed
Jun 15, 2021
1 parent
1b64d31
commit 35a2d25
Showing
4 changed files
with
62 additions
and
24 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters