Question

CS 334 Final Exam Prof. Danyang Thang Read the following function and draw the corresponding geometric shape according 6. to

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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:

Add a comment
Know the answer?
Add Answer to:
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...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT