Advertisement
HugoBallee

fractal.cpp

Feb 28th, 2012
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.95 KB | None | 0 0
  1. // File fractal.cpp
  2. // Hugo Gallée
  3. // Sat Feb 25 14:27:56 CET 2012
  4.  
  5. #include <GL/glut.h>
  6. #include <math.h>
  7. #include <stdio.h>
  8.  
  9. typedef struct point {
  10.   float x;
  11.   float y;
  12. } Point;
  13.  
  14.  
  15. void drawTriangle (Point a, Point b, Point c) {
  16.   float z = 0.0;
  17.   glBegin(GL_LINE_LOOP);
  18.     glColor3f (0.0, 0.0, 1.0);
  19.     glVertex3f(a.x, a.y, z);
  20.     glVertex3f(b.x, b.y, z);
  21.     glVertex3f(c.x, c.y, z);
  22.   glEnd();
  23. }
  24.  
  25. float distance (Point a, Point b) {
  26.   return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
  27. }
  28.  
  29. void drawFractal(Point a, Point b, float alpha, int prof) {
  30.   float dist;
  31.   if ((dist = distance(a, b)) < 0.01)return;
  32.  
  33.   Point c;
  34.   float teta = 0.7;
  35.   float r = dist / (2 * cos(teta));
  36.  
  37.   c.x = r * cos(teta + alpha) + a.x;
  38.   c.y = r * sin(teta + alpha) + a.y;
  39.  
  40.   drawTriangle(a, b, c);
  41.   drawFractal(a, c, alpha + teta, prof+1);
  42.   drawFractal(c, b, alpha - teta, prof+1);
  43. }
  44.  
  45. void init (void) {
  46.     // select clearing color
  47.     glClearColor (0.0, 0.0, 0.0, 0.0);
  48.  
  49.     // initialize viewing values
  50.     glMatrixMode(GL_PROJECTION);
  51.     glLoadIdentity();
  52.     glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
  53. }
  54.  
  55. void display(void) {
  56.     // clear all pixels
  57.     glClear (GL_COLOR_BUFFER_BIT);
  58.  
  59.     Point a, b;
  60.     a.x = 0.2;
  61.     a.y = 0.5;
  62.     b.x = 0.8;
  63.     b.y = 0.5;
  64.  
  65.     drawFractal(a, b, 0.0, 0);
  66.     drawFractal(b, a, M_PI, 0);
  67.  
  68.     // Swap the buffers to show the oneon which we writed
  69.     glutSwapBuffers();
  70. }
  71.  
  72. /*
  73.  * Declare initial window size, position, and display mode
  74.  * (double buffer and RGB).  Open window with "hello"
  75.  * in its title bar.  Call initialization routines.
  76.  * Register callback function to display graphics.
  77.  * Enter main loop and process events.
  78.  */
  79. int main(int argc, char** argv) {
  80.     glutInit(&argc, argv);
  81.     glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
  82.     glutInitWindowSize (500, 500);
  83.     glutInitWindowPosition (100, 100);
  84.     glutCreateWindow ("triangles");
  85.     init ();
  86.     glutDisplayFunc(display);
  87.     glutMainLoop();
  88.     return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement