#include <GL/glut.h>
#include <math.h>
const int WindowWidth=600,WindowHeight=600;
void lineDDA(int x0 , int y0 , int x1 , int y1)
{
if (x0>x1){ //if start point is at the right side
x0+=x1,x1=x0-x1,x0-=x1;
y0+=y1,y1=y0-y1,y0-=y1;}//swap start point & end point
int i;
if (y0==y1) //horizontal line
{for (i=x0; i<=x1; i++) glVertex2i(i, y0);return;}
if (x0==x1) //vertical line
{for (i=y0; i<=y1; i++) glVertex2i(x0, i);return;}
float x,y,delta_x,delta_y;
int dy,dx,steps;
dx = x1 - x0;
dy = y1 - y0;
if(abs(dx) > abs(dy)) { // |k|<1
steps=abs(dx);
delta_x=1, delta_y=(float)dy/(float)steps;}
else { // |k|>=1
steps=abs(dy);
if (dy<0) delta_y = -1; //for k<-1 case
else delta_y=1;
delta_x=(float)dx/(float)steps;}
x=x0, y=y0;
glVertex2i(x,y);
for (i=1; i<=steps; i++){
x+=delta_x, y+=delta_y;
glVertex2i((int)x,(int)y);
}
}
void MidPointLine(int x0 , int y0 , int x1 , int y1)
{
if (x0>x1){ //if start point is at the right side
x0+=x1,x1=x0-x1,x0-=x1;
y0+=y1,y1=y0-y1,y0-=y1;}//swap start point & end point
int i;
if (y0==y1) //horizontal line
{for (i=x0; i<=x1; i++) glVertex2i(i, y0);return;}
if (x0==x1) //vertical line
{for (i=y0; i<=y1; i++) glVertex2i(x0, i);return;}
int dx,dy,incrE,incrNE,d,x,y;
dx = (x1-x0);
dy = abs(y1-y0);
if ( dx > dy){ // |k|<1
d=dx-2*dy;
incrE=-2*dy;
incrNE=2*(dx-dy);
x=x0,y=y0;
glVertex2i(x,y);
while(x<x1)
{
if(d>0){ //just move right
d += incrE;
x++;}
else{ //
d += incrNE;
if (y1<y0) y--; //for -1<k<0 case
else y++;
x++;}
glVertex2i(x,y);
}
}
else{ // |k|>1
d=dy-2*dx;
incrE=-2*dx;
incrNE=2*(dy-dx);
x=x0,y=y0;
glVertex2i(x,y);
if (y1>y0) //for k>1 case
while(y<y1)
{
if(d>0){
d += incrE;
y++;}
else{
d += incrNE;
y++;
x++;}
glVertex2i(x,y);
}
else while(y>y1) //for k<-1 case
{
if(d>0){
d += incrE;
y--;}
else{
d += incrNE;
y--;
x++;}
glVertex2i(x,y);
}
}
}
void CirclePoints(int CenterX, int CenterY, int x,int y)
{
glVertex2i(x+CenterX,y+CenterY);
glVertex2i(y+CenterX,x+CenterY);
glVertex2i(-y+CenterX,x+CenterY);
glVertex2i(-x+CenterX,y+CenterY);
glVertex2i(y+CenterX,-x+CenterY);
glVertex2i(x+CenterX,-y+CenterY);
glVertex2i(-x+CenterX,-y+CenterY);
glVertex2i(-y+CenterX,-x+CenterY);
}
void MidPointCircle(int CenterX, int CenterY, int radius)
{
int x = 0;
int y = radius;
float d = 1.25-radius;
CirclePoints(CenterX, CenterY, x, y);
while(x<y){
x++;
if(d<=0) d += 2*x+3;
else d+=2*(x-y)+5,y--;
CirclePoints(CenterX, CenterY, x, y);
}
}
void draw()
{
glBegin(GL_POINTS);
glColor3f(1, 1, 1);//set DDA color as white
lineDDA(150, 160, 200, 290); //test k>1
lineDDA(150, 150, 200,180);//test 0<k<1
lineDDA(150, 150, 200,0);//test k<-1
lineDDA(150, 150, 200,130);//test -1<k<0
lineDDA(150, 150, 150, 280);//test vertical line
lineDDA(50, 150, 150, 150);//test horizontal line
glColor3f(1,0.5,0.7);//set MidPointLine color as pink
MidPointLine(-100, -100, 0,-70);//test 0<k<1
MidPointLine(-100, -100,0,-110);//test -1<k<0
MidPointLine(-50, -50,0,-300);//test k<-1
MidPointLine(0, 0, 50,200);//test k>1
MidPointLine(-50, -50,-50,-300);//test vertical line
MidPointLine(0, 200, 50,200);//test horizontal line
glColor3f(0.4,0.5,1);//set MidPointCircle color as blue
MidPointCircle(-150,20,100);
glEnd();
glFlush();
}
void resetSize(int w, int h)
{
glutReshapeWindow(WindowWidth, WindowHeight);
}
void init()
{
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(50 , 50);
glutInitWindowSize(WindowWidth, WindowHeight);
glutCreateWindow("Jinzhaoli's project 1");
gluOrtho2D(-WindowWidth/2, WindowWidth/2, -WindowHeight/2, WindowHeight/2);//set coordinate system
glutReshapeFunc(resetSize); //disable window size change
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.1, 0.1, 0.1); //set axis' color
glBegin(GL_LINES);
glVertex2i(-WindowWidth,0);
glVertex2i(WindowWidth,0);//draw x axis
glVertex2i(0,-WindowHeight);
glVertex2i(0,WindowHeight);//draw y axis
glEnd();
}
int main(int argc , char** argv)
{
glutInit(&argc , argv);
init();
glutDisplayFunc(draw);
glutMainLoop();
return 0;
}