Xom9ik

SRS #2/15var (IV semester) CG

May 2nd, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.67 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "glut.h"
  3. #include <cmath>
  4. #include <iostream>
  5. #include <string>
  6. #include <ctime>
  7.  
  8. using namespace std;
  9.  
  10. # define M_PI           3.14159265358979323846  /* pi */
  11. GLint Width = 800, Height = 800;
  12.  
  13. void Paint(double &x, double &y, double l, double u, int t, int q);
  14. void GospersCurve(double x, double y, double l, double u, int t, int q)
  15. {
  16.     // начало построения ломанных
  17.     if (t > 0)
  18.     {
  19.         if (q == 1)
  20.         {
  21.             //формулы построения
  22.             x += l * cos(u);
  23.             y -= l * sin(u);
  24.             u += M_PI;
  25.         }
  26.         u -= 2.0 * M_PI / 19.0;//соединение линий
  27.         l /= sqrt(7); //масштаб
  28.                       //функции рисования
  29.         Paint(x, y, l, u, t - 1.0, 0);
  30.         Paint(x, y, l, u + M_PI / 3.0, t - 1.0, 1.0);
  31.         Paint(x, y, l, u + M_PI, t - 1.0, 1.0);
  32.         Paint(x, y, l, u + 2.0 * M_PI / 3.0, t - 1.0, 0.0);
  33.         Paint(x, y, l, u, t - 1.0, 0);
  34.         Paint(x, y, l, u, t - 1.0, 0);
  35.         Paint(x, y, l, u - M_PI / 3.0, t - 1.0, 1.0);
  36.     }
  37.     else {
  38.         glBegin(GL_LINES);
  39.         glColor3d(0.0, 1.0, 0.0);
  40.         glLineWidth(2.0);
  41.         glVertex2d(
  42.             (double)round(x),
  43.             (double)round(y)
  44.         );
  45.         glVertex2d(
  46.             (double)round(x + cos(u) * l),
  47.             (double)round(y - sin(u) * l)
  48.         );
  49.         glEnd();
  50.         glFinish();
  51.     }
  52. }
  53. void Paint(double &x, double &y, double l, double u, int t, int q)
  54. {
  55.     GospersCurve(x, y, l, u, t, q);
  56.     x += l * cos(u);
  57.     y -= l * sin(u);
  58. }
  59.  
  60. void Draw()
  61. {
  62.     unsigned int start_time = clock();
  63.     GospersCurve(-700, 600, 300.0, 9.755, 1, 0); //x, y, scale, rotate, depth, ?
  64.     GospersCurve(-300, 600, 300.0, 10.08, 2, 0);
  65.     GospersCurve(100, 600, 300.0, 10.41, 3, 0);
  66.     GospersCurve(-700, 100, 300.0, 10.41, 4, 0);
  67.     //GospersCurve(-300, 100, 300.0, 10.41, 5, 0);
  68.     //GospersCurve(100, 100, 300.0, 10.41, 6, 0);
  69.     unsigned int end_time = clock();
  70.     std::string time_ = "Total milliseconds : " + to_string(end_time - start_time);
  71.  
  72.     char *text = new char[time_.length() + 1];
  73.     strcpy(text, time_.c_str());
  74.     glRasterPos2i(-980, 940);
  75.     for (int i = 0; i < strlen(text); i++)
  76.         glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, text[i]);
  77.     glEnd();
  78.     glFinish();
  79. }
  80. void Reshape(GLint w, GLint h)
  81. {
  82.     glClearColor(0.3, 0.3, 0.3, 1.0);
  83.     glClear(GL_COLOR_BUFFER_BIT);
  84.     Width = w;
  85.     Height = h;
  86.     glViewport(0, 0, w, h);
  87. }
  88. void main(int argc, char ** argv)
  89. {
  90.     glutInit(&argc, argv);
  91.     glutInitDisplayMode(GLUT_RGB);
  92.     glutInitWindowPosition(200, 200);
  93.     glutInitWindowSize(Width, Height);
  94.     glutCreateWindow("Gosper's Curve");
  95.  
  96.     glClearColor(0.3, 0.3, 0.3, 1.0);
  97.     glClear(GL_COLOR_BUFFER_BIT);
  98.  
  99.     glutDisplayFunc(Draw);
  100.     glutReshapeFunc(Reshape);
  101.     glMatrixMode(GL_PROJECTION);
  102.  
  103.     glScaled(0.001f, 0.001f, 0.001f);
  104.  
  105.     glutMainLoop();
  106. }
Advertisement
Add Comment
Please, Sign In to add comment