Sunteți pe pagina 1din 8

Ministerul Educaiei al Republicii Moldova

Universitatea Tehnic a Moldovei


Facultatea Calculatoare, Informatica, Microelectronica

Disciplina:Graficapecalculator

Raport
lalucrareadelaboratornr2
Tema : Transformri geometrice a imaginilor

A elaborat : st. gr.TI-131 F/R


Cebotari Ion

A verificat : Lector Superior


Constantin Plotnic

Chiinu 2016
Scopul lucrrii: Obinerea cunotinelor practice n realizarea transformrilor geometrice 2D i 3D
a imaginilor

Sarcina lucrrii:

1. Elaborarea programului de rotire, scalare i deplasare a imaginilor n plan i n spaiu


a) Extragei la ecran un triunghi de culoarea R
b) Extragei la ecran un ptrat de culoarea S
c) ndeplinii transformarea tuturor punctelor de culoarea S n conformitate cu variantele
(Tabela 2). Punctul ce se deplaseaz se recoloreaz n culoarea R. Pentru determinarea
culorii punctului folosii procedure GetPixel.
Rotirea s fie efectuat n jurul unui punct oarecare din spaiul coordonatelor ecran.
2. Rotirea unui obiect n jurul unei axe
a) Desenarea unui cub specificat prin coordonatele vrfurilor sale
b) Rotirea cubului n jurul axelor orizontal i vertical care trec prin centrul su, precum i
n jurul axei OZ

Varianta 19.
Lista de transformri:

1. Rotirea cu unghiul de 245


2. Scalarea cu coeficientul de 1.25
3. Deplasarea X i Y = 240 , 32

Codul-surs al programului 1:
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <time.h>
#include <conio.h>
#include <graphics.h>

void printPointList(int *pointList, int length, int posX, int posY);

void scale(int *point, float scaleFactor) {


point[0] = floor(point[0]*scaleFactor);
point[1] = floor(point[1]*scaleFactor);
// return point;
}

void rotate (int *point,float rotationAngle) {


// static int point[2];
float deg2rad=rotationAngle*M_PI/180;
point[0]=floor(point[0]*cos(deg2rad)+point[0]*sin(deg2rad));
point[1]= floor(point[1]*cos(deg2rad)-point[1]*sin(deg2rad));
// return point;
}

void translate(int *point,int translateX, int translateY){


// static int point[2];
point[0] = point[0] + translateX;
point[1] = point[1] + translateY;
// return point;
}

void swapPoints(int *pointList,int pos1, int pos2){

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;
}

void printPointList(int *pointList, int length, int posX, int posY){


int counter;
char text[20];
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
settextjustify(CENTER_TEXT,CENTER_TEXT);
for (counter=0;counter<length;counter++){
outtextxy(posX+counter*40,posY,itoa(pointList[counter],text,10));
}
}

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 */
}

srand ((unsigned) time(&t));


colorR =(rand()%15)+1;
do {colorS =(rand()%15)+1;}
while(colorR == colorS);
setcolor(colorR);
drawpoly(4,triangle);
setcolor(colorS);
rectangle(rectangleX[0],rectangleY[0],rectangleX[1],rectangleY[1]);
setcolor(RED);
line(pivotX-5,pivotY-5,pivotX+5,pivotY+5);
line(pivotX+5,pivotY-5,pivotX-5,pivotY+5);
getch();
counter=0;
for (tempX=0;tempX<2;tempX++){
for(tempY=0;tempY<2;tempY++){
tempPoint[0]=rectangleX[tempX];
tempPoint[1]=rectangleY[tempY];
//Rotation vs pivot
translate(tempPoint,-pivotX,-pivotY);
rotate(tempPoint,245);
translate(tempPoint,pivotX,pivotY);
resultRotation[counter]=tempPoint[0];
resultRotation[counter+1]=tempPoint[1];

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)

def rotateOX (self,angle):

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)

def rotateOY (self,angle):


"""Rotates the point around the OX axis by the given
angle in degrees """
angleRad=degreeToRadian(angle)
x=self.z*cos(angleRad)-self.x*sin(angleRad)
z=self.x*cos(angleRad)+self.z*sin(angleRad)
return point3D(x,self.y,z)

def rotateOZ (self,angle):


"""Rotates the point around the OX axis by the given
angle in degrees """
angleRad=degreeToRadian(angle)
x=self.x*cos(angleRad)-self.y*sin(angleRad)
y=self.x*sin(angleRad)+self.y*cos(angleRad)
return point3D(x,y,self.z)

def project (self, win_width, win_height,fov,viewer_distance):


"""Uses the orthogonal projection to show the cube on the screen"""
factor=fov/(viewer_distance+self.z)
x=self.x*factor+win_width/2
y=self.y*factor+win_width/2
return point3D(x,y,self.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)]

# Define colors for each face


colors = [(255,0,255),(255,0,0),(0,255,0),(0,0,255),(0,255,255),(255,255,0)]

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))

# It will hold transformed vertices.


t = []

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)

# Calculate the average Z values of each face.


avg_z = []
i = 0
for f in faces:
z = (t[f[0]].z + t[f[1]].z + t[f[2]].z + t[f[3]].z) / 4.0
avg_z.append([i,z])
i = i + 1

# Draw the faces using the Painter's algorithm:


# Distant faces are drawn before the closer ones.
for tmp in sorted(avg_z,key=itemgetter(1),reverse=True):
face_index = tmp[0]
f = faces[face_index]
pointlist = [(t[f[0]].x, t[f[0]].y), (t[f[1]].x, t[f[1]].y),
(t[f[1]].x, t[f[1]].y), (t[f[2]].x, t[f[2]].y),

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:

Imagine 1: Starea imaginilor de pn la


transformare

Imagine 2: Imaginile dup transformare

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