fork download
  1. #include<iostream>
  2. #include<GL/glut.h>
  3. #include<stdlib.h>
  4. #include<stdio.h>
  5. using namespace std;
  6. #define SIN 0.86602540 //sin(60 degree)
  7.  
  8. int n;
  9. int x1=0,x2=550,y1=0,y2=0;
  10.  
  11. // draw a polygon via list of vertices
  12. void koch(int x1,int y1,int x2,int y2,int m)
  13. {
  14. int xx,yy,x[5],y[5],lx,ly,offx=50,offy=300;
  15. lx=(x2-x1)/3;
  16. ly=(y2-y1)/3;
  17. x[0]=x1; //store point p0;
  18. y[0]=y1;
  19. x[4]=x2; //store point p4;
  20. y[4]=y2;
  21. x[1]=x[0]+lx; //store point p1;
  22. y[1]=y[0]+ly;
  23. x[3]=x[0]+2*lx; //store point p3;
  24. y[3]=y[0]+2*ly;
  25.  
  26. xx=x[3]-x[1]; //Translate point p2 to Origin;
  27. yy=y[3]-y[1];
  28.  
  29. x[2]=xx*(0.5)+yy*(SIN); //Perform rotation for point p3
  30. y[2]=-xx*(SIN)+yy*(0.5);
  31.  
  32. x[2]=x[2]+x[1]; //Perform inverse translation;
  33. y[2]=y[2]+y[1];
  34.  
  35. if(m>0)
  36. {
  37. koch(x[0],y[0],x[1],y[1],m-1); //Recursive call to Draw Part1
  38. koch(x[1],y[1],x[2],y[2],m-1); //Recursive call to Draw Part2
  39. koch(x[2],y[2],x[3],y[3],m-1); //Recursive call to Draw Part3
  40. koch(x[3],y[3],x[4],y[4],m-1); //Recursive call to Draw Part4
  41. }
  42. else
  43. {
  44. glBegin(GL_LINES);
  45. glVertex2d(offx+x[0],650-(offy+y[0]));
  46. glVertex2d(offx+x[1],650-(offy+y[1]));
  47. glEnd();
  48.  
  49. glBegin(GL_LINES);
  50. glVertex2d(offx+x[1],650-(offy+y[1]));
  51. glVertex2d(offx+x[2],650-(offy+y[2]));
  52. glEnd();
  53.  
  54. glBegin(GL_LINES);
  55. glVertex2d(offx+x[2],650-(offy+y[2]));
  56. glVertex2d(offx+x[3],650-(offy+y[3]));
  57. glEnd();
  58.  
  59. glBegin(GL_LINES);
  60. glVertex2d(offx+x[3],650-(offy+y[3]));
  61. glVertex2d(offx+x[4],650-(offy+y[4]));
  62. glEnd();
  63. }
  64. }
  65.  
  66. // display callback
  67. void display(void)
  68. {
  69. glClear(GL_COLOR_BUFFER_BIT);
  70. glColor3f(1.0,1.0,1.0);
  71. koch(x1,y1,x2,y2,n); // draw koch curve
  72. glFlush();// send all output to display
  73. }
  74.  
  75. void myinit()
  76. {
  77. glClearColor(0.0,0.0,0.0,1.0);// Set backgroud as black
  78. glColor3f(1.0,1.0,0.0); //Draw in Yellow
  79. glMatrixMode(GL_PROJECTION); // Estabilish a coordinate system
  80. glLoadIdentity();
  81. gluOrtho2D(0.0,650.0,0.0,650.0);
  82. }
  83.  
  84. int main(int argc, char **argv)
  85. {
  86. cout<<"Enter the level of curve Generation :";
  87. cin>>n;
  88.  
  89. glutInit(&argc,argv);
  90. glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  91. glutInitWindowSize(650,650);
  92. glutInitWindowPosition(0,0);
  93. glutCreateWindow("Koch Curve ");
  94. glutDisplayFunc(display);
  95. myinit();
  96. glutMainLoop();
  97. return 0;
  98. }
  99.  
  100.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c:1:9: fatal error: iostream: No such file or directory
 #include<iostream>
         ^~~~~~~~~~
compilation terminated.
stdout
Standard output is empty