ZDIRY-TUFWT-EBONM-EYJ00-IDBLANTER.COM
ZDIRY-TUFWT-EBONM-EYJ00
BLANTERWISDOM105

Cara membuat perpotongan dengan opengl

Thursday, 1 August 2019
Nesiasite - Tutorial & Berita Teknologi - Halo apa kabar kalian semua, kali ini saya akan berbagi sedikit codingan tentang Cara membuat perpotongan dengan opengl. Mudah-mudahan dengan artikel ini dapat membantu dan bermanfaat buat teman-teman semua. 
Perpotiongan dua garis ini, yang perlu kita lakukan adalah memasukan rumus matematika kedalam OpenGL kita. Kita hanya perlu perubahan sedikit saja terhadapap rumus tersebut. Dengan memasukan lebih dulu rumus untuk menentukan titik potong yang kita inginkan
Pada codingan saya bagikan ini ada dua perpotongan silahkan langsung saja di copy codingannya di bawah ini.



Source Code
#include
#include

void layar(void) {
 float ax = 11;
 float ay = 16;
 float bx = 12;
 float by = 23;
 float cx = 10;
 float cy = 21;
 float dx = 20;
 float dy = 22;

 float ex = 12;
 float ey = 23;
 float fx = 13;
 float fy = 28;
 float gx = 20;
 float gy = 26;
 float hx = 10;
 float hy = 25;

 float m1, c1, m2, c2, m3, c3, m4, c4, titikPotong1x, titikPotong1y, titikPotong2x, titikPotong2y;

 glClear(GL_COLOR_BUFFER_BIT);
 glBegin(GL_LINES);
 glLineWidth(6.0);
 //titik potong 1
 glColor3f(1.0, 0.0, 0.0);
 glVertex2i(ax, ay);
 glVertex2i(bx, by);

 glVertex2i(cx, cy);
 glVertex2i(dx, dy);
 glEnd();
 //titik poton2
 glBegin(GL_LINES);
 glLineWidth(6.0);
 glColor3f(1.0, 0.0, 0.0);
 glVertex2i(ex, ey);
 glVertex2i(fx, fy);

 glVertex2i(gx, gy);
 glVertex2i(hx, hy);
 glEnd();
 // ]titik potong 1
 m1 = (by - ay) / (bx - ax);

 c1 = ay - (m1 * ax);

 m2 = (dy - cy) / (dx - cx);

 c2 = cy - (m2 * cx);

 titikPotong1x = (c2 - c1) / (m1 - m2);
 titikPotong1y = (m1 * titikPotong1x) + c1;
 printf("(%f, %f)",
  titikPotong1x, titikPotong1y);

 //TITIK POTONG 2
 m3 = (fy - ey) / (fx - ex);

 c3 = ey - (m3 * ex);

 m4 = (gy - hy) / (gx - hx);

 c4 = hy - (m4 * hx);

 titikPotong2x = (c4 - c3) / (m3 - m4);
 titikPotong2y = (m3 * titikPotong2x) + c3;
 printf("(%f, %f)", titikPotong2x, titikPotong2y);

 //titik potong 1

 glPointSize(4);
 glBegin(GL_POINTS);
 glVertex2f(titikPotong1x, titikPotong1y);

 glEnd();

 //titik potong 2
 glBegin(GL_POINTS);
 glVertex2f(titikPotong2x, titikPotong2y);
 glEnd();
 glutSwapBuffers();

}

void Loading(void) {
 glClearColor(1.0, 1.0, 1.0, 0.0);
 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();
 glOrtho(-30.0, 50.0, -30.0, 50.0, -30.0, 30.0);
}

int main(int argc, char** argv) {
 glutInit(&argc, argv);
 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
 glutInitWindowSize(460, 480);
 glutInitWindowPosition(10, 10);
 glutCreateWindow("Felicio-672017901");
 Loading();
 glutDisplayFunc(layar);
 glutMainLoop();
 return 0;
}
 
glutSwapBuffers();
}
void yoo(void){
glClearColor(1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-30.0, 50.0, -30.0, 50.0, -30.0, 30.0);
}
int main(int argc, char **argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(460,480);
glutInitWindowPosition(10,10);
glutCreateWindow("Kevin");
yoo();
glutDisplayFunc(tampilan);
glutMainLoop();
return 0;
}

 #include
#include

void tampilan(void){
 float ax = 10;
 float ay = 10;
 float bx = 25;
 float by = 25;
 float cx = 15;
 float cy = 30;
 float dx = 20;
 float dy = 5;
 float m1, c1, m2, c2, ex, ey;

 glClear(GL_COLOR_BUFFER_BIT);
 glBegin(GL_LINES);
 glColor3f(1.0,0.0,0.0);
 glVertex2i(ax,ay);
 glVertex2i(bx,by);
 glVertex2i(cx,cy);
 glVertex2i(dx,dy);
 glEnd();

 m1=(by-ay)/(bx-ax);
 c1=ay-(m1*ax);

 m2=(dy-cy)/(dx-cx);
 c2=cy-(m2*cx);

 ex=(c2-c1)/(m1-m2);
 ey=(m1*ex)+c1;
 printf("(%f, %f)", ex, ey);
 glPointSize(4);
 glBegin(GL_POINTS);
 glVertex2f(ex,ey);
 glEnd();

 //glColor3f(1.0,0.5,1.0);
 //glPushMatrix();
 //glScaled(2, 2, 1);
 //glBegin(GL_LINE_LOOP);
 //glVertex2i(-5,-2);
 //glVertex2i(-1,-2);
 //glVertex2i(-1,5);
 //glVertex2i(-5,5);
 //glVertex2i(-5,3);
 //glVertex2i(-4,3);
 //glVertex2i(-4,4);
 //glVertex2i(-2,4);
 //glVertex2i(-2,2);
 //glVertex2i(-4,2);
 //glVertex2i(-4,1);
 //glVertex2i(-2,1);
 //glVertex2i(-2,-1);
 //glVertex2i(-4,-1);
 //glVertex2i(-4,0);
 //glVertex2i(-5,0);
 //glEnd();
 //glPopMatrix();
 //

 //glTranslated(-7, 20, 1);
 //glRotated(90, 0, 0, 1);
 //glScaled(1, -1, 1); 
 //glBegin(GL_LINE_LOOP);
 //glColor3f(1.0, 1.0, 1.0);
 //glVertex2i(-5,-2);
 //glVertex2i(-1,-2);
 //glVertex2i(-1,5);
 //glVertex2i(-5,5);
 //glVertex2i(-5,3);
 //glVertex2i(-4,3);
 //glVertex2i(-4,4);
 //glVertex2i(-2,4);
 //glVertex2i(-2,2);
 //glVertex2i(-4,2);
 //glVertex2i(-4,1);
 //glVertex2i(-2,1);
 //glVertex2i(-2,-1);
 //glVertex2i(-4,-1);
 //glVertex2i(-4,0);
 //glVertex2i(-5,0);
 //glEnd();

 //glTranslated(-34, 0, 1);
 //glRotated(-180, 0, 0, 1);
 //glScalef(1.0, -1.0, 1.0);
 //glBegin(GL_LINE_LOOP);
 //glColor3f(0.0, 1.0, 0.0);
 //glVertex2i(-5,-2);
 //glVertex2i(-1,-2);
 //glVertex2i(-1,5);
 //glVertex2i(-5,5);
 //glVertex2i(-5,3);
 //glVertex2i(-4,3);
 //glVertex2i(-4,4);
 //glVertex2i(-2,4);
 //glVertex2i(-2,2);
 //glVertex2i(-4,2);
 //glVertex2i(-4,1);
 //glVertex2i(-2,1);
 //glVertex2i(-2,-1);
 //glVertex2i(-4,-1);
 //glVertex2i(-4,0);
 //glVertex2i(-5,0);
 //glEnd();

 //glTranslated(-18, 15, 1);
 //glRotated(90, 0, 0, 1);
 //glScalef(1.0, -1.0, 1.0);
 //glBegin(GL_LINE_LOOP);
 //glColor3f(1.0, 0.0, 0.0);
 //glVertex2i(-5,-2);
 //glVertex2i(-1,-2);
 //glVertex2i(-1,5);
 //glVertex2i(-5,5);
 //glVertex2i(-5,3);
 //glVertex2i(-4,3);
 //glVertex2i(-4,4);
 //glVertex2i(-2,4);
 //glVertex2i(-2,2);
 //glVertex2i(-4,2);
 //glVertex2i(-4,1);
 //glVertex2i(-2,1);
 //glVertex2i(-2,-1);
 //glVertex2i(-4,-1);
 //glVertex2i(-4,0);
 //glVertex2i(-5,0);
 //glEnd();

 //glTranslated(-28, 0, 1);
 //glRotated(-180, 0, 0, 1);
 //glScalef(1.0, -1.0, 1.0);
 //glBegin(GL_LINE_LOOP);
 //glColor3f(0.0, 0.0, 1.0);
 //glVertex2i(-5,-2);
 //glVertex2i(-1,-2);
 //glVertex2i(-1,5);
 //glVertex2i(-5,5);
 //glVertex2i(-5,3);
 //glVertex2i(-4,3);
 //glVertex2i(-4,4);
 //glVertex2i(-2,4);
 //glVertex2i(-2,2);
 //glVertex2i(-4,2);
 //glVertex2i(-4,1);
 //glVertex2i(-2,1);
 //glVertex2i(-2,-1);
 //glVertex2i(-4,-1);
 //glVertex2i(-4,0);
 //glVertex2i(-5,0);
 //glEnd();

 glutSwapBuffers();

}

void yoo(void){
 glClearColor(1.0, 1.0, 1.0, 0.0);
 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();
 glOrtho(-30.0, 50.0, -30.0, 50.0, -30.0, 30.0);
}

int main(int argc, char **argv){
 glutInit(&argc, argv);
 glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
 glutInitWindowSize(460,480);
 glutInitWindowPosition(10,10);
 glutCreateWindow("Kevin");
 yoo();
 glutDisplayFunc(tampilan);
 glutMainLoop();
 return 0;
}
 

Oke teman-teman itulah sedikit codingan yang saya bagikan untuk kalian secara gratis. Silahkan di sedot saja untuk kebutuhan kalian, source code tersebut masih sangatlah sederhana. Silahkan teman-teman rubah atau tambah sesuai kebutuhan kalian. terimakasih
Share This :

0 komentar