Transformasi Grafik menggunakan bentuk translasi dengan Empat Titik || Dev++
Transformasi Grafik
Translasi dengan Empat Titik
Grafika komputer |
Untuk menggambarkan translasi suatu objek berupa persegi dengan koordinat A(20,20) B(40,20), C(40,40) dan D(20,40) dengan tx,ty(20,30), 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
=20+30=50
A'=(40,50)
Titik B
x'B = xB +tx
=40+20=60
y'B = yB +ty
=20+20=40
B'=(60,40)
Titik C
x'C = xC +tx
=40+20=60
y'C = yC +ty
=40+30=70
C'=(60,70)
Titik D
x'D = xD +tx
=20+20=40
y'D = yD +ty
=40+20=60
D'=(40,60)
Koding
#include <windows.h>
#include <GL/gl.h>
#include <GL/glut.h>
#include <stdio.h>
int Xa1, Xa2, Xa3, Xa4, //nilai titik awal
Ya1, Ya2, Ya3, Ya4;
int pindah_x, pindah_y; //nilai perpindahan
int hasil_Xa1, hasil_Xa2, hasil_Xa3, hasil_Xa4, //hasil perpindahan titik
hasil_Ya1, hasil_Ya2, hasil_Ya3, hasil_Ya4;
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);
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);
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================================");
//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;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(1200,600);
glutInitWindowPosition(100,100);
glutCreateWindow("Transformasi Grafik dg translasi 4 Titik");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
Jangan lupa masukan Linker sebelum di Run :
-lopengl32
-lfreeglut
-lglu32
Komentar
Posting Komentar