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.

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

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

How To Learn The Move Constructors In Modern C++?

C++C++17Code SnippetLearn C++

How To Convert Char Array String To Unicode String Correctly In C++?

Artificial Intelligence TechC++Language FeatureLearn C++

How To Develop A Simple Hopfield Network In C++