How do math-like programs draw graphs and how can I make such a program?

I was wondering how programs such as mathematics and mathematics, etc., grace function graphs so gracefully and quickly. Can someone explain to me how they do it, and besides, how can I do it? Is this related to an aspect or course in computer programming or mathematics? What then?

+8
math wolfram-mathematica matlab graphics discrete-mathematics
source share
4 answers

Ok, with some support from belisarius, here is my comment as an answer: try a look at matplotlib . On the home page:

matplotlib is a python-based 2D graphics library that provides publication quality metrics in various formats on print media and in interactive environments across platforms. matplotlib can be used in python scripts, python and ipython shell (ala MATLAB® * or Mathematica®), web application servers and six GUI tools.

Initially, he was inspired by the possibilities of building MATLAB, although he has grown a lot since then. This is reliable software - and it is open source under the BSD license, so you can not only read the source code, but also hack it and use it anywhere.

Another place you can see is gnuplot . This is not one of the common open source licenses, but certainly open source, with some permissions to modify, etc.

Gnuplot is a portable command line graphical utility for Linux, OS / 2, MS Windows, OSX, VMS and many other platforms. The source code is protected by copyright, but freely distributed (i.e. you do not need to pay for it). It was originally created to allow scientists and students to interactively display math functions and data, but has grown to support many non-interactive applications such as web scripting. It is also used as a graphics engine by third-party applications such as Octave. Gnuplot has been supported and actively developed since 1986.

He also does 3D graphics, which matplotlib does not, and he has been around a lot longer. The reason I thought about matplotlib is because it is intended as a library for a higher level language, not a standalone application, so I assume it will be easier for you to read.

Another suggestion, just to get an idea of ​​the things Mathematica is doing under the hood, is to look at the documentation for Plot . In particular, if you look at the available options, you can print everything.

MaxRecursion Automatic maximum number of recursive units is allowed. Method Automatic method used to refine PerformanceGoal curves $PerformanceGoal aspects to try to optimize the PlotPoints Automatic initial number of sample points

From MaxRecursion and PlotPoints you can see that it performs the initial sampling, and then somehow decides which regions need to be divided (resampled) to get the exact view of the plot. And from there, it's magic: for this there is Method , and PerformanceGoal is ...

+5
source share

For MATLAB, due to the cross-platform requirement, there are no alternatives like using OpenGL . The MATLAB runtime is written in C ++, and the non-segmented GUI uses Java Swing. Therefore, MATLAB Plot is probably a mixture of C ++ / OpenGL / Swing.

In fact, MATLAB graphics are much less complex than video game graphics. I think it’s easier to find tutorials on video game graphics and then “reduce them” to MATLAB functionality, for example, draw one line with the same color.

The most important concept is probably the Transformation Matrix .

+1
source share

Basically, most programs that build a graph of any type (in particular, any graph of reasonable complexity) will use some types of third-party libraries.

The specific library used will depend on the programming language used. For example:

For a .Net application, you can use Crystal reports. http://en.wikipedia.org/wiki/Crystal_Reports

For Java, you can use JFreeChart. http://www.jfree.org/jfreechart/ And so on ...

You will probably find many libraries for any language in which you decide to enter the code.

If you want to perform this function in your specific project, I suggest using the library, especially if you are a beginner. The internal difficulties of implementing these graph libraries would be significant due to many problems, such as cross-platform compatibility, optimization of graphic rendering (that is, providing fast visualization of graphics and “beautifully”), mathematics associated with positioning elements on a graph and etc.

Finally, I doubt that you will find specific courses on this subject (or require them), since once again excluding VERY specific cases, programmers will always use existing libraries.

Why code it when someone has already solved the problem for you?

+1
source share

A good place to start is to understand that there is a grammar of the graph, and what you want to build after receiving the plot command is a symbolic representation of the graph. For Mathematica, you can do something like

 FullForm[Plot[Sin[x], {x, 0, 2 Pi}]] 

to see the internal representation that Mathematica uses. Basically, you need to describe the line segments (2D) or grids (3D) that you want to draw in terms of their color and coordinates. In addition, information is needed on the scale of the graph and methods for applying labels, label axes, etc.

This leads us to the main question, how do you define the line segment that you want to draw from the function and range? If you delve into the help file for the plot, you see several things. First there is the plot point option and the MaxRecursion option. It makes me believe (and this is just an educated guess, but that’s how I would do it) that Mathematica calculates the initial number of points on a uniform interval over the range to get the initial value. The next part is to identify regions where the changes exceed a certain threshold, and then try to score more points until the “change” between any two points of your line segment is below the threshold value. Mathematica does this recursively, so the option is MaxRecursion.

Until now, I have vaguely determined the rate of change. A more useful way to describe the change is to take 3 points in your line segment. Suppose that there is a linear relationship between the 1st and 3rd points and, assuming this linear connection, make a prediction about what will be the 2nd point. If the error of this prediction is low enough, consider the next group of three points. If the error is above the threshold, you should try a few more points in this area until the threshold is reached. Thus, you will need relatively small points where the curve is relatively straight and larger in the “interesting” parts, where it bends in new directions. The smoothness of the curve you draw will be proportional to the error you are willing to tolerate in linear point prediction.

0
source share

Source: https://habr.com/ru/post/651395/


All Articles