我需要一个有助于绘制时钟的功能的说明

时间:2019-06-03 09:30:45

标签: c++

我被要求注释代码并将函数名称从未知重新设置为合适的名称,这里是代码。想问我的注释是否正确并询问什么是deltaT

// To draw the arrows
void DrawClockHand(bool handLayer[PictureWidth][PictureHeight], float degree, float length)
{
    //To assure the length of arrows are enough to be displayed
    if (length > 1) return;

    // To properly position the arrows of the clock
    double StartPointx = PictureWidth / 2.0;
    double StartPointy = PictureHeight / 2.0;

    // To set the lenght of arrows
    double LineLengthx = StartPointx * length;
    double LineLengthy = StartPointy * length;

    //Setting the length of the arrows
    double LineLength = sqrt(LineLengthx * LineLengthx + LineLengthy * LineLengthy);

    //
    double deltaT = min(LineLengthx, LineLengthy) / max(LineLengthx, LineLengthy);

    //Variable declaration for loop
    int x, y;
    for (double t = 0; t <= LineLength; t += deltaT)
    {
        // The equation to set how the arrows position and move
        x = int(StartPointx + t * cos((degree - 0.25) * 2.0 * PI));
        y = int(StartPointy + t * sin((degree - 0.25) * 2.0 * PI));

        // To display the arrows
        handLayer[x][y] = true;
    }
}

仍然没有评论deltat。我不明白它的作用

对于那些想要查看完整代码并查看在何处使用的人 在这里

 // included libraries
#include <iostream>
#include <Windows.h>
#include <cmath>
#include <stdio.h>
#include <ctime>

// Definig the variables such as height and width to be used in functions
#define PI (22.0/7.0)
#define PictureWidth 27
#define PictureHeight 27

// Frames Variables
const int targetedFPS = 1;
const float idealDurationPerFrame = 1000.0f / targetedFPS;
float nextFrameAfter = 1, duration = 0;
clock_t startTime;
unsigned int framesCounter = 0;

// To set the characters to draw the circle
char symbolChar = 219;
char emptyChar = 32;

using namespace std;

// To reset/refresh the layer 
void ClearLayer(bool layer[PictureWidth][PictureHeight])
{
    for (int x = 0; x < PictureWidth; x++)
    {
        for (int y = 0; y < PictureHeight; y++)
        {
            layer[x][y] = false;
        }
    }
}

// To draw the clock 
void DrawCircle(bool layer[PictureWidth][PictureHeight])
{
    layer[0][10] = true;
    layer[0][11] = true;
    layer[0][12] = true;
    layer[0][13] = true;
    layer[0][14] = true;
    layer[0][15] = true;
    layer[0][16] = true;

    layer[1][8] = true;
    layer[1][9] = true;
    layer[1][17] = true;
    layer[1][18] = true;

    layer[2][6] = true;
    layer[2][7] = true;
    layer[2][19] = true;
    layer[2][20] = true;

    layer[3][5] = true;
    layer[3][21] = true;

    layer[4][4] = true;
    layer[4][22] = true;

    layer[5][3] = true;
    layer[5][23] = true;

    layer[6][2] = true;
    layer[6][24] = true;

    layer[7][2] = true;
    layer[7][24] = true;

    layer[8][1] = true;
    layer[8][25] = true;

    layer[9][1] = true;
    layer[9][25] = true;

    layer[10][0] = true;
    layer[10][26] = true;

    layer[11][0] = true;
    layer[11][26] = true;

    layer[12][0] = true;
    layer[12][26] = true;

    layer[13][0] = true;
    layer[13][26] = true;

    layer[14][0] = true;
    layer[14][26] = true;

    layer[15][0] = true;
    layer[15][26] = true;

    layer[16][0] = true;
    layer[16][26] = true;

    layer[17][1] = true;
    layer[17][25] = true;

    layer[18][1] = true;
    layer[18][25] = true;

    layer[19][2] = true;
    layer[19][24] = true;

    layer[20][2] = true;
    layer[20][24] = true;

    layer[21][3] = true;
    layer[21][23] = true;

    layer[22][4] = true;
    layer[22][22] = true;

    layer[23][5] = true;
    layer[23][21] = true;

    layer[24][6] = true;
    layer[24][7] = true;
    layer[24][20] = true;
    layer[24][19] = true;

    layer[25][8] = true;
    layer[25][9] = true;
    layer[25][18] = true;
    layer[25][17] = true;

    layer[26][10] = true;
    layer[26][11] = true;
    layer[26][12] = true;
    layer[26][13] = true;
    layer[26][14] = true;
    layer[26][15] = true;
    layer[26][16] = true;
}

// To properly position numbers such as 3,6,9,12 on the clock
void DrawClockNumbers(bool layer[PictureWidth][PictureHeight])
{
    //12
    layer[11][2] = true;
    layer[11][3] = true;
    layer[11][4] = true;
    layer[11][5] = true;
    layer[11][6] = true;

    layer[13][2] = true;
    layer[14][2] = true;
    layer[15][2] = true;
    layer[15][3] = true;
    layer[13][4] = true;
    layer[14][4] = true;
    layer[15][4] = true;
    layer[13][5] = true;
    layer[13][6] = true;
    layer[14][6] = true;
    layer[15][6] = true;

    //3
    layer[22][11] = true;
    layer[23][11] = true;
    layer[24][11] = true;
    layer[24][12] = true;
    layer[22][13] = true;
    layer[23][13] = true;
    layer[24][13] = true;
    layer[24][14] = true;
    layer[22][15] = true;
    layer[23][15] = true;
    layer[24][15] = true;

    //6
    layer[12][20] = true;
    layer[13][20] = true;
    layer[14][20] = true;
    layer[12][21] = true;
    layer[12][22] = true;
    layer[13][22] = true;
    layer[14][22] = true;
    layer[12][23] = true;
    layer[14][23] = true;
    layer[12][24] = true;
    layer[13][24] = true;
    layer[14][24] = true;

    //9
    layer[2][11] = true;
    layer[3][11] = true;
    layer[4][11] = true;
    layer[2][12] = true;
    layer[4][12] = true;
    layer[2][13] = true;
    layer[3][13] = true;
    layer[4][13] = true;
    layer[4][14] = true;
    layer[2][15] = true;
    layer[3][15] = true;
    layer[4][15] = true;

    //dot as the following numbers (1, 2, 4, 5, 7, 8, 10, 11)
    //1
    layer[17][4] = true;
    layer[17][5] = true;
    layer[18][4] = true;
    layer[18][5] = true;

    //2
    layer[21][7] = true;
    layer[21][8] = true;
    layer[22][7] = true;
    layer[22][8] = true;

    //4
    layer[21][18] = true;
    layer[21][19] = true;
    layer[22][18] = true;
    layer[22][19] = true;

    //5
    layer[17][21] = true;
    layer[17][22] = true;
    layer[18][21] = true;
    layer[18][22] = true;

    //7
    layer[8][21] = true;
    layer[8][22] = true;
    layer[9][21] = true;
    layer[9][22] = true;

    //8
    layer[4][18] = true;
    layer[4][19] = true;
    layer[5][18] = true;
    layer[5][19] = true;

    //10
    layer[4][7] = true;
    layer[4][8] = true;
    layer[5][7] = true;
    layer[5][8] = true;

    //11
    layer[8][4] = true;
    layer[8][5] = true;
    layer[9][4] = true;
    layer[9][5] = true;
}

///Deltat
//
void CombineLayersIntoFirst(bool firstLayer[PictureWidth][PictureHeight], bool secondLayer[PictureWidth][PictureHeight])
{
    for (int x = 0; x < PictureWidth; x++)
    {
        for (int y = 0; y < PictureHeight; y++)
        {
            if (firstLayer[x][y] || secondLayer[x][y])
            {
                firstLayer[x][y] = false;
            }
        }
    }
}

//To print the actual clock
void PrintClock(bool clockFrameLayer[PictureWidth][PictureHeight], bool clockNumbersLayer[PictureWidth][PictureHeight], bool hoursHandLayer[PictureWidth][PictureHeight], bool minutesHandLayer[PictureWidth][PictureHeight], bool secondsHandLayer[PictureWidth][PictureHeight], bool centerPointLayer[PictureWidth][PictureHeight])
{
    //Set the color on sides
    system("color 2F");

    //Nested for loop execution
    for (int y = 0; y < PictureHeight; y++)
    {
        for (int x = 0; x < PictureWidth; x++)
        {
            // Check for any of these boolean arrays 
            if (secondsHandLayer[x][y] || minutesHandLayer[x][y] || hoursHandLayer[x][y] || clockNumbersLayer[x][y] || clockFrameLayer[x][y] || centerPointLayer[x][y])
            {
                //Set the colors
                if (centerPointLayer[x][y]) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x04); //Set center point to Red
                else if (secondsHandLayer[x][y]) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x0f); //Set the colour for the second layer
                else if (minutesHandLayer[x][y]) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x09); //Set the colour for the minute layer
                else if (hoursHandLayer[x][y]) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x02); //Set the colour for the hour layer
                else if (clockNumbersLayer[x][y]) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x04); //Set the colour for clock number
                else if (clockFrameLayer[x][y]) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x4); //Set the colour for clock Around

                cout << char(symbolChar) << char(symbolChar); // To display the clock parts
            }
            else
            {
                cout << char(emptyChar) << char(emptyChar); // To avoid or fill the unneeded parts with spaces
            }
        }

        //Moves to next line
        cout << endl;
    }
}

// To draw the arrows
void DrawClockHand(bool handLayer[PictureWidth][PictureHeight], float degree, float length)
{
    //To assure the length of arrows are enough to be displayed
    if (length > 1) return;

    // To properly position the arrows of the clock
    double StartPointx = PictureWidth / 2.0;
    double StartPointy = PictureHeight / 2.0;

    // To set the lenght of arrows
    double LineLengthx = StartPointx * length;
    double LineLengthy = StartPointy * length;

    //Setting the length of the arrows
    double LineLength = sqrt(LineLengthx * LineLengthx + LineLengthy * LineLengthy);

    //
    double deltaT = min(LineLengthx, LineLengthy) / max(LineLengthx, LineLengthy);

    //Variable declaration for loop
    int x, y;
    for (double t = 0; t <= LineLength; t += deltaT)
    {
        // The equation to set how the arrows position and move
        x = int(StartPointx + t * cos((degree - 0.25) * 2.0 * PI));
        y = int(StartPointy + t * sin((degree - 0.25) * 2.0 * PI));

        // To display the arrows
        handLayer[x][y] = true;
    }
}

// To set the mid point of the clock
void DrawCenterPoint(bool layer[PictureWidth][PictureHeight])
{
    layer[PictureWidth / 2][PictureHeight / 2] = true;
}

// The equation to ensure it ticks and moves at the right time
///
float ConvertTimeToDegree(int time, int ticks, int timeFraction = 0, int timeFractionTicks = 1)
{
    return float(time) / float(ticks) + float(timeFraction) / (float(timeFractionTicks) * float(ticks));
}

// To display the clock in actual time 
void PrintDigitalClock(int seconds, int minutes, int hours)
{
    cout << "Clock: " << hours << ":" << minutes << ":" << seconds << endl;
}

//Print the framecounter and its duration
void PrintFramesLog()
{
    cout << "frame #" << framesCounter << ";   " << "frame duration: " << duration << " ms;   " << "sleep period: " << (idealDurationPerFrame - duration) << " ms;   running at: " << (1000.0f / (duration + max(0, (idealDurationPerFrame - duration)))) << " fps;   next frame after: " << nextFrameAfter << "; " << endl;
}

// get the time from the local system
void UpdateClockToCurrentTime(int& hours, int& minutes, int& seconds)
{
    time_t t = time(0);      // get time now
    struct tm* now = new tm; // intialize a struc
    localtime_s(now, &t);    // Converts a time_t time value to a tm structure, and corrects for the local time zone.

    // store the time in the var.
    seconds = now->tm_sec;
    minutes = now->tm_min;
    hours = now->tm_hour;

    //cout << (now->tm_year + 1900) << '-'
    //  << (now->tm_mon + 1) << '-'
    //  << now->tm_mday
    //  << endl;
}

int main()
{
    // Protype
    bool circleLayer[PictureWidth][PictureHeight];
    bool numbersLayer[PictureWidth][PictureHeight];
    bool secondsHandLayer[PictureWidth][PictureHeight];
    bool minutesHandLayer[PictureWidth][PictureHeight];
    bool hoursHandLayer[PictureWidth][PictureHeight];
    bool centerPointLayer[PictureWidth][PictureHeight];

    // Declaring variables to store the actual time
    int seconds = 0;
    int minutes = 0;
    int hours = 0;

    // loop for ever
    while (true)
    {
        // get the time at the start
        startTime = clock();

        // process the frame
        {
            //
            ///
            ClearLayer(circleLayer);
            ClearLayer(numbersLayer);
            ClearLayer(centerPointLayer);
            ClearLayer(secondsHandLayer);
            ClearLayer(minutesHandLayer);
            ClearLayer(hoursHandLayer);

            // To store the actual time in variables seconds, minutes , and hours
            UpdateClockToCurrentTime(hours, minutes, seconds);

            //Draw this parts of the circle
            DrawCircle(circleLayer);
            DrawClockNumbers(numbersLayer);
            DrawCenterPoint(centerPointLayer);
            DrawClockHand(secondsHandLayer, ConvertTimeToDegree(seconds, 60), 0.5f);
            DrawClockHand(minutesHandLayer, ConvertTimeToDegree(minutes, 60, seconds, 60), 0.4f);
            DrawClockHand(hoursHandLayer, ConvertTimeToDegree(hours, 12, minutes, 60), 0.3f);

            //Reset the screen
            system("cls");

            // To print the actual clock
            PrintClock(circleLayer, numbersLayer, hoursHandLayer, minutesHandLayer, secondsHandLayer, centerPointLayer);

            //Showcase the digital time/clock
            PrintDigitalClock(seconds, minutes, hours);
        }

        // find duration of the processed frame
        duration = (clock() - startTime);

        // find the next targeted frame
        nextFrameAfter = (duration / 1000.0f) + 1;

        // increase the frame counter
        framesCounter += nextFrameAfter;

        // To dispaly the frames
        PrintFramesLog();

        // frame process duration finished too early based on the targeted frames per seconds so sleep
        while (duration < idealDurationPerFrame)
        {
            duration = (clock() - startTime);
        }
    }

    // To pause the console
    system("pause");
    return 0;
}

0 个答案:

没有答案