Click here to Skip to main content
15,887,267 members
Articles / Programming Languages / C#
Tip/Trick

Rendering of Segmented Numbers

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
31 Jul 2015CPOL 12.4K   244   4   6
Explains how numbers can be rendered like segmented numbers (looking like old-school digital clock)

Introduction

Before a while, I needed to implement rendering of numbers in one project (C++ MFC), where it wasn't possible to simply use rendering of text using font. So I made the prototype in C# WinForms and maybe someone will find it useful.

Background

I'm using classical old-school approach -> 7-segments rendering of numbers.

Source Code

I've created SegmentRender class for rendering of segmented numbers.

At first, I've created Enum of segment type for better orientation.

C#
private enum Segment
{
    Up, LeftUp, RightUp, Middle, LeftDown, RightDown, Down
}

Here is simple 2D array [10 digits, 7 segments] for definition of segments used for each digit:

C#
static int[,] segValues =
{
       {1, 1, 1, 0, 1, 1, 1},
       {0, 0, 1, 0, 0, 1, 0},
       {1, 0, 1, 1, 1, 0, 1},
       {1, 0, 1, 1, 0, 1, 1},
       {0, 1, 1, 1, 0, 1, 0},
       {1, 1, 0, 1, 0, 1, 1},
       {1, 1, 0, 1, 1, 1, 1},
       {1, 0, 1, 0, 0, 1, 0},
       {1, 1, 1, 1, 1, 1, 1},
       {1, 1, 1, 1, 0, 1, 1}
};

Then I defined 3 functions:

C#
public void DrawNumber(int n, Graphics g, Point pos)

Here, the maximal order of our Int32 number is found:

C#
while (true)
    if (n < Math.Pow(10, ++maxOrder))
        break;

Then, each single digit is found and called to be rendered:

C#
for (int ord = 0; ord < maxOrder; ord++)
{
    int dig = n / Convert.ToInt32(Math.Pow(10, ord)) % 10;

    Point digPos = new Point
        (pos.X + (maxOrder - ord - 1) * (size + space), pos.Y);

    DrawDigit(dig, g, digPos);
}

The next function:

C#
private void DrawDigit(int n, Graphics g, Point pos)

In this function, all single digits are called to be rendered:

C#
for (int i = 0; i < 7; i++)
    if (segValues[n, i] == 1)
        DrawSegment((Segment)i, g, pos);

And the last rendering function:

C#
private void DrawSegment(Segment s, Graphics g, Point pos)

Here I've defined all segments points (x, y) and rendered these segments, which are for actual digit enabled in variable segValues.

That's all, simple enough. The rest of the code is only handling of the application form.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Czech Republic Czech Republic
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
SuggestionThis can be improved upon... Pin
Matt T Heffron7-Aug-15 13:53
professionalMatt T Heffron7-Aug-15 13:53 
AnswerRe: This can be improved upon... Pin
majaak9-Aug-15 8:54
majaak9-Aug-15 8:54 
GeneralRe: This can be improved upon... Pin
Matt T Heffron10-Aug-15 7:17
professionalMatt T Heffron10-Aug-15 7:17 
GeneralMy vote of 4 Pin
fredatcodeproject1-Aug-15 13:35
professionalfredatcodeproject1-Aug-15 13:35 
GeneralRe: My vote of 4 Pin
majaak2-Aug-15 22:54
majaak2-Aug-15 22:54 
GeneralRe: My vote of 4 Pin
fredatcodeproject3-Aug-15 4:34
professionalfredatcodeproject3-Aug-15 4:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.