CG/Assignment B-5a (Snowflake).cpp

61 lines
1.4 KiB
C++
Raw Normal View History

/*
Problem Statement: a) Write C++ program to generate snowflake using concept of fractals.
Code from Computer Graphics (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-codes/CG
*/
// BEGINNING OF CODE
2023-11-23 12:23:20 +05:30
#include<iostream>
#include<graphics.h>
#include<cmath>
using namespace std;
void koch(int x1,int y1,int x2,int y2,int it)
{
float angle = (60*3.14)/180;
int x3 = (2*x1 + x2)/3;
int y3 = (2*y1 + y2)/3;
int x4 = (2*x2 + x1)/3;
int y4 = (2*y2 + y1)/3;
int x = x3 + (x4-x3)*cos(angle) + (y4-y3)*sin(angle);
2023-11-23 12:23:20 +05:30
int y = y3 - (x4-x3)*sin(angle) + (y4-y3)*cos(angle);
if(it>0)
{
koch(x1,y1,x3,y3,it-1);
koch(x3,y3,x,y,it-1);
koch(x,y,x4,y4,it-1);
koch(x4,y4,x2,y2,it-1);
}
else
{
line(x1,y1,x3,y3);
line(x3,y3,x,y);
line(x,y,x4,y4);
line(x4,y4,x2,y2);
}
}
int main()
{
int gd = DETECT,gm;
initgraph(&gd,&gm,NULL);
int x1,y1,x2,y2,order;
float angle = (60*3.14)/180;
cout<<"Enter value of x1: ";
cin>>x1;
cout<<"Enter value of y1: ";
cin>>y1;
cout<<"Enter value of x2: ";
cin>>x2;
cout<<"Enter value of y2: ";
cin>>y2;
cout<<"Enter order of curve: ";
cin>>order;
int x3 = x1 + (x2-x1)*cos(angle) + (y2-y1)*sin(angle);
int y3 = y1 - (x2-x1)*sin(angle) + (y2-y1)*cos(angle);
koch(x2,y2,x1,y1,order);
2023-11-23 12:23:20 +05:30
koch(x1,y1,x3,y3,order);
koch(x3,y3,x2,y2,order);
delay(10000);
return 0;
}
// END OF CODE