1
$\begingroup$

I am new to CG and I need to draw a wall clock using bresenhams circle algo . I have drawn a circle using this algo but how do I add minute and hour needle in it basically how do i add lines inside a circle ? I am using C language to code. Below is the code that i am using to draw a circle by this algo

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

    void    EightWaySymmetricPlot(int xc,int yc,int x,int y)
   {
    putpixel(x+xc,y+yc,RED);
    putpixel(x+xc,-y+yc,RED);
    putpixel(-x+xc,-y+yc,RED);
    putpixel(-x+xc,y+yc,RED);
    putpixel(y+xc,x+yc,6);
    putpixel(y+xc,-x+yc,6);
    putpixel(-y+xc,-x+yc,6);
    putpixel(-y+xc,x+yc,6);
   }

    void BresenhamCircle(int xc,int yc,int r)
   {
    int x=0,y=r,d=3-(2*r);
    EightWaySymmetricPlot(xc,yc,x,y);

    while(x<=y)
     {
      if(d<=0)
             {
        d=d+(4*x)+6;
      }
     else
      {
        d=d+(4*x)-(4*y)+10;
        y=y-1;
      }
       x=x+1;
       EightWaySymmetricPlot(xc,yc,x,y);
      }
    }

    int  main(void)
   {
    /* request auto detection */
    int xc,yc,r,gdriver = DETECT, gmode, errorcode;
    /* initialize graphics and local variables */
     initgraph(&gdriver, &gmode, " ");

     /* read result of initialization */
     errorcode = graphresult();

      if (errorcode != grOk)  /* an error occurred */
     {
        printf("Graphics error: %s\n", grapherrormsg(errorcode));
        printf("Press any key to halt:");
        getch();
        exit(1); /* terminate with an error code */
     }
       printf("Enter the values of xc and yc :");
       scanf("%d%d",&xc,&yc);
       printf("Enter the value of radius  :");
       scanf("%d",&r);
       BresenhamCircle(xc,yc,r);
     getch();
     closegraph();
     return 0;
    }

$\endgroup$
1
$\begingroup$

The usual method is to just draw over the circle with your lines. The clock is behind the minute hand and the minute hand is behind the hour hand so the drawing order would be. Draw the clock face, then draw the minute hand, then draw the hour hand.

This is the basic idea behind Z buffering.

Drawing the lines is basically the same idea as the circle, just write a function that draws a line it would take a start point and an end point as the parameters. Then another function figures out the start point and end points of each line based on the current time.

Figuring out the start and end points of the line is surprisingly simple. The start point is always the center of the circle. The end points are

clock_size *(cos(time_rads),sin(time_rads)) 

Where the clock_size is the size of your clock, and time_rads is the current time converted to radians. ( time_in_hours*60 + time_in_minutes gives minutes on the clock face, 2pi radians in a circle.....)

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.