Transformasi Grafik menggunakan bentuk translasi dengan Lima Titik || Dev++

 

Transformasi Grafik 

Translasi dengan Empat Titik 

Untuk menggambarkan translasi suatu objek berupa segi lima dengan koordinat A(20,10) B(40,10), C(50,30), D(30,50) dan E(10,30) dengan tx,ty(20,20), tentukan koordinat yang barunya ?

Jawab : 

Proses Penghitungan Translasi

Rumus yang di gunakan :

 x‟ = x + tx (x,y) = titik asal sebelum translasi 

 y"= y + ty (x‟,y‟) = titik baru hasil translasi

Titik A

x'A = xA + tx

       =20 + 20 = 40 

y'A = yA + ty

       = 10 + 20 = 30 

Titik B

x'B = xB + tx

       =40 + 20 = 60

y'B = yB + ty

       = 10 + 20 = 30

Titik C

x'C = xC + tx

       =50 + 20 =  70

y'C = yC + ty

       = 30 + 20 = 50

Titik D

x'D = xD + tx

       =30+ 20 =  50

y'D = yD + ty

       = 50 + 20 =  70

Titik E

x'E = xE + tx

       =10 + 20 = 30

y'E = yE + ty

       = 30 + 20 = 50

 

Koding :

#include <windows.h>
#include <GL/gl.h>
#include <GL/glut.h>
#include <stdio.h>
int Xa1, Xa2, Xa3, Xa4, Xa5, //nilai titik awal
 Ya1, Ya2, Ya3, Ya4, Ya5;
 
int pindah_x, pindah_y; //nilai perpindahan
int hasil_Xa1, hasil_Xa2, hasil_Xa3, hasil_Xa4, hasil_Xa5, //hasil perpindahan titik
 hasil_Ya1, hasil_Ya2, hasil_Ya3, hasil_Ya4, hasil_Ya5;
void display (void){
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
//posisi objek awal
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex3f(Xa1, Ya1, 0.0);
glVertex3f(Xa2, Ya2, 0.0);
glVertex3f(Xa3, Ya3, 0.0);
glVertex3f(Xa4, Ya4, 0.0);
glVertex3f(Xa5, Ya5, 0.0);
glEnd();

//posisi hasil Translasi
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex3f(hasil_Xa1, hasil_Ya1, 0.0);
glVertex3f(hasil_Xa2, hasil_Ya2, 0.0);
glVertex3f(hasil_Xa3, hasil_Ya3, 0.0);
glVertex3f(hasil_Xa4, hasil_Ya4, 0.0);
glVertex3f(hasil_Xa5, hasil_Ya5, 0.0);
glEnd();
 
glFlush();
}
void reshape(int lebar, int tinggi){
glViewport(0,0,(GLsizei)lebar, (GLsizei) tinggi);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D (0.0, (GLdouble)lebar, 0.0,(GLdouble)tinggi);
}

int main(int argc, char *argv[]){
 printf("\nInput Nilai Titik Koordinat Objek");
 printf("\n================================");
 printf("\n Titik Kordinat 1 ");
 printf("\n================================");
 printf("\nMasukan Nilai X Titik A1 = ");
 scanf("%i",&Xa1);
 printf("\nMasukan Nilai Y Titik A1 = ");
 scanf("%i",&Ya1);
 printf("\n================================");
 
 printf("\n================================");
 printf("\n Titik Kordinat 2 ");
 printf("\n================================");
 printf("\nMasukan Nilai X Titik A2 = ");
 scanf("%i",&Xa2);
 printf("\nMasukan Nilai Y Titik A2 = ");
 scanf("%i",&Ya2);
 printf("\n================================");

 printf("\n================================");
 printf("\n Titik Kordinat 3 ");
 printf("\n================================");
 printf("\nMasukan Nilai X Titik A3 = ");
 scanf("%i",&Xa3);
 printf("\nMasukan Nilai Y Titik A3 = ");
 scanf("%i",&Ya3);
 printf("\n================================");
 
 printf("\n================================");
 printf("\n Titik Kordinat 4 ");
 printf("\n================================");
 printf("\nMasukan Nilai X Titik A4 = ");
 scanf("%i",&Xa4);
 printf("\nMasukan Nilai Y Titik A4 = ");
 scanf("%i",&Ya4);
 printf("\n================================");
 
 printf("\n================================");
 printf("\n Titik Kordinat 5 ");
 printf("\n================================");
 printf("\nMasukan Nilai X Titik A5 = ");
 scanf("%i",&Xa5);
 printf("\nMasukan Nilai Y Titik A5 = ");
 scanf("%i",&Ya5);
 printf("\n================================");
 
 //buatkan input untuk nilai perpindahan
 printf("\n================================");
 printf("\n Titik Translasi ");
 printf("\n================================");
 printf("\nMasukan Nilai Translasi Sumbu X = ");
 scanf("%i",&pindah_x);
 printf("\nMasukan Nilai Translasi Sumbu Y = ");
 scanf("%i",&pindah_y);
 printf("\n================================");

hasil_Xa1 = Xa1 + pindah_x; //proses transalasi titik A
 hasil_Ya1 = Ya1 + pindah_y;
 
 hasil_Xa2 = Xa2 + pindah_x; //proses transalasi titik B
 hasil_Ya2 = Ya2 + pindah_y;
 
 hasil_Xa3 = Xa3 + pindah_x; //proses transalasi titik C
 hasil_Ya3 = Ya3 + pindah_y;
 
 hasil_Xa4 = Xa4 + pindah_x; //proses transalasi titik D
 hasil_Ya4 = Ya4 + pindah_y;
 
 hasil_Xa5 = Xa5 + pindah_x; //proses transalasi titik E
 hasil_Ya5 = Ya5 + pindah_y;
 
 
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(1200,600);
glutInitWindowPosition(100,100);
glutCreateWindow("Transformasi Grafik dg translasi 5 Titik");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}




Hasil :



 

 

 

 

Komentar