Page 106 - Programming Microcontrollers in C
P. 106

Structures     91

                       struct point p1;
                       struct point p2;
                   };

                   /* here is a function to make a circle */
                   struct circle make_circle ( struct point ct, int
                   rad )
                   {
                       struct circle temp;


                       temp.center = ct;
                       temp.radius = rad;
                       return temp;
                   }


                   /* some useful function like macros */
                   #define min(a,b) (((a)<(b)) ? (a) : (b))
                   #define abs(a) ((a)<0 ? -(a) : (a))
                   /* function prototypes */


                   void draw_rectangle(struct rect);
                   void draw_circle(struct circle);

                   int main ( void )
                   {
                       struct circle cir;
                       struct point center;
                       struct rect window = { {80,80},{600,400} };
                       int radius,xc,yc;
                       center.x = (window.p1.x+window.p2.x)/2;
                       center.y = (window.p1.y+window.p2.y)/2;


                       xc = abs(window.p1.x -window.p2.x)/2;
                       yc = abs(window.p1.y -window.p2.y)/2;
                       radius = min(xc,yc);


                       cir=make_circle(center,radius);
   101   102   103   104   105   106   107   108   109   110   111