Advertisement
Xom9ik

Lab_12/15var (IV semester) CG

May 26th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.33 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include "glut.h"
  5. #include <iostream>
  6. #include <string>
  7. using namespace std;
  8. int width = 960, height = 720;
  9.  
  10. void PutPixel(int x, int y) {
  11.     glBegin(GL_POINTS);
  12.     glColor3f(1.0, 0.0, 0.0);
  13.     glVertex2i(x, y);
  14.     glEnd();
  15. }
  16.  
  17. void DrawCircle(int x, int y, int r) {
  18.     int x1, y1, yk = 0;
  19.     int sigma, delta, f;
  20.     x1 = 0;
  21.     y1 = r;
  22.     delta = 2 * (1 - r);
  23.     do {
  24.         PutPixel(x + x1, y + y1);
  25.         PutPixel(x - x1, y + y1);
  26.         PutPixel(x + x1, y - y1);
  27.         PutPixel(x - x1, y - y1);
  28.         f = 0;
  29.         if (y1 < yk)
  30.             break;
  31.  
  32.         if (delta < 0) {
  33.             sigma = 2 * (delta + y1) - 1;
  34.             if (sigma <= 0) {
  35.                 x1++;
  36.                 delta += 2 * x1 + 1;
  37.                 f = 1;
  38.             }
  39.         }
  40.         else if (delta > 0) {
  41.             sigma = 2 * (delta - x1) - 1;
  42.             if (sigma > 0){
  43.                 y1--;
  44.                 delta += 1 - 2 * y1;
  45.                 f = 1;
  46.             }
  47.         }
  48.         if (!f) {
  49.             x1++;
  50.             y1--;
  51.             delta += 2 * (x1 - y1 - 1);
  52.         }
  53.     } while (1);
  54. }
  55.  
  56. void DrawLine(int x1, int y1, int x2, int y2) {
  57.     int dx, dy, i, e;
  58.     int incx, incy, inc1, inc2;
  59.     int x, y;
  60.     dx = x2 - x1;
  61.     dy = y2 - y1;
  62.     if (dx < 0) dx = -dx;
  63.     if (dy < 0) dy = -dy;
  64.     incx = 1;
  65.     if (x2 < x1) incx = -1;
  66.     incy = 1;
  67.     if (y2 < y1) incy = -1;
  68.     x = x1; y = y1;
  69.     if (dx > dy) {
  70.         PutPixel(x, y);
  71.         e = 2 * dy - dx;
  72.         inc1 = 2 * (dy - dx);
  73.         inc2 = 2 * dy;
  74.         for (i = 0; i<dx; i++) {
  75.             if (e >= 0) {
  76.                 y += incy;
  77.                 e += inc1;
  78.             }
  79.             else
  80.                 e += inc2;
  81.             x += incx;
  82.             PutPixel(x, y);
  83.         }
  84.  
  85.     }
  86.     else {
  87.         PutPixel(x, y);
  88.         e = 2 * dx - dy;
  89.         inc1 = 2 * (dx - dy);
  90.         inc2 = 2 * dx;
  91.         for (i = 0; i<dy; i++) {
  92.             if (e >= 0) {
  93.                 x += incx;
  94.                 e += inc1;
  95.             }
  96.             else
  97.                 e += inc2;
  98.             y += incy;
  99.             PutPixel(x, y);
  100.         }
  101.     }
  102.  
  103. }
  104. void Display(void) {                           
  105.     glClear(GL_COLOR_BUFFER_BIT);
  106.     DrawCircle(320, 240, 200);
  107.     DrawLine(320, 440, 320, 40);
  108.     DrawLine(120, 240, 520, 240);
  109.  
  110.     DrawLine(120, 240, 420, 413);
  111.     DrawLine(120, 240, 420, 67);
  112.     DrawLine(420, 413, 420, 67);
  113.  
  114.     glFlush();
  115. }
  116. void Init() {
  117.     glClearColor(1.0, 1.0, 1.0, 0);
  118.     glColor3f(0.0, 0.0, 0.0);
  119.     gluOrtho2D(0, width, 0, height);
  120. }
  121.  
  122. void main(int argc, char **argv) {
  123.     glutInit(&argc, argv);
  124.     glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  125.     glutInitWindowPosition(0, 0);
  126.     glutInitWindowSize(width, height);
  127.     glutCreateWindow("Bresenham");
  128.     Init();
  129.     glutDisplayFunc(Display);
  130.     glutMainLoop();
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement