Tuesday, 30 December 2014

C program to draw Koch Curve using recursion


#include<graphics.h>
#include<conio.h>
#include<math.h>

void koch(int x1, int y1, int x2, int y2, int it)
{
float angle = 60*M_PI/180;
int x3 = (2*x1+x2)/3;
int y3 = (2*y1+y2)/3;

int x4 = (x1+2*x2)/3;
int y4 = (y1+2*y2)/3;

int x = x3 + (x4-x3)*cos(angle)+(y4-y3)*sin(angle);
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(void)
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
int x1 = 100, y1 = 100, x2 = 400, y2 = 400;
koch(x1, y1, x2, y2, 4);
getch();
return 0;
}

16 comments:

  1. there is an error in this program.
    int x1=100,y1=100,x2=400,y2=400
    Error:Declaration is not allowed here.

    ReplyDelete
    Replies
    1. just put initgraph below the declarations

      Delete
    2. just remove that void function from the int main(void) and compile the program then it will not show the error ^ remove it.

      Delete
  2. #include<iostream.h>

    #include<conio.h>

    #include<graphics.h>

    #include<math.h>

    /*struct node

    {

    float x;

    float y;

    };*/

    int dx;

    void koch(float x1,float y1,float x2,float y2)

    {

    float x,px,py;

    x=(x2-x1);

    if(x<=dx) return;

    if(y1==y2)

    {

    line(x1,y1,x1+x/3,y1);

    line(x1+2*x/3,y2,x2,y2);

    px=(x2-x1)/2+x1;

    py=y1-((x/6)*sqrt(3));

    line(x1+x/3,y1,px,py);

    line(px,py,x1+2*x/3,y2);

    setcolor(BLACK);

    line(x1+x/3,y1,x1+2*x/3,y1);

    setcolor(WHITE);

    koch(x1,y1,x1+x/3,y1);

    koch(x1+2*x/3,y2,x2,y2);

    koch(x1+x/3,y1,px,py);

    koch(px,py,x1+2*x/3,y2);

    }

    else

    {

    float ax,ay,bx,by,cx,cy;

    if(y1>y2)

    {

    setcolor(BLACK);

    line(x1,y1,x2,y2);

    setcolor(WHITE);

    ax=x1+x/3;

    ay=y1-(y1- y2)/3;

    cx=x1+2*x/3;

    cy=y1-2*(y1- y2)/3;

    bx=cx-2*x/3;

    by=cy;

    line(x1,y1,ax,ay);

    line(ax,ay,bx,by);

    line(bx,by,cx,cy);

    line(cx,cy,x2,y2);

    koch(x1,y1,ax,ay);

    koch(bx,by,ax,ay);

    koch(bx,by,cx,cy);

    koch(cx,cy,x2,y2);

    }

    if(y1<y2)

    {

    setcolor(BLACK);

    line(x1,y1,x2,y2);

    setcolor(WHITE);

    ax=x1+x/3;

    ay=y1+(y2-y1)/3;

    cx=x1+2*x/3;

    cy=y1+2*(y2-y1)/3;

    bx=ax+2*x/3;

    by=ay;

    line(x1,y1,ax,ay);

    line(ax,ay,bx,by);

    line(bx,by,cx,cy);

    line(cx,cy,x2,y2);

    koch(x1,y1,ax,ay);

    koch(ax,ay,bx,by);

    koch(cx,cy,bx,by);

    koch(cx,cy,x2,y2);

    }

    }

    }

    int main()

    {

    int gd=DETECT,gm;

    initgraph(&gd,&gm,"c:\\turboc3\\bgi");

    float x1,y1,x2,y2;

    cout<<"enter end points of line(x,y)";

    cin>>x1>>y1>>x2>>y2;

    cout<<"enter minimum spacing";

    cin>>dx;

    koch(x1,y1,x2,y2);

    getch();

    return 0;

    }

    ReplyDelete
  3. What is value of variable it....why we use that variable

    ReplyDelete
  4. What is value of variable it....why we use that variable

    ReplyDelete
    Replies
    1. i gues thats the degree of the koch curve.

      Delete
  5. how did u derive the formula of the points in method koch()?

    ReplyDelete
  6. hey ,
    I have also tried a C++ program for Koch curve and written it on my blog ,
    check it out at link below :
    http://computerstudentworld...
    and give me suggestions if any,
    Thank you!

    ReplyDelete
  7. ~~Insted of Taking the taking SIN & Cos values I take direct sin(60)=0.866 and cos(60)=0.5
    #include
    #include
    #include
    #include
    using namespace std;
    #define SIN 0.866

    void koch(int x1,int y1,int x2,int y2,int m)
    {
    int xx,yy,x[5],y[5],lx,ly,offx=50,offy=300;

    lx=(x2-x1)/3;
    ly=(y2-y1)/3;
    x[0]=x1;
    y[0]=y1;
    x[4]=x2;
    y[4]=y2;
    x[1]=x[0]+lx;
    y[1]=y[0]+ly;
    x[3]=x[0]+2*lx;
    y[3]=y[0]+2*ly;
    xx=x[3]-x[1];
    yy=y[3]-y[1];
    x[2]=xx*(0.5)+yy*(SIN);
    y[2]=-xx*(SIN)+yy*(0.5);
    x[2]=x[2]+x[1];
    y[2]=y[2]+y[1];
    if(m>0)
    {
    koch(x[0],y[0],x[1],y[1],m-1);
    koch(x[1],y[1],x[2],y[2],m-1);
    koch(x[2],y[2],x[3],y[3],m-1);
    koch(x[3],y[3],x[4],y[4],m-1);
    }
    else
    {
    line(offx+x[0],offy+y[0],offx+x[1],offy+y[1]);
    line(offx+x[1],offy+y[1],offx+x[2],offy+y[2]);
    line(offx+x[2],offy+y[2],offx+x[3],offy+y[3]);
    line(offx+x[3],offy+y[3],offx+x[4],offy+y[4]);
    }
    }
    int main()
    {
    int n,gd,gm;
    int x1=0,x2=550,y1=0,y2=0;
    cout<<"\nenter the level of the curve generation :";
    cin>>n;
    detectgraph(&gd,&gm);
    initgraph(&gd,&gm,NULL);
    koch(x1,y1,x2,y2,n);

    getch();
    closegraph();
    return 0;
    }

    ReplyDelete
  8. Best easy code for koch curve

    ReplyDelete