CODE:
#include <Windows.h>
#include <GL\glew.h>
#include <GL\freeglut.h>
#include <iostream>
using namespace std;
int x, y;
void changeView(int w, int h)
{
glViewport(0, 0, w, h);
}
void myinit(void)
{
glClearColor(0, 0, 0, 1.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 100.0, 0.0, 100.0);
}
void setPixel(GLint x, GLint y){
glPointSize(2);
glBegin(GL_POINTS);
glColor3f(1, 0, 0);
glVertex2i(x, y);
glEnd();
glFlush();
}
void draw(){
//int x0, int y0, int xEnd, int yEnd
int x0 = 18, y0 = 11, xEnd = 10, yEnd = 5;
int dx = fabs(static_cast<float>(xEnd - x0)), dy
= fabs(static_cast<float>(yEnd - y0));
int p = 2 * dy - dx;
int twoDy = 2 * dy, twoDyMinusDx = 2 * (dy - dx);
int x, y;
if (x0 > xEnd){
x = xEnd;
y = yEnd;
xEnd = x0;
}
else{
x = x0;
y = y0;
}
setPixel(x, y);
while (x < xEnd){
x++;
if (p < 0){
p +=
twoDy;
}
else{
y++;
p +=
twoDyMinusDx;
}
setPixel(x, y);
}
glutSwapBuffers();
}
void display(){
glClear(GL_COLOR_BUFFER_BIT);
draw(); //18, 11, 10, 5
glFlush();
}
int main(int argc, char* argv[]){
glutInit(&argc, argv); // Initialize glut
//create memory buffers to display
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(800, 600); // window size
glutCreateWindow(" "); // create window
glutReshapeFunc(changeView);
//glutDisplayFunc(render);
glutDisplayFunc(display);//links the display event
with the display event handler(display)
myinit();
glutIdleFunc(draw);
GLenum error = glewInit();
if (GLEW_OK != error){
fprintf(stderr, "Glew
error");
return 1;
}
glutMainLoop();
return 0;
}
OUTPUT:
CS 334 Final Exam Prof. Danyang Thang Read the following function and draw the corresponding geometric shape according 6. to the function call afterwards (draw it pixel-by-pixel, i.e., plot each p...
S 334 Final E 6Read the ollowing function and drw the corespodingoti she to the function call aterwards (draw it pisel-by pisel, Lplt ech piset) 5 vold dras (int O Int yo, int sEnd, Int yn int p 2dy -dx: int twoDy = 2 * dy, twoDyMinusDx-2+ (dy-da). int x, y: it (xo > xEnd) t x xEnd; xEnd = x0; else f x = x0; setPixel (x, y); I/ plot point (x, y) while (xxEnd) xt+; if (p <0)...