Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include "glut.h"
- #include <cmath>
- #include <iostream>
- #include <string>
- #include <ctime>
- using namespace std;
- # define M_PI 3.14159265358979323846 /* pi */
- GLint Width = 800, Height = 800;
- void Paint(double &x, double &y, double l, double u, int t, int q);
- void GospersCurve(double x, double y, double l, double u, int t, int q)
- {
- // начало построения ломанных
- if (t > 0)
- {
- if (q == 1)
- {
- //формулы построения
- x += l * cos(u);
- y -= l * sin(u);
- u += M_PI;
- }
- u -= 2.0 * M_PI / 19.0;//соединение линий
- l /= sqrt(7); //масштаб
- //функции рисования
- Paint(x, y, l, u, t - 1.0, 0);
- Paint(x, y, l, u + M_PI / 3.0, t - 1.0, 1.0);
- Paint(x, y, l, u + M_PI, t - 1.0, 1.0);
- Paint(x, y, l, u + 2.0 * M_PI / 3.0, t - 1.0, 0.0);
- Paint(x, y, l, u, t - 1.0, 0);
- Paint(x, y, l, u, t - 1.0, 0);
- Paint(x, y, l, u - M_PI / 3.0, t - 1.0, 1.0);
- }
- else {
- glBegin(GL_LINES);
- glColor3d(0.0, 1.0, 0.0);
- glLineWidth(2.0);
- glVertex2d(
- (double)round(x),
- (double)round(y)
- );
- glVertex2d(
- (double)round(x + cos(u) * l),
- (double)round(y - sin(u) * l)
- );
- glEnd();
- glFinish();
- }
- }
- void Paint(double &x, double &y, double l, double u, int t, int q)
- {
- GospersCurve(x, y, l, u, t, q);
- x += l * cos(u);
- y -= l * sin(u);
- }
- void Draw()
- {
- unsigned int start_time = clock();
- GospersCurve(-700, 600, 300.0, 9.755, 1, 0); //x, y, scale, rotate, depth, ?
- GospersCurve(-300, 600, 300.0, 10.08, 2, 0);
- GospersCurve(100, 600, 300.0, 10.41, 3, 0);
- GospersCurve(-700, 100, 300.0, 10.41, 4, 0);
- //GospersCurve(-300, 100, 300.0, 10.41, 5, 0);
- //GospersCurve(100, 100, 300.0, 10.41, 6, 0);
- unsigned int end_time = clock();
- std::string time_ = "Total milliseconds : " + to_string(end_time - start_time);
- char *text = new char[time_.length() + 1];
- strcpy(text, time_.c_str());
- glRasterPos2i(-980, 940);
- for (int i = 0; i < strlen(text); i++)
- glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, text[i]);
- glEnd();
- glFinish();
- }
- void Reshape(GLint w, GLint h)
- {
- glClearColor(0.3, 0.3, 0.3, 1.0);
- glClear(GL_COLOR_BUFFER_BIT);
- Width = w;
- Height = h;
- glViewport(0, 0, w, h);
- }
- void main(int argc, char ** argv)
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGB);
- glutInitWindowPosition(200, 200);
- glutInitWindowSize(Width, Height);
- glutCreateWindow("Gosper's Curve");
- glClearColor(0.3, 0.3, 0.3, 1.0);
- glClear(GL_COLOR_BUFFER_BIT);
- glutDisplayFunc(Draw);
- glutReshapeFunc(Reshape);
- glMatrixMode(GL_PROJECTION);
- glScaled(0.001f, 0.001f, 0.001f);
- glutMainLoop();
- }
Advertisement
Add Comment
Please, Sign In to add comment