Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GLFW/glfw3.h>
- #include <iostream>
- #include <cmath>
- void setPixel(int x, int y)
- {
- float pixel_x = float(x)/1000.0;
- float pixel_y = float(y)/1000.0;
- glBegin(GL_POINTS);
- glVertex2f(pixel_x, pixel_y);
- glEnd();
- glFlush();
- }
- void drawBresenhamLine(int x1, int y1, int x2, int y2)
- {
- /*
- В переменных x и y хранятся текущие значения координаты точки
- dx , dy - разница по иксу и по игрику
- d = (dy * 2) - dx
- d1 = dy * 2
- d2 = (dy - dx) * 2
- */
- int x, y, dx = x2 - x1, dy = y2 - y1;
- int d = (dy * 2) - dx, d1 = dy * 2, d2 = (dy - dx) * 2;
- x = x1, y = y1;
- setPixel(x1, y1);
- setPixel(x2, y2);
- x2--;
- while (x < x2)
- {
- x++;
- if (d < 0) d += d1;
- else
- {
- y++;
- d += d2;
- }
- setPixel(x, -y);
- }
- }
- void drawLine(int x1, int y1, int x2, int y2) {
- const int deltaX = abs(x2 - x1);
- const int deltaY = abs(y2 - y1);
- const int signX = x1 < x2 ? 1 : -1;
- const int signY = y1 < y2 ? 1 : -1;
- //
- int error = deltaX - deltaY;
- //
- setPixel(x2, y2);
- while (x1 != x2 || y1 != y2)
- {
- setPixel(x1, y1);
- const int error2 = error * 2;
- //
- if (error2 > -deltaY)
- {
- error -= deltaY;
- x1 += signX;
- }
- if (error2 < deltaX)
- {
- error += deltaX;
- y1 += signY;
- }
- }
- }
- void drawCircle(int x0, int y0, int radius) {
- int x = 0;
- int y = radius;
- int delta = 1 - 2 * radius;
- int error = 0;
- while (y >= 0) {
- //setPixel(x0 + x, y0 + y); //1-2
- setPixel(x0 + x, y0 - y); // 7-8
- //setPixel(x0 - x, y0 + y); // 3-4
- //setPixel(x0 - x, y0 - y); // 5-6
- error = 2 * (delta + y) - 1;
- if (delta < 0 && error <= 0) {
- ++x;
- delta += 2 * x + 1;
- continue;
- }
- error = 2 * (delta - x) - 1;
- if (delta > 0 && error > 0) {
- --y;
- delta += 1 - 2 * y;
- continue;
- }
- ++x;
- delta += 2 * (x - y);
- --y;
- }
- }
- int main(void)
- {
- GLFWwindow* window;
- if (!glfwInit())
- return -1;
- window = glfwCreateWindow(1000, 1000, "Hello World", NULL, NULL);
- if (!window)
- {
- glfwTerminate();
- return -1;
- }
- glfwMakeContextCurrent(window);
- while (!glfwWindowShouldClose(window))
- {
- //drawBresenhamLine(0, 0, 1000, 1000);
- drawLine(0, 0, 500, -1000);
- drawCircle(0, 0, 500);
- //glClear(GL_COLOR_BUFFER_BIT);
- glfwSwapBuffers(window);
- glfwPollEvents();
- }
- glfwTerminate();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement