Advertisement
Guest User

Untitled

a guest
Jun 18th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MPASM 2.53 KB | None | 0 0
  1. // CS 435 - Project 1: write a program to render a koch snowflake, or something
  2. // to that effect.
  3. // g++ -lGL -lGLU -lglut -o snowflake snowflake.cpp
  4. #include <GL/glut.h>
  5. #include <math.h>
  6. using namespace std;
  7.  
  8. #ifndef M_PI
  9. #define M_PI (3.1415926535897932384626433832795)
  10. #endif
  11.  
  12. double x = -0.5; // starting point
  13. double y = 0.3;
  14. double angle = 0; //starting angle is 0, drawing east ->
  15. int fractallevel = 8; // fractal depth, 8 is large enough to be detailed without being so large as to take FOREVER
  16.  
  17. void recurse_koch(int level, double length) {
  18.    if (level <= 0) { //oh ho ho we got here time to draw draw draw
  19.       double newx = x + length * cos(M_PI * angle / 180.0); // sum trig work it out if you want to know what it does
  20.       double newy = y + length * sin(M_PI * angle / 180.0); // mo' trig, all computed relatively to current point/direction
  21.       glBegin(GL_LINES);    // draw the line it is time :-o
  22.       glVertex2d(x, y);
  23.       glVertex2d(newx, newy);
  24.       glEnd();
  25.       x = newx; // change our starting point for the next line segment, dont wanna create a * thing.
  26.       y = newy; // doop doop
  27.    } else {
  28.       recurse_koch(level-1, length/3.0); // first line segment.
  29.       angle += 60; // rotate counter clockwise, creating the 'hump' (technical term)
  30.       recurse_koch(level-1, length/3.0); // upward half of hump
  31.       angle -= 120; // correct for the hump plus some to get back to where the line's supposed to be
  32.       recurse_koch(level-1, length/3.0); // downward half of hump
  33.       angle += 60; // there angle fixed we're going in the correct direction again
  34.       recurse_koch(level-1, length/3.0); // finish the line
  35.    }
  36. }
  37.  
  38. void koch(int level) {
  39.    recurse_koch(level, 1.0); // after this finishes, will be east of the starting point, so rotate clockwise
  40.    angle -= 120;
  41.    recurse_koch(level, 1.0); // after this finishes, will be at the bottom point, so rotate clockwise towards the orgin
  42.    angle -= 120;
  43.    recurse_koch(level, 1.0); // will finish at the orgin, completing the triangle/figure
  44. }
  45.  
  46. void display() {
  47.    glClearColor(1.0, 1.0, 1.0, 1.0);  // white background
  48.    glClear(GL_COLOR_BUFFER_BIT);
  49.    glLineWidth(1.0);
  50.    glColor3d(1.0, 0.0, 0.0); // red color
  51.    koch(fractallevel);
  52.    glMatrixMode(GL_MODELVIEW);
  53.    glFlush();
  54. }
  55.  
  56. int main(int argc, char ** argv) {
  57.    glutInit(&argc, argv);
  58.    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  59.    glutInitWindowSize(500, 500);
  60.    glutCreateWindow("Koch Snowflake");
  61.    glutDisplayFunc(display);
  62.    glutMainLoop();
  63.    return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement