Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // File fractal.cpp
- // Hugo Gallée
- // Sat Feb 25 14:27:56 CET 2012
- #include <GL/glut.h>
- #include <math.h>
- #include <stdio.h>
- typedef struct point {
- float x;
- float y;
- } Point;
- void drawTriangle (Point a, Point b, Point c) {
- float z = 0.0;
- glBegin(GL_LINE_LOOP);
- glColor3f (0.0, 0.0, 1.0);
- glVertex3f(a.x, a.y, z);
- glVertex3f(b.x, b.y, z);
- glVertex3f(c.x, c.y, z);
- glEnd();
- }
- float distance (Point a, Point b) {
- return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
- }
- void drawFractal(Point a, Point b, float alpha, int prof) {
- float dist;
- if ((dist = distance(a, b)) < 0.01)return;
- Point c;
- float teta = 0.7;
- float r = dist / (2 * cos(teta));
- c.x = r * cos(teta + alpha) + a.x;
- c.y = r * sin(teta + alpha) + a.y;
- drawTriangle(a, b, c);
- drawFractal(a, c, alpha + teta, prof+1);
- drawFractal(c, b, alpha - teta, prof+1);
- }
- void init (void) {
- // select clearing color
- glClearColor (0.0, 0.0, 0.0, 0.0);
- // initialize viewing values
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
- }
- void display(void) {
- // clear all pixels
- glClear (GL_COLOR_BUFFER_BIT);
- Point a, b;
- a.x = 0.2;
- a.y = 0.5;
- b.x = 0.8;
- b.y = 0.5;
- drawFractal(a, b, 0.0, 0);
- drawFractal(b, a, M_PI, 0);
- // Swap the buffers to show the oneon which we writed
- glutSwapBuffers();
- }
- /*
- * Declare initial window size, position, and display mode
- * (double buffer and RGB). Open window with "hello"
- * in its title bar. Call initialization routines.
- * Register callback function to display graphics.
- * Enter main loop and process events.
- */
- int main(int argc, char** argv) {
- glutInit(&argc, argv);
- glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
- glutInitWindowSize (500, 500);
- glutInitWindowPosition (100, 100);
- glutCreateWindow ("triangles");
- init ();
- glutDisplayFunc(display);
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement