Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.28 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. #include <GL/gl.h>
  4. #include <GL/glut.h>
  5.  
  6. typedef struct p Point;
  7.  
  8. struct p{
  9.     float x, y;
  10.     p(float x,float y)
  11.     {
  12.         this->x = x;
  13.         this->y = y;
  14.     }
  15.     Point operator=(const Point& p)
  16.     {
  17.         this->x = p.x;
  18.         this->y=p.y;
  19.     }
  20.     void inc(float xi,float yi)
  21.     {
  22.         this->x+=xi;
  23.         this->y+=yi;
  24.     }
  25.     void print()const
  26.     {
  27.         std::cout<<x<<" " <<y<<std::endl;
  28.     }
  29. };
  30.  
  31. Point A(10,20),B(30,50);
  32.  
  33. std::vector<Point> dda()
  34. {
  35.    std::vector<Point> ret;
  36.     int dx = B.x-A.x;
  37.     int dy = B.y - A.y;
  38.     int step = std::max(dx,dy);
  39.  
  40.     float xInc = dx*1.0/step;
  41.     float yInc = dy*1.0/step;
  42.  
  43.     Point temp = A;
  44.  
  45.     for(int i=0; i<step; i++)
  46.     {
  47.         temp.inc(xInc,yInc);
  48.         ret.push_back(temp);
  49.         temp.print();
  50.     }
  51.  
  52.     return ret;
  53. }
  54.  
  55. void display(void)
  56. {
  57.     /* clear all pixels */
  58.     glClear (GL_COLOR_BUFFER_BIT);
  59.     /* draw white polygon (rectangle) with corners at
  60.     * (0.25, 0.25, 0.0) and (0.75, 0.75, 0.0)
  61.     */
  62.     glColor3f (1.0, 0.25, 1.0);
  63.     glBegin(GL_POINTS);
  64.  
  65.     std::vector<Point> points = dda();
  66.  
  67.     for(const auto& p:points) {
  68.         glVertex3f(p.x,p.y,0);
  69.     }
  70.  
  71.     glEnd();
  72.     /* don't wait!
  73.     * start processing buffered OpenGL routines
  74.     */
  75.     glutSwapBuffers();
  76. }
  77.  
  78.  
  79.  
  80. void init (void)
  81. {
  82.  
  83.     glClearColor (0.0, 0.0, 0.0, 0.0);   // Background Color
  84.     glMatrixMode(GL_PROJECTION);        /* initialize viewing values */
  85.     glLoadIdentity();
  86.     glOrtho(0.0, 100.0, 0.0, 100.0, -1.0, 1.0); // To specify the coordinate & Specify the distances to the nearer and farther depth
  87. }
  88.  
  89. /*
  90. * Declare initial window size, position, and display mode
  91. * (Double buffer & RGBA). Open window with "hello"
  92. * In its title bar. Call initialization routines.
  93. * Register callback function to display graphics.
  94. * Enter main loop and process events.
  95. */
  96.  
  97.  
  98. int main(int argc, char** argv)
  99. {
  100.     glutInit(&argc, argv);
  101.     glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); //Double Frame
  102.     glutInitWindowSize (500, 500);
  103.     glutInitWindowPosition (200, 200);
  104.     glutCreateWindow ("DDA");
  105.     init ();
  106.     glutDisplayFunc(display);
  107.     glutMainLoop();
  108.  
  109.     return 0; /* ISO C requires main to return int. */
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement