Anda di halaman 1dari 3

ddA

#include<windows.h>
#include<GL/glut.h>
#include<stdio.h>
#define wndw_h 1300
#define wndw_w 700

float x1,x2,y1,y2;
void SetPixel(int x, int y)
{
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
glFlush();
}
void algorithm()
{
float dx=x2-x1;
float dy=y2-y1;
float m=dy/dx;
if(m<=1)
{

if(x1>x2)
{
int x=x2;
int y=y2;
while(x<x1)
{
x++;
y=y+m+0.5;
SetPixel(x,y);
}
}
else
{
int x=x1;
int y=y1;
while(x<x2)
{
x++;
y=y+m+0.5;
SetPixel(x,y);
}
}

}
else
{

if(y1>y2)
{
int x=x2;
int y=y2;
while(y<y1)
{
y++;
x=x+1/m+0.5;
SetPixel(x,y);
}
}
else
{
int x=x1;
int y=y1;
while(y<y2)
{
y++;
x=x+1/m+0.5;
SetPixel(x,y);
}
}
}
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(1.0,1.0,1.0,1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glColor3f(1.0,0.0,0.0);
gluOrtho2D(0.0,wndw_h,0.0,wndw_w);

algorithm();
}
int main(int iArgc, char** cppArgv)
{
scanf("%f %f %f %f",&x1,&y1,&x2,&y2);
glutInit(&iArgc, cppArgv);

glutInitWindowSize(wndw_h,wndw_w);
glutInitWindowPosition(0,0);
glutCreateWindow("DDA2 Algorithm");
glutDisplayFunc(display);
glutMainLoop();
}

direct

#include<windows.h>
#include<GL/glut.h>

#define wndw_h 1300


#define wndw_w 700

float x1,x2,y1,y2;
void SetPixel(int x, int y)
{
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
//glutSwapBuffers();
glFlush();
}
void algorithm()
{
x1=60;
x2=500;
y1=60;
y2=500;
float dx=x2-x1;
float dy=y2-y1;
float m=dy/dx;
if(m<=1)
{
int x=x1;
int y=y1;
while(x<x2)
{
x++;
y=y+m+0.5;
SetPixel(x,y);
}
}
else
{
int x=x1;
int y=y1;
while(y<y2)
{
y++;
x=x+1/m+0.5;
SetPixel(x,y);
}
}
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(1.0,1.0,1.0,1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glColor3f(1.0,0.0,0.0);
gluOrtho2D(0.0,wndw_h,0.0,wndw_w);

algorithm();
}
int main(int iArgc, char** cppArgv)
{
glutInit(&iArgc, cppArgv);

glutInitWindowSize(wndw_h,wndw_w);
glutInitWindowPosition(0,0);
glutCreateWindow("DDA Algorithm");
glutDisplayFunc(display);
glutMainLoop();
}

Minat Terkait