You are here:

C++/draw a graph


Followup To
Question -
how can i draw the graph of two non_equal linear equation if i know there objective function.i can work with visual c++,& i can not use MFC.
draw ax+by=c
the objective function is:
Answer -
Hello yosr eman, thank you for the question.

You can accomplish this very easily. Create a Windows win32 application. Create your window and get the handle to it. Use the handle to get the device context(GetDC()). Then use the handle to the device context to start coloring points with SetPixel(). SetPixel() takes an x value, y value, and a COLORREF. Now before you draw, process your input however you may do it, plug the x and y you get from input(or you could just use hard coded x's and y's) into a macro that has your formula in it, then call SetPixel().

See, it's not so bad. If you have any other questions please don't hesistate to ask me. I am superior with the Windows GDI.

I hope this information was helpful.

- Eddie
thx alot about this informatios but i am sorry i have ever open win 32 application i always open win 32 consol application then open c++ source file can i draw by using this way & if i must work by openning win 32 application could u help me by sending the steps which i should folow.
thx alot

Hello yosr eman, thank you for the question.

If they told you that you cannot use MFC, then I see no reason why you can't make a standard windows application. Under no circumstances should anything be drawn in a console application. Console apps are for easy input/output to the screen, not for drawing.

Here are the steps for creating a windows application.

Open the compiler. Go to File->New->Project. Select Win32, name the project, and select the CloseSolution tab to close your current solution(if any).

Click OK. Click Finish.

You are going to want to do your drawing in your main Windows message loop. Go to the file named the same as your project + .cpp. Find this code:

// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))


You catch the handle of your window in the function InitInstance(). It would probably be easiest to store it in a global variable. Don't forget to call ReleaseDC() right after your message loop, otherwise you will leak memory.

If you have any other questions, please don't hesistate to ask me.

I hope this information was helpful.

- Eddie


All Answers

Answers by Expert:

Ask Experts




I can answer questions about the C++ language, object oriented design and architecture. I am knowledgable in a lot of the math that goes into programming, and am certified by I also know a good deal about graphics via OpenGL, and GUIs.


I have completed numerous games and demos created with the C++ programming language. Currently employed as a software engineer in the modeling and simulation field. I have about 7 years experience.

©2017 All rights reserved.