C++Learn C++

Quickly Learn To Draw Powerful Mathematical Functions Dynamically in Modern C++ on Windows

Most of engineering applications needs to display data in part of time or in real time. There are many ways to draw mathematical functions or data series. There are also components which makes easy to display, zoom, arrange, save or load data. In this post we will explain what is the best way to draw mathematical functions.

Modern way to display drawings in C++ is using these Bitmaps. Bitmaps has Canvas property to draw, you can use ARGB format to draw pixels on it. You can also use alpha background, your drawings may be easily displayed over other images, textures that makes your drawings more professional. In this example we will use bitmap to draw mathematical functions. In C++ Builder, basically in technique we draw things into Bitmap (TBitmap) and we display all the bitmap by using Image (Tımage).

To draw your functions in line mode you just need DrawLine(…) command as shown below;

To draw your functions with solid bars, you need FillRect(…) command,

You can use Ellipse and Text drawing on your graphics too. For more details please read this “How to use Canvas in C++ to do Drawings“.

If we have lines between every two points , so we can draw as below;

With these information, now lets start building our drawing application.

An Example to Draw Mathematical Functions Dynamically on Windows

  1. Create a new Mutli-Device C++ Builder Project, Save all project and unit files to a folder (i.e.MyMathDraw)
  2. Add an Image from Component Palette, Set Width 800 and Height 400. Set WrapMode to Original, so each pixels will be shown on your screen pixels ( no shrink / resize).
  3. Add a Trackbar, we will use this to modify points generated by our function
  4. Add a Trackbar, we will use this to modify points generated by our function.
  5. Add a Timer component from Component Palette , we will use this to set new parameters with out function (these parameters may be obtained by a sensor or from a file too) and we will draw these parameters on ourImage bitmap;
  6. Press F12 and Let start with defining our point array and bitmap. In this example we will have 100 points (-0 to 99) and we will draw

6. Now lets define our y=f(x) function. Actually this is a y=Sin(x) function where x is in degrees, to draw this we should modify to have arranged position as given below.

This function changes with x and the Value of our TrackBar1; You can add more Trackbars, Edits etc. to retrieve parameters and modify your functions.

6. Double click to Timer to create OnTimer() event, and add these. This will set our function and it will display by timer intervals.

8. Run your codes by pressing F9, your application will show your function like this.


Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

Related posts
C++C++11C++14C++17Introduction to C++Learn C++

Learn about C++ Encapsulation

C++C++11C++14C++17Introduction to C++Learn C++

Discover Class Methods in C++

C++Learn C++Videos

Learn C++ With Monoids, Monads, and Applicative Functors: Repeated Software Patterns (CPPCon 2020) Video

C++Learn C++Videos

Learn C++ With Pipes: How Plumbing Can Make Your C++ More Expressive by Jonathan Boccara (CPPCon 2020) Video