Stopwatch program (need help!)

This is a discussion on Stopwatch program (need help!) within the C Programming forums, part of the General Programming Boards category; Hi, I'm a rookie in c programming and need some help with writing a simple stopwatch program. The stopwatch must ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    4

    Stopwatch program (need help!)

    Hi, I'm a rookie in c programming and need some help with writing a simple stopwatch program.

    The stopwatch must have a circular background, with both a digital (minutes: seconds) and analogue display (second, minute hands). The second hand must sweep behind the digital display as well. All graphics are to be done using only pixel manipulation.

    Can someone provide a bit of code to get me started on this? Thanks in advance!

  2. #2
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    %man 3 ctime

    if not on *nix

    here

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    You didn't say which OS/Compiler/graphics library you have

    You first need to get some practice in with drawing simple things on screen using your graphics library.
    Try looking in the local help and/or example programs which came with your library.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    4
    OS: Windows
    Compiler: Microsoft Visual C++

    I don't think I'm supposed to use any graphics compiler... must do it using pixel manipulation as stated. A helpful way is to use the Bresenham Algorithm to assist in drawing the lines and circles?

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I don't think I'm supposed to use any graphics compiler...
    You're stuck with ASCII art then. Plotting pixels in Windows isn't as simple as
    Code:
    plot ( x, y, color );
    You need some form of graphics library to do anything resembling pixel manipulation.
    My best code is written with the delete key.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Top down approach
    Start with using libplotline(x1,y1,x2,y2)
    When it all works, replace it with myplotline(x1,y1,x2,y2)

    Bottom up approach
    Forget all about the stopwatch problem and focus just on getting a line drawn between two points.

    Though to be honest, I'd check with your tutor as to what the requirement actually is. Doing everything you suggest given only plotpixel() is a non-trivial task.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    4
    Hi this is what I did so far, I managed to get the clock face up and running but not the digital display. No idea how to do that.. and how to get the hand to sweep behind the digital display? Can someone help with the coding? Thanks.

    Code:
    #include "stdafx.h"
    #include "resource.h"
    #include "math.h"
    #include "windows.h"
    #include "s4024906_1.h"
    
    #define RADIUS 150
    #define pi 3.141592654
    
    int myMethod(HDC hdc){
    
    int x = (GetTickCount() + 1000);
    double y = RADIUS;
    int i=270;
    drawCircle(hdc);
    
    while(0==0) {
    	if (GetTickCount() > x) {
    		drawSeconds(hdc,i);
    		i=i+6;
    		x= (GetTickCount() + 1000);
    		}	
    	} 
    }
    
    int drawCircle(HDC hdc) {
    
    int center=250;
    double i = 0;
        
    SetPixel(hdc, 250, 250, RGB( 0, 0, 0));
    
    SetPixel(hdc, center + RADIUS*cos(0), 250 + RADIUS*sin(0), RGB( 0, 0, 0));
    SetPixel(hdc, center + RADIUS*cos(pi/30), 250 + RADIUS*sin(pi/30), RGB( 0, 0, 0));
    SetPixel(hdc, center + RADIUS*cos(pi/15), 250 + RADIUS*sin(pi/15), RGB( 0, 0, 0));
    SetPixel(hdc, center + RADIUS*cos(pi/10), 250 + RADIUS*sin(pi/10), RGB( 0, 0, 0));
    SetPixel(hdc, center + RADIUS*cos(2*pi/15), 250 + RADIUS*sin(2*pi/15), RGB( 0, 0, 0));
    SetPixel(hdc, center + RADIUS*cos(pi/6), 250 + RADIUS*sin(pi/6), RGB( 0, 0, 0));
    SetPixel(hdc, center + RADIUS*cos(pi/5), 250 + RADIUS*sin(pi/5), RGB( 0, 0, 0));
    SetPixel(hdc, center + RADIUS*cos(7*pi/30), 250 + RADIUS*sin(7*pi/30), RGB( 0, 0, 0));
    SetPixel(hdc, center + RADIUS*cos(4*pi/15), 250 + RADIUS*sin(4*pi/15), RGB( 0, 0, 0));
    SetPixel(hdc, center + RADIUS*cos(3*pi/10), 250 + RADIUS*sin(3*pi/10), RGB( 0, 0, 0));
    SetPixel(hdc, center + RADIUS*cos(pi/3), 250 + RADIUS*sin(pi/3), RGB( 0, 0, 0));
    SetPixel(hdc, center + RADIUS*cos(11*pi/30), 250 + RADIUS*sin(11*pi/30), RGB( 0, 0, 0));
    SetPixel(hdc, center + RADIUS*cos(2*pi/5), 250 + RADIUS*sin(2*pi/5), RGB( 0, 0, 0));
    SetPixel(hdc, center + RADIUS*cos(13*pi/30), 250 + RADIUS*sin(13*pi/30), RGB( 0, 0, 0));
    SetPixel(hdc, center + RADIUS*cos(7*pi/15), 250 + RADIUS*sin(7*pi/15), RGB( 0, 0, 0));
    SetPixel(hdc, center + RADIUS*cos(pi/2), 250 + RADIUS*sin(pi/2), RGB( 0, 0, 0));
    
    SetPixel(hdc, 250 - RADIUS*cos(0), 250 + RADIUS*sin(0), RGB( 0, 0, 0));
    SetPixel(hdc, 250 - RADIUS*cos(pi/30), 250 + RADIUS*sin(pi/30), RGB( 0, 0, 0));
    SetPixel(hdc, 250 - RADIUS*cos(pi/15), 250 + RADIUS*sin(pi/15), RGB( 0, 0, 0));
    SetPixel(hdc, 250 - RADIUS*cos(pi/10), 250 + RADIUS*sin(pi/10), RGB( 0, 0, 0));
    SetPixel(hdc, 250 - RADIUS*cos(2*pi/15), 250 + RADIUS*sin(2*pi/15), RGB( 0, 0, 0));
    SetPixel(hdc, 250 - RADIUS*cos(pi/6), 250 + RADIUS*sin(pi/6), RGB( 0, 0, 0));
    SetPixel(hdc, 250 - RADIUS*cos(pi/5), 250 + RADIUS*sin(pi/5), RGB( 0, 0, 0));
    SetPixel(hdc, 250 - RADIUS*cos(7*pi/30), 250 + RADIUS*sin(7*pi/30), RGB( 0, 0, 0));
    SetPixel(hdc, 250 - RADIUS*cos(4*pi/15), 250 + RADIUS*sin(4*pi/15), RGB( 0, 0, 0));
    SetPixel(hdc, 250 - RADIUS*cos(3*pi/10), 250 + RADIUS*sin(3*pi/10), RGB( 0, 0, 0));
    SetPixel(hdc, 250 - RADIUS*cos(pi/3), 250 + RADIUS*sin(pi/3), RGB( 0, 0, 0));
    SetPixel(hdc, 250 - RADIUS*cos(11*pi/30), 250 + RADIUS*sin(11*pi/30), RGB( 0, 0, 0));
    SetPixel(hdc, 250 - RADIUS*cos(2*pi/5), 250 + RADIUS*sin(2*pi/5), RGB( 0, 0, 0));
    SetPixel(hdc, 250 - RADIUS*cos(13*pi/30), 250 + RADIUS*sin(13*pi/30), RGB( 0, 0, 0));
    SetPixel(hdc, 250 - RADIUS*cos(7*pi/15), 250 + RADIUS*sin(7*pi/15), RGB( 0, 0, 0));
    
    SetPixel(hdc, 250 - RADIUS*cos(0), 250 - RADIUS*sin(0), RGB( 0, 0, 0));
    SetPixel(hdc, 250 - RADIUS*cos(pi/30), 250 - RADIUS*sin(pi/30), RGB( 0, 0, 0));
    SetPixel(hdc, 250 - RADIUS*cos(pi/15), 250 - RADIUS*sin(pi/15), RGB( 0, 0, 0));
    SetPixel(hdc, 250 - RADIUS*cos(pi/10), 250 - RADIUS*sin(pi/10), RGB( 0, 0, 0));
    SetPixel(hdc, 250 - RADIUS*cos(2*pi/15), 250 - RADIUS*sin(2*pi/15), RGB( 0, 0, 0));
    SetPixel(hdc, 250 - RADIUS*cos(pi/6), 250 - RADIUS*sin(pi/6), RGB( 0, 0, 0));
    SetPixel(hdc, 250 - RADIUS*cos(pi/5), 250 - RADIUS*sin(pi/5), RGB( 0, 0, 0));
    SetPixel(hdc, 250 - RADIUS*cos(7*pi/30), 250 - RADIUS*sin(7*pi/30), RGB( 0, 0, 0));
    SetPixel(hdc, 250 - RADIUS*cos(4*pi/15), 250 - RADIUS*sin(4*pi/15), RGB( 0, 0, 0));
    SetPixel(hdc, 250 - RADIUS*cos(3*pi/10), 250 - RADIUS*sin(3*pi/10), RGB( 0, 0, 0));
    SetPixel(hdc, 250 - RADIUS*cos(pi/3), 250 - RADIUS*sin(pi/3), RGB( 0, 0, 0));
    SetPixel(hdc, 250 - RADIUS*cos(11*pi/30), 250 - RADIUS*sin(11*pi/30), RGB( 0, 0, 0));
    SetPixel(hdc, 250 - RADIUS*cos(2*pi/5), 250 - RADIUS*sin(2*pi/5), RGB( 0, 0, 0));
    SetPixel(hdc, 250 - RADIUS*cos(13*pi/30), 250 - RADIUS*sin(13*pi/30), RGB( 0, 0, 0));
    SetPixel(hdc, 250 - RADIUS*cos(7*pi/15), 250 - RADIUS*sin(7*pi/15), RGB( 0, 0, 0));
    SetPixel(hdc, 250 - RADIUS*cos(pi/2), 250 - RADIUS*sin(pi/2), RGB( 0, 0, 0));
    
    SetPixel(hdc, 250 + RADIUS*cos(pi/30), 250 - RADIUS*sin(pi/30), RGB( 0, 0, 0));
    SetPixel(hdc, 250 + RADIUS*cos(pi/15), 250 - RADIUS*sin(pi/15), RGB( 0, 0, 0));
    SetPixel(hdc, 250 + RADIUS*cos(pi/10), 250 - RADIUS*sin(pi/10), RGB( 0, 0, 0));
    SetPixel(hdc, 250 + RADIUS*cos(2*pi/15), 250 - RADIUS*sin(2*pi/15), RGB( 0, 0, 0));
    SetPixel(hdc, 250 + RADIUS*cos(pi/6), 250 - RADIUS*sin(pi/6), RGB( 0, 0, 0));
    SetPixel(hdc, 250 + RADIUS*cos(pi/5), 250 - RADIUS*sin(pi/5), RGB( 0, 0, 0));
    SetPixel(hdc, 250 + RADIUS*cos(7*pi/30), 250 - RADIUS*sin(7*pi/30), RGB( 0, 0, 0));
    SetPixel(hdc, 250 + RADIUS*cos(4*pi/15), 250 - RADIUS*sin(4*pi/15), RGB( 0, 0, 0));
    SetPixel(hdc, 250 + RADIUS*cos(3*pi/10), 250 - RADIUS*sin(3*pi/10), RGB( 0, 0, 0));
    SetPixel(hdc, 250 + RADIUS*cos(pi/3), 250 - RADIUS*sin(pi/3), RGB( 0, 0, 0));
    SetPixel(hdc, 250 + RADIUS*cos(11*pi/30), 250 - RADIUS*sin(11*pi/30), RGB( 0, 0, 0));
    SetPixel(hdc, 250 + RADIUS*cos(2*pi/5), 250 - RADIUS*sin(2*pi/5), RGB( 0, 0, 0));
    SetPixel(hdc, 250 + RADIUS*cos(13*pi/30), 250 - RADIUS*sin(13*pi/30), RGB( 0, 0, 0));
    SetPixel(hdc, 250 + RADIUS*cos(7*pi/15), 250 - RADIUS*sin(7*pi/15), RGB( 0, 0, 0));
    
    for (i=1;i<1440;i++)
    	SetPixel(hdc, 250 + RADIUS*cos(pi/(720/i)), 250 - RADIUS*sin(pi/(720/i)), RGB( 0, 0, 0));
    }
    
    int drawSeconds(HDC hdc, int i) {
    		 int x=0;
    		 int y=0;
             int z=0;
    
             for (z=0;z<135;z++) {		// new 2nd hand
         		 SetPixel(hdc, 250 + z*cos((i*pi)/(180)), 250 + z*sin((i*pi)/180), RGB( 0, 0, 0));
             }
    		 
    		 for (z=0;z<135;z++) {		// remove old 2nd hand
         		 SetPixel(hdc, 250 + z*cos(((i-6)*pi)/(180)), 250 + z*sin(((i-6)*pi)/180), RGB( 255,255,255));
    		 }
    }

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Here's something very simple. As far as how to get the hand to sweep behind the digital display, I don't know. But if you draw the hand first, then the digital display, maybe it will have almost the same effect. You might ask this question on the Windows programming board.
    Code:
    int myMethod(HDC hdc){
    
    int x_time = 250;
    int y_time = 250;
    int min, sec;
    char time_text[9];
    
    int x = (GetTickCount() + 1000);
    double y = RADIUS;
    int i=270;
    drawCircle(hdc);
    
    min = 0;
    sec = 0;
    sprintf(time_text,"%02d:%02d",min,sec);
    TextOut(hdc, x_time, y_time, time_text, strlen(time_text));
    while(0==0) {
    	if (GetTickCount() > x) {
    		drawSeconds(hdc,i);
    		i=i+6;
    		x= (GetTickCount() + 1000);
    		sec++;
    		if (sec == 60)
    		{
    			min++;
    			sec = 0;
    		}
    		sprintf(time_text,"%02d:%02d",min,sec);
    		TextOut(hdc, x_time, y_time, time_text, strlen(time_text));
    		}	
    	} 
    }

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    4
    Hi, thanks for your reply. However, I can't just use sprintf, i'm supposed to use pixel manipulation (like i did for the displaying of the seconds hand) to display the digits. Any idea how to do that?

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Ideally, you'd probably want to have a bitmap which represents each digit, then when a digit need displaying, draw that particular bitmap to the screen.

    A simpler idea would be to create a map of the digits, which you could use to determine which pixels to set. For example, make a 3-d array, with each digit being a 10x10 grid.
    Code:
       char digits[10][10][11] =
    {{
    " ######## ",
    " ######## ",
    " ##    ## ",
    " ##    ## ",
    " ##    ## ",
    " ##    ## ",
    " ##    ## ",
    " ##    ## ",
    " ######## ",
    " ######## "
    },
    //etc.
    Then use two nested for-loops to read the array and set each pixel.

    You could even scale the size of the digit (for example take the 10x10 grid and turn it into a 20x20 grid).
    Last edited by swoopy; 03-22-2004 at 12:36 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to do a stopwatch in a C++ program
    By h3ckf1r3 in forum C++ Programming
    Replies: 10
    Last Post: 09-12-2010, 08:19 PM
  2. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21