Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <stdio.h>
- #include <math.h>
- #include "glut.h"
- #include <iostream>
- #include <string>
- using namespace std;
- int width = 960, height = 720;
- void PutPixel(int x, int y) {
- glBegin(GL_POINTS);
- glColor3f(1.0, 0.0, 0.0);
- glVertex2i(x, y);
- glEnd();
- }
- void DrawCircle(int x, int y, int r) {
- int x1, y1, yk = 0;
- int sigma, delta, f;
- x1 = 0;
- y1 = r;
- delta = 2 * (1 - r);
- do {
- PutPixel(x + x1, y + y1);
- PutPixel(x - x1, y + y1);
- PutPixel(x + x1, y - y1);
- PutPixel(x - x1, y - y1);
- f = 0;
- if (y1 < yk)
- break;
- if (delta < 0) {
- sigma = 2 * (delta + y1) - 1;
- if (sigma <= 0) {
- x1++;
- delta += 2 * x1 + 1;
- f = 1;
- }
- }
- else if (delta > 0) {
- sigma = 2 * (delta - x1) - 1;
- if (sigma > 0){
- y1--;
- delta += 1 - 2 * y1;
- f = 1;
- }
- }
- if (!f) {
- x1++;
- y1--;
- delta += 2 * (x1 - y1 - 1);
- }
- } while (1);
- }
- void DrawLine(int x1, int y1, int x2, int y2) {
- int dx, dy, i, e;
- int incx, incy, inc1, inc2;
- int x, y;
- dx = x2 - x1;
- dy = y2 - y1;
- if (dx < 0) dx = -dx;
- if (dy < 0) dy = -dy;
- incx = 1;
- if (x2 < x1) incx = -1;
- incy = 1;
- if (y2 < y1) incy = -1;
- x = x1; y = y1;
- if (dx > dy) {
- PutPixel(x, y);
- e = 2 * dy - dx;
- inc1 = 2 * (dy - dx);
- inc2 = 2 * dy;
- for (i = 0; i<dx; i++) {
- if (e >= 0) {
- y += incy;
- e += inc1;
- }
- else
- e += inc2;
- x += incx;
- PutPixel(x, y);
- }
- }
- else {
- PutPixel(x, y);
- e = 2 * dx - dy;
- inc1 = 2 * (dx - dy);
- inc2 = 2 * dx;
- for (i = 0; i<dy; i++) {
- if (e >= 0) {
- x += incx;
- e += inc1;
- }
- else
- e += inc2;
- y += incy;
- PutPixel(x, y);
- }
- }
- }
- void Display(void) {
- glClear(GL_COLOR_BUFFER_BIT);
- DrawCircle(320, 240, 200);
- DrawLine(320, 440, 320, 40);
- DrawLine(120, 240, 520, 240);
- DrawLine(120, 240, 420, 413);
- DrawLine(120, 240, 420, 67);
- DrawLine(420, 413, 420, 67);
- glFlush();
- }
- void Init() {
- glClearColor(1.0, 1.0, 1.0, 0);
- glColor3f(0.0, 0.0, 0.0);
- gluOrtho2D(0, width, 0, height);
- }
- void main(int argc, char **argv) {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
- glutInitWindowPosition(0, 0);
- glutInitWindowSize(width, height);
- glutCreateWindow("Bresenham");
- Init();
- glutDisplayFunc(Display);
- glutMainLoop();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement