sidrs

CGR Ass 5 - Koch Curve

Dec 15th, 2024
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.37 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. #include <graphics.h>
  4. using namespace std;
  5.  
  6. class kochCurve {
  7. public:
  8.     void koch(int it, int x1, int y1, int x5, int y5) {
  9.         int x2, y2, x3, y3, x4, y4;
  10.         int dx, dy;
  11.         if (it == 0) {
  12.             line(x1, y1, x5, y5); // Draw a straight line
  13.         } else {
  14.             delay(10);
  15.             dx = (x5 - x1) / 3;
  16.             dy = (y5 - y1) / 3;
  17.  
  18.             // Points for dividing and calculating the Koch curve
  19.             x2 = x1 + dx;
  20.             y2 = y1 + dy;
  21.             x3 = static_cast<int>(0.5 * (x1 + x5) + sqrt(3) * (y1 - y5) / 6);
  22.             y3 = static_cast<int>(0.5 * (y1 + y5) + sqrt(3) * (x5 - x1) / 6);
  23.             x4 = 2 * dx + x1;
  24.             y4 = 2 * dy + y1;
  25.  
  26.             // Recursive calls
  27.             koch(it - 1, x1, y1, x2, y2);
  28.             koch(it - 1, x2, y2, x3, y3);
  29.             koch(it - 1, x3, y3, x4, y4);
  30.             koch(it - 1, x4, y4, x5, y5);
  31.         }
  32.     }
  33. };
  34.  
  35. int main() {
  36.     kochCurve k;
  37.     int it;
  38.  
  39.     // User input
  40.     cout << "Enter Number Of Iterations: ";
  41.     cin >> it;
  42.  
  43.     // Graphics initialization
  44.     int gd = DETECT, gm;
  45.     initgraph(&gd, &gm, NULL);
  46.  
  47.     // Draw the Koch snowflake
  48.     k.koch(it, 150, 20, 20, 280);
  49.     k.koch(it, 280, 280, 150, 20);
  50.     k.koch(it, 20, 280, 280, 280);
  51.  
  52.     getch();
  53.     closegraph();
  54.     return 0;
  55. }
  56.  
Advertisement
Add Comment
Please, Sign In to add comment