Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #include <GL/gl.h>
- #include <GL/glut.h>
- typedef struct p Point;
- struct p{
- float x, y;
- p(float x,float y)
- {
- this->x = x;
- this->y = y;
- }
- Point operator=(const Point& p)
- {
- this->x = p.x;
- this->y=p.y;
- }
- void inc(float xi,float yi)
- {
- this->x+=xi;
- this->y+=yi;
- }
- void print()const
- {
- std::cout<<x<<" " <<y<<std::endl;
- }
- };
- Point A(10,20),B(30,50);
- std::vector<Point> dda()
- {
- std::vector<Point> ret;
- int dx = B.x-A.x;
- int dy = B.y - A.y;
- int step = std::max(dx,dy);
- float xInc = dx*1.0/step;
- float yInc = dy*1.0/step;
- Point temp = A;
- for(int i=0; i<step; i++)
- {
- temp.inc(xInc,yInc);
- ret.push_back(temp);
- temp.print();
- }
- return ret;
- }
- void display(void)
- {
- /* clear all pixels */
- glClear (GL_COLOR_BUFFER_BIT);
- /* draw white polygon (rectangle) with corners at
- * (0.25, 0.25, 0.0) and (0.75, 0.75, 0.0)
- */
- glColor3f (1.0, 0.25, 1.0);
- glBegin(GL_POINTS);
- std::vector<Point> points = dda();
- for(const auto& p:points) {
- glVertex3f(p.x,p.y,0);
- }
- glEnd();
- /* don't wait!
- * start processing buffered OpenGL routines
- */
- glutSwapBuffers();
- }
- void init (void)
- {
- glClearColor (0.0, 0.0, 0.0, 0.0); // Background Color
- glMatrixMode(GL_PROJECTION); /* initialize viewing values */
- glLoadIdentity();
- 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
- }
- /*
- * Declare initial window size, position, and display mode
- * (Double buffer & RGBA). Open window with "hello"
- * In its title bar. Call initialization routines.
- * Register callback function to display graphics.
- * Enter main loop and process events.
- */
- int main(int argc, char** argv)
- {
- glutInit(&argc, argv);
- glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); //Double Frame
- glutInitWindowSize (500, 500);
- glutInitWindowPosition (200, 200);
- glutCreateWindow ("DDA");
- init ();
- glutDisplayFunc(display);
- glutMainLoop();
- return 0; /* ISO C requires main to return int. */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement