#include <GL/glut.h>
#include <math.h>
#include <stdio.h>
#include <windows.h>
int xf,xl,yf,yl;
void init(void)
{
glClearColor(1.0,1.0,1.0,0.0);
//Set display-window color to white.
glMatrixMode(GL_PROJECTION);
//Set projection parameters.
gluOrtho2D(0.0,200.0,0.0,150.0);
//Define the display window.
}
int abs(int x)
{
if(x>=0) return x;
else return -x;
}
int round(double x)
{
return int(x+0.5);
}
void setPixel(GLint xCoord,GLint yCoord)
{
int vertex1[]={xCoord-1,yCoord-1};
int vertex2[]={xCoord+1,yCoord+1};
DWORD iTime=GetCurrentTime();
glRectiv(vertex1,vertex2);
// while(GetCurrentTime()<iTime+20);
glFlush();
}
void DDAline(int x0,int y0,int x1,int y1)
{
int step,x,y,i;
double dx,dy,xcurent=double(x0),ycurent=double(y0);
x=abs(x1-x0);
y=abs(y1-y0);
step=x>y?x:y;
dx=double(x1-x0)/step;
dy=double(y1-y0)/step;
for(i=0;i<step;i++)
{
setPixel(round(xcurent),round(ycurent));
xcurent+=dx;
ycurent+=dy;
}
}
void lineSegment(void)
{
glClear(GL_COLOR_BUFFER_BIT);
//Clear display window.
glColor3f(0.0,1.0,1.0);
//Set line segment color to red.
DDAline(xf,yf,xl,yl);
//Specify line-segment geometry.
glFlush();
//Process all OpenGL routines as quickly as possible.
}
void main(int argc,char **argv)
{
glutInit(&argc,argv);
//Initialize GLUT.
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
//Set display mode;
glutInitWindowPosition(50,100);
//Set top-left display-window position.
glutInitWindowSize(300,400);
//Set display-window width and height.
glutCreateWindow("An Example OpenGL Program");
//Create display widow.
printf("please input the first point(x0,y0) of the line: ");
scanf("%d%d",&xf,&yf);
printf("please input the last point(x1,y1) of the line: ");
scanf("%d%d",&xl,&yl);
init();
//Execute initialization procedure.
glutDisplayFunc(lineSegment);
//Send graphics to display window.
glutMainLoop();
//Display everything and wait.
}
#include <GL/glut.h>
#include <math.h>
#include <stdio.h>
#include <windows.h>
int xf,xl,yf,yl;
void init(void)
{
glClearColor(1.0,1.0,1.0,0.0);
//Set display-window color to white.
glMatrixMode(GL_PROJECTION);
//Set projection parameters.
gluOrtho2D(0.0,200.0,0.0,150.0);
//Define the display window.
}
int abs(int x)
{
if(x>=0) return x;
else return -x;
}
int round(double x)
{
return int(x+0.5);
}
void setPixel(GLint xCoord,GLint yCoord)
{
int vertex1[]={xCoord-1,yCoord-1};
int vertex2[]={xCoord+1,yCoord+1};
DWORD iTime=GetCurrentTime();
glRectiv(vertex1,vertex2);
// while(GetCurrentTime()<iTime+20);
glFlush();
}
void DDAline(int x0,int y0,int x1,int y1)
{
int step,x,y,i;
double dx,dy,xcurent=double(x0),ycurent=double(y0);
x=abs(x1-x0);
y=abs(y1-y0);
step=x>y?x:y;
dx=double(x1-x0)/step;
dy=double(y1-y0)/step;
for(i=0;i<step;i++)
{
setPixel(round(xcurent),round(ycurent));
xcurent+=dx;
ycurent+=dy;
}
}
void lineSegment(void)
{
glClear(GL_COLOR_BUFFER_BIT);
//Clear display window.
glColor3f(0.0,1.0,1.0);
//Set line segment color to red.
DDAline(xf,yf,xl,yl);
//Specify line-segment geometry.
glFlush();
//Process all OpenGL routines as quickly as possible.
}
void main(int argc,char **argv)
{
glutInit(&argc,argv);
//Initialize GLUT.
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
//Set display mode;
glutInitWindowPosition(50,100);
//Set top-left display-window position.
glutInitWindowSize(300,400);
//Set display-window width and height.
glutCreateWindow("An Example OpenGL Program");
//Create display widow.
printf("please input the first point(x0,y0) of the line: ");
scanf("%d%d",&xf,&yf);
printf("please input the last point(x1,y1) of the line: ");
scanf("%d%d",&xl,&yl);
init();
//Execute initialization procedure.
glutDisplayFunc(lineSegment);
//Send graphics to display window.
glutMainLoop();
//Display everything and wait.
}