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.

close

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.

About author

Dr. Yilmaz Yoru has 33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He was born in 1974, Eskisehir-Turkey, started coding in college and graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. He worked as a research assistant at the same university for more than 10 years. He received his MSc and PhD degrees from the same department at the same university. Since 2012, he is the founder and CEO of Esenja LLC Company. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17Learn C++Templates

Learn How To Eliminate Pointers In Templates in C++

C++C++11C++14C++17Learn C++Templates

Learn To Export And Import Template Members From A DLL Or A Package In C++

C++C++11C++14C++17Learn C++Templates

What Is A Function Template In C++?

C++C++11C++14C++17Learn C++Templates

What Is A Class Template In C++?