Ask Question

Name:
Title:
Your Question:

Answer Question

Name:
Your Answer:
User Submitted Source Code!


Description:
  1.c
Language: C/C++
Code:
        #include <graphics.h>
        #include <stdlib.h>
        #include <stdio.h>
        #include <conio.h>
        #include <math.h>
        #define Pi 3.1415926536
        void init_gr(void)
        {
        int gdriver = DETECT, gmode, errorcode;
        initgraph(&gdriver, &gmode, "");
        errorcode = graphresult();
        if (errorcode != grOk) /* an error occurred */
        {
        printf("Graphics error: %s\n", grapherrormsg(errorcode));
        printf("Press any key to halt:");
        getch();
        exit(1); /* return with error code */
        }
        }
        void end_gr(void)
        {
        closegraph();
        }
        void MoveTo(int x, int y)
        {
        moveto(x,y);
        }
        void LineTo(int x, int y)
        {
        lineto(x,y);
        }
        const N=6; // number of points in the figure
       
        enum actions {MOVE,DRAW};
        struct
        {
        actions action;
        int x;
        int y;
        } figure[N]={{MOVE,360,270},{DRAW,360,260},{DRAW,355,260},{DRAW,360,250},
        {DRAW,365,260},{DRAW,360,260}};
        int x0,y0,dx,dy;
        float phi;
        int main(void)
        {
        init_gr();
        x0=300;
        y0=260;
        phi=45.0*Pi/180.0;
        for(int i=0;i<8;i++)
        {
        for (int j=0;j<N;j++)
        {
        dx=figure[j].x-x0;
        dy=figure[j].y-y0;
        figure[j].x=x0+dx*cos(phi)-dy*sin(phi);
        figure[j].y=y0+dx*sin(phi)+dy*cos(phi);
        }
        // drawing rotated figure
        for (j=0;j<N;j++)
        if (figure[j].action==MOVE)
        MoveTo(figure[j].x,figure[j].y);
        else
        LineTo(figure[j].x,figure[j].y);
        }
        // clean up
        getch();
        end_gr();
        return 0;
        }     
Comments: