Sekedar sharing buat teman-teman mahasiswa, dan juga buat dokumentasi.
Siapa tahu dibutuhkan suatu saat...
// AhmadFadlyDzilJalal_2010470121_KG2.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <glut.h>
GLuint global_w=800;
GLuint global_h=600;
void init();
void display();
void reshape(int w,int h);
void AlgoritmaNaif1(int x1,int y1,int x2,int y2);
void AlgoritmaNaif2(int x1,int y1,int x2,int y2);
void AlgoritmaDDA (int x1,int y1,int x2,int y2);
void MidPoint (int x1,int y1,int x2,int y2);
int _tmain(int argc, char* argv[])
{
glutInit(&argc,argv );
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutInitWindowPosition(50,50);
glutInitWindowSize(global_w,global_h);
glutCreateWindow ("Tugas");
glutSetWindowTitle( "Garis Dengan Algoritma Naif 1 & 2 & DDA" );
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
void reshape(int w,int h)
{
global_w=w;
global_h=h;
glViewport(0,0,(GLsizei)w,(GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,w,0,h);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glScalef(1,1,1);
glutPostRedisplay();
}
void init()
{
glClearColor(0.3,0.5,0.5,1);
glShadeModel(GL_3D);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1,1,0);//glPointSize(20);
glBegin(GL_POINTS);
AlgoritmaNaif1(0,10,200,500 );
AlgoritmaNaif2(0,10,500,500);
AlgoritmaDDA (0,0,500,500);
MidPoint (100,100,500,150);
glEnd();
glFlush();
}
void MidPoint (int x1,int y1,int x2,int y2)
{
GLint xi =x1;
GLint yi =y1;
GLint dy = y2-y1;
GLint dx = x2-x1;
GLint D = (2 * dy) -dx;
GLint dE = 2* dy;
GLint dNE = dy-dx;
while (xi <= x2)
{
glVertex2f (xi,yi);
if (D<0) D+= dE;
else {
D+= dNE;
xi++;
}
yi++;
}
}
void AlgoritmaNaif1(int x1,int y1,int x2,int y2) {
GLfloat xi=x1;
GLfloat yi=y1;
GLfloat m=(y2-y1)/(x2-x1);
for (xi=x1; xi<x2; xi++) {
yi=m*(xi-x1)+y1;
glVertex2f(xi,yi);
printf("(%f,%f)\n",xi,yi);
}
}
void AlgoritmaNaif2(int x1,int y1,int x2,int y2) {
GLfloat xi=0;
GLfloat yi=y1;
GLfloat m=(y2-y1)/(x2-x1);
for (xi=x1; xi<x2; xi++) {
yi=m*(xi-x1)+y1;
yi+=m;
glVertex2f(xi,yi);
printf("(%f,%f)\n",xi,yi);
}
}
void AlgoritmaDDA (int x1, int y1, int x2, int y2){
GLfloat xi=x1;
GLfloat yi=y1;
GLfloat m = (y2-y1)/(x2-x1);
for (xi=x1; xi<x2; xi++){
yi+= m;
glVertex2f (xi,yi);
printf ("%f, %f\n", xi,yi);
}
}