Documente Academic
Documente Profesional
Documente Cultură
Disciplina:Graficapecalculator
Raport
lalucrareadelaboratornr2
Tema : Transformri geometrice a imaginilor
Chiinu 2016
Scopul lucrrii: Obinerea cunotinelor practice n realizarea transformrilor geometrice 2D i 3D
a imaginilor
Sarcina lucrrii:
Varianta 19.
Lista de transformri:
Codul-surs al programului 1:
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <time.h>
#include <conio.h>
#include <graphics.h>
Pag. 1
int temp1,temp2;
temp1=pointList[2*pos1];
temp2=pointList[2*pos1+1];
pointList[2*pos1]=pointList[2*pos2];
pointList[2*pos1+1]=pointList[2*pos2+1];
pointList[2*pos2]=temp1;
pointList[2*pos2+1]=temp2;
}
int main() {
int gd=DETECT, gm, pivotX=60,
pivotY=45,colorR,colorS,tempX,tempY,tempPoint[2];
int
resultRotation[10],rectangleX[]={36,56},rectangleY[]={240,270},errorcode,counter
;
int resultScaling[10],resultTranslation[10];
int triangle[]={10,20,30,180,340,90,10,20};
time_t t;
initgraph(&gd, &gm, "C:\\TC\\BGI");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
Pag. 2
//1.25 coefficient scaling
scale(tempPoint,1.25);
resultScaling[counter]=tempPoint[0];
resultScaling[counter+1]=tempPoint[1];
//Translation X=240, Y=32
translate(tempPoint,240,32);
resultTranslation[counter]=tempPoint[0];
resultTranslation[counter+1]=tempPoint[1];
counter+=2;
}
}
resultRotation[8]=resultRotation[0];
resultRotation[9]=resultRotation[1];
resultScaling[8]=resultScaling[0];
resultScaling[9]=resultScaling[1];
resultTranslation[8]=resultTranslation[0];
resultTranslation[9]=resultTranslation[1];
//printPointList(result,10,200,360);
swapPoints(resultRotation,1,2);
swapPoints(resultScaling,1,2);
swapPoints(resultTranslation,1,2);
//printPointList(result,10,200,400);
swapPoints(resultRotation,2,3);
swapPoints(resultScaling,2,3);
swapPoints(resultTranslation,2,3);
//printPointList(result,10,200,460);
setcolor(colorR);
drawpoly(5,resultRotation);
outtextxy(resultRotation[0],resultRotation[1]+20,"Rotatia");
drawpoly(5,resultScaling);
outtextxy(resultScaling[0],resultScaling[1]+20,"Rotatia + Scalarea");
drawpoly(5,resultTranslation);
outtextxy(resultTranslation[0],resultTranslation[1]+20,"Rotatia + Scalarea
+ Translarea");
getch();
closegraph();
restorecrtmode();
return 0;
}
Codul-surs al programului 2:
Programul dat a fost elaborat folosing limbajul de programare Python, prin utilizarea librriei
grafice pygame. Pygame utilizeaz OpenGL pentru a desena obiectele pe ecran.
#!/usr/bin/python
import pygame
import sys
from math import pi,sin,cos
from operator import itemgetter
def degreeToRadian(degrees):
return degrees*pi/180
class point3D:
def __init__(self, x=0, y=0, z=0):
self.x,self.y,self.z = float(x),float(y),float(z)
Pag. 3
"""Rotates the point around the OX axis by the given
angle in degrees """
angleRad=degreeToRadian(angle)
y=self.y*cos(angleRad)-self.z*sin(angleRad)
z=self.y*sin(angleRad)+self.z*cos(angleRad)
return point3D(self.x,y,z)
pygame.init()
win_width,win_height=640,480
screen = pygame.display.set_mode((win_width, win_height))
pygame.display.set_caption("Simularea rotirii unui cub pe trei axe")
# initialize font; must be called after 'pygame.init()' to avoid 'Font not
Initialized' error
myfont = pygame.font.SysFont("monospace", 15)
clock = pygame.time.Clock()
vertices = [
point3D(-1,1,-1),
point3D(1,1,-1),
point3D(1,-1,-1),
point3D(-1,-1,-1),
point3D(-1,1,1),
point3D(1,1,1),
point3D(1,-1,1),
point3D(-1,-1,1)
]
# Define the vertices that compose each of the 6 faces. These numbers are
# indices to the vertices list defined above.
faces = [(0,1,2,3),(1,5,6,2),(5,4,7,6),(4,0,3,7),(0,4,5,1),(3,2,6,7)]
angle = 0
xRot='X'
yRot='Y'
zRot='Z'
Pag. 4
sRot='S'
rotation=xRot
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_s:
rotation=sRot
angle=0
elif event.key == pygame.K_x:
rotation=xRot
angle=0
elif event.key == pygame.K_y:
rotation=yRot
angle=0
elif event.key == pygame.K_z:
rotation=zRot
angle=0
clock.tick(50)
screen.fill((0,0,0))
label = myfont.render("Rotatia: {0}".format(rotation), 1, (255,255,0))
screen.blit(label, (50, 50))
for v in vertices:
if (rotation==xRot):
r = v.rotateOX(angle)
elif (rotation==yRot):
r = v.rotateOY(angle)
elif (rotation==zRot):
r = v.rotateOZ(angle)
elif (rotation==sRot):
# Rotate the point around X axis, then around Y axis, and finally
around Z axis.
r = v.rotateOX(angle).rotateOY(angle).rotateOZ(angle)
# Transform the point from 3D to 2D
p = r.project(screen.get_width(), screen.get_height(), 256, 4)
# Put the point in the list of transformed vertices
t.append(p)
Pag. 5
(t[f[2]].x, t[f[2]].y), (t[f[3]].x, t[f[3]].y),
(t[f[3]].x, t[f[3]].y), (t[f[0]].x, t[f[0]].y)]
pygame.draw.polygon(screen,colors[face_index],pointlist)
angle += 1
pygame.display.flip()
Rezultatele:
1. Captura de ecran a figurilor sintezate:
Pag. 6
2. Captura de ecran a cubului n rotirea concomitent pe axele X, Y i Z
Concluzii: Manipularea per pixel a imaginilor presupune o cantitate mai mare de resurse de
calculare. La transformarea unei imagini colorate n interior timpul de calculare a noii imagini este
destul de mare.
Pag. 7