如何在 C++ 中绘制函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15985118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to draw functions in C++
提问by alwaystudent
I would like to create charts of functions in C++. Can anyone help me in getting started ? Let's suppose I want to draw the function y=x ( it is a straight line passing through the points (0,0) (1,1) (2,2) ..... (20,20) etc. ) How could I do it ? ( If you also suggest how to draw the y & x axes it is even better ) Sorry but it is not on books ( good texts recommendations are welcome, by the way :-) C++ ) and on the net there are references mainly to game graphics. The only serious reference to my problem I was able to find is this: somewhat related question asked in this very forum( God bless, we have stackoverflow ! )
我想用 C++ 创建函数图表。任何人都可以帮助我入门吗?假设我想绘制函数 y=x (它是一条穿过点 (0,0) (1,1) (2,2) ..... (20,20) 等的直线)如何我可以吗?(如果您还建议如何绘制 y 和 x 轴,那就更好了)抱歉,但它不在书本上(顺便说一下,欢迎推荐好的文本:-) C++),并且在网上有主要是对游戏的参考图形。我能找到的对我的问题的唯一严肃参考是: 在这个论坛中提出了一些相关的问题(上帝保佑,我们有 stackoverflow !)
回答by Dr_Sam
I would consider two options:
我会考虑两种选择:
- If you want to do everything in C++, you will probably need an external library. OpenGL is a solution, there is also Qt, which is probably easier to learn and larger (and more powerful).
- If you can afford to output a file and read it with another program, the easiest way is to simply write an ASCII file with the coordinates of the points and plot is with Octave(free).
回答by BenC
You could also use Gnuplot, an interactive plotting program: your C++ code handles and computes your data, and then it can export the result to a file or the standard output that can be processed by Gnuplot. For instance, you can do something like this:
您还可以使用Gnuplot,一个交互式绘图程序:您的 C++ 代码处理和计算您的数据,然后它可以将结果导出到文件或 Gnuplot 可以处理的标准输出。例如,您可以执行以下操作:
my_program | gnuplot
my_program | gnuplot
which will call Gnuplot with the result of your program.
它将使用程序的结果调用 Gnuplot。
Gnuplot is very powerful, you can check the examples available here.
(source: sourceforge.net)
(来源:sourceforge.net)
You can find additional information on Gnuplot in C/C++ here:
您可以在此处找到有关 C/C++ 中 Gnuplot 的其他信息:
How to plot graphs in Gnuplot in Real time in C++?
UPDATE
更新
Another possibility is to use R. This language is very famous among statisticians. You may want to look at some examples.
另一种可能性是使用R。这种语言在统计学家中非常有名。您可能想查看一些示例。
Related question: How to run plot of R into C/C++?
回答by BenC
I use ROOT. It's what most particle physicists use, particularly at CERN and Fermilab. It's free. They have versions for Linux, Mac, and Windows. http://root.cern.ch/drupal/
我用ROOT。这是大多数粒子物理学家使用的,尤其是在欧洲核子研究中心和费米实验室。免费。他们有适用于 Linux、Mac 和 Windows 的版本。 http://root.cern.ch/drupal/
It's easy to make plots, histograms and produce output suitable for publication.
制作图表、直方图和生成适合发表的输出很容易。
Edit: It's built on C++ but has python bindings as well.
编辑:它建立在 C++ 上,但也有 python 绑定。
Edit: If root is install on linux to compile code using ROOT with GCC do e.g:
编辑:如果在 linux 上安装 root 以使用 ROOT 和 GCC 编译代码,请执行例如:
g++ foo.C -o foo `root-config --cflags --libs` -O3
回答by Tyler Jandreau
I had this same question about three months ago. The best way I found to plot for my application was utilizing the Qt
framework (because it already creates a GUI for you, and then leveraging this tool:
大约三个月前我也有同样的问题。我发现为我的应用程序绘图的最佳方法是利用Qt
框架(因为它已经为您创建了一个 GUI,然后利用这个工具:
This widget, when compiled and linked with your project, allow you to plot 2d graphs directly from your interface. This saves you the trouble of writing to a file, loading with matlab, etc.
这个小部件在编译并与您的项目链接时,允许您直接从您的界面绘制二维图形。这样可以省去写入文件、用 matlab 加载等的麻烦。
Currently, it only handles 2 d plots, but it wouldn't be difficult to extend it to three dimensions or maybe a surface plot.
目前,它只处理二维图,但将其扩展到三个维度或曲面图并不困难。
Best of luck!
祝你好运!
回答by Zanven
i dont know of a effective fast and simple method to do what you describe in c++. you will either be outputting text to a console to show the effect of the "lines" you wish to display. or setup a proper GUI interface with either directx or opengl. Many librarys do exsist which simplify the task of setting up a GUI. but there is initial overhead.
我不知道一种有效的快速和简单的方法来执行您在 C++ 中描述的操作。您要么将文本输出到控制台以显示您希望显示的“线条”的效果。或使用 directx 或 opengl 设置适当的 GUI 界面。许多库确实存在,它们简化了设置 GUI 的任务。但有初始开销。
i would suggest making a c# form application and looking into drawing lines within that. it would also easily allow you to put equations in at run time rather then hard coding them.
我建议制作 ac# 表单应用程序并查看其中的绘图线。它还可以轻松地让您在运行时输入方程,而不是对其进行硬编码。
回答by alwaystudent
In these days I discovered a couple of things: 1. the book "introduction to C++ programming and graphics" C.Pozrikidis 2. Visual Studio C++ 2010 express and OpenGL( see the last comment in that page ) Can anyone suggest better solutions ? ( textbooks, free dlls etc. )
在这些日子里,我发现了几件事: 1. “C++ 编程和图形简介”一书 C.Pozrikidis 2. Visual Studio C++ 2010 express 和 OpenGL(请参阅该页面的最后一条评论) 谁能提出更好的解决方案?(教科书、免费 dll 等)
回答by IcyFlame
You can use some functions that are there in Windows.h. Start with the console functions. They are a good place to start and enable us to work on the console as if it were a Cartesian plane.
您可以使用 Windows.h 中的一些函数。从控制台功能开始。它们是一个很好的起点,使我们能够在控制台上工作,就好像它是笛卡尔平面一样。
Though I have never tried to draw something with them. I have been able to create nice tables and layouts for text presentation. Like the name and telephone number of a contact as a table in a contacts application.
虽然我从来没有试图用它们画什么东西。我已经能够为文本演示创建漂亮的表格和布局。像联系人的姓名和电话号码一样作为联系人应用程序中的表格。
The functions enable moving the cursor to specific coordinates. And write something there. And overall they quite work like a Cartesian plane. But only the coordinate system is not the normal one which we use(4 quadrants) it is more of a computer type system with only one quadrant and labelled from left o right on the x axis and from up to down on the y axis. You may have to build some functions so as to optimise this or the normal system that we use.
这些功能可以将光标移动到特定坐标。并在那里写点东西。总的来说,它们就像笛卡尔平面一样工作。但只有坐标系不是我们使用的普通坐标系(4 个象限),它更像是一个只有一个象限的计算机类型系统,在 x 轴上从左到右标记,在 y 轴上从上到下标记。您可能需要构建一些函数来优化这个或我们使用的正常系统。
But all in all, it is useful and you can try it.
但总而言之,它很有用,您可以尝试一下。
回答by alwaystudent
I installed gnuplot ( very easy: create a folder in the hard disk, unzip and send a link to the destop from wgnuplot.exe ) and now I export the data to be plotted from Visual Studio ( C++ ) in .txt files and then I call those files from the gnuplot window. Just to save to others the same waste of time and headache I experienced :-)
我安装了 gnuplot(非常简单:在硬盘中创建一个文件夹,解压缩并从 wgnuplot.exe 发送一个指向 destop 的链接),现在我将要从 Visual Studio(C++)绘制的数据导出到 .txt 文件中,然后我从 gnuplot 窗口调用这些文件。只是为了向其他人节省我经历过的同样浪费的时间和头痛:-)
I have also installed ROOT, very easy, there is a regular Microsoft installer in the cern page. Here is the link: http://root.cern.ch/drupal/content/production-version-534and scroll to the bottom of the page then select the MSI version compatible with your Visual Studio. I must still learn how to use ROOT from Visual Studio, anyway. Help would be appreciated. There is also this page helping a lot with the creation of projects in Visual Studio using ROOT: http://root.cern.ch/phpBB3/viewtopic.php?f=3&t=11641
我也安装了ROOT,很简单,cern页面里有一个普通的微软安装程序。这是链接:http: //root.cern.ch/drupal/content/production-version-534并滚动到页面底部,然后选择与您的 Visual Studio 兼容的 MSI 版本。无论如何,我仍然必须学习如何从 Visual Studio 使用 ROOT。帮助将不胜感激。还有这个页面对使用 ROOT 在 Visual Studio 中创建项目有很大帮助:http://root.cern.ch/phpBB3/viewtopic.php?f=3&t= 11641
All of the above is for WINDOWS users.
以上所有内容均适用于 WINDOWS 用户。
回答by Fernando Fiore
i was just plotting a simmilar function y=x2
in windows, or any library based on windows you will probably intercept message WM_PAINT
case WM_PAINT:
HDC hdc = beginpaint();
// change device contexts hdc y axys orientation
// change logical coordinates of point (0,0) to half of width and half of //eight of your client area
// u might want to change from pixels to mm
// for each x from -10 to 10 y = x2
// draw a little line for each point in this range
// end painting, this will release hcd