The purpose of writing this topic is to introduce you to the wonderful language Processing . This PL cannot boast of wide functionality or rich expressive means, but it is able to offer something else ...
What is it?
So, processing is a sublanguage of programming based on java with a simple and clear C-like syntax. Processing makes it possible to quickly and easily create multimedia applications (in the terminology of processing - sketches). By the word multimedia, I mean language tools that allow you to develop graphics, animation, various visualizations, interactive applications ... In principle, nothing prevents even creating 3D applications (including games), because processing has the means to support OpenGL. All these features, coupled with a large number of functions and a very logical syntax, make this language ideal for learning and instilling interest in programming.
Well aware that one picture is worth a thousand words, I want to present a couple of examples of visualization created in processing:
')
How to start
Procesing 1.0 is free, open source, cross-platform software. The original archive includes the java-machine, the interpreter itself, a mini-IDE, and a few dozen examples. Versions for different platforms are available on the download page (all links are at the end of the topic). After downloading and unpacking the archive, you need to find in the root directory the executable file that runs the IDE, in which you can and should write code. Everything is ready to go!
Fast start
I did not set myself the task of teaching anyone the basics of this language. This is not necessary, because, in addition to the detailed manual, there are several articles in which it is very accessible, in simple English, all the basics and features of working with processing are explained. Everything is described in great detail, there are even illustrations. There is also an extensive community, represented by a forum with tens of thousands of posts. Therefore, I will focus on only a few points that newcomers should be aware of. To tell you the truth, I myself am still a complete newbie, but I have already learned something and I hasten to share it. So let's start with the main thing - with the syntax of the language.
In my opinion, he is very hike to the classic si. So if you have experience with languages such as C, PHP, JavaScript, etc, then we can assume that you practically know processing - many language constructs, operators, loops look exactly the same.
We will understand the terminology.
Sketch - the source file of your program. Sketchbook - a directory containing source files, resource files, etc. In short everything that relates to one project. PDE - Processing Development Environment. Native language development environment.
Once again about the possibilities
Before I begin to review code samples, I want to mention once again the possibilities that processing offers to us. So, at our disposal are tools for constructing graphic primitives, 3D-objects, working with light, text, and transformation tools. We can import and export audio / video / audio format files, handle mouse / keyboard events, work with third-party libraries (openGL, PDF, DXF), work with the network.
This is what PDE looks like in Windows XP:
The result of the program:
Write helloWorld
Finally, we got to the most important thing - the first example of the code. Analogous to the classic "hello, world" we will have this code:
line(25, 100, 125, 100);
What this function does, I think, needs no explanation. But just in case, I will tell you :) The line function takes four arguments and draws in a two-dimensional plane a line with the coordinates specified in the arguments, with a default color and thickness. Arguments in the order of use: x1, y1, x2, y2 - coordinates of the starting and ending points. Actually, almost all problems are solved in this language in the same simple ways. For 3D objects, of course, the Z axis is added.
Initial initialization
Although processing is a very simple language that allows a lot of liberties, but if we want to write a good program, then some agreements must be followed.
For example, all initialization functions: size () - window size, stroke () - color of lines, background () - background color, and some others, must be placed inside a special utility function void setup () . It is recommended to write it first.
The next utility function is void draw () . Its analogue can be called int main () in C ++. This feature is the basis for building any animation. Its peculiarity is that it is automatically called whenever the framebuffer is updated.
The last agreement is related to the positioning of objects in the coordinate plane. After initializing the window size with the setup () function, two global constants become available within the program - WIDTH and HEIGHT, in which the width and height of the window are stored, respectively. Every time you want to position, say, a circle in the center of the screen, use this entry:
ellipse (WIDTH / 2, HEIGHT / 2, 50, 50);
Here is an example of a small program written using agreements:
void setup () {
size (400, 400);
stroke (255);
background (192, 64, 0);
}
void draw () {
line (150, 25, mouseX, mouseY);
}
The mouseX and mouseY functions return the current coordinates of the mouse cursor. Thus, each time the mouse moves, new lines will be drawn. It looks like this:
At last
Anticipating possible exclamations, like "Yes, how is this processing better than the same Adobe Flash / Microsoft Silverlight etc ...". Firstly: it is a great free and open source alternative. Moreover, the result of your work can be converted into a java applet and pasted into a web page. Secondly: the developers themselves have already answered this question in their FAQ . I can not quote a paragraph from there:
It will be a bit better in processing. But fundamentally (and this cannot be emphasized enough), this is not an all-or-nothing game ... We're talking about tools. Do people refuse to use pencils because pens exist? No, you just use them for specific reasons. If processing works for you, then use it. If not, don't. It's easy! It's free! You're not being forced to do anything.
And really: these are different things, different tools, each of which is suitable for its own purposes. So no holivar, please. I repeat once again: processing is very well suited for training - there is nothing superfluous in it, and the result is available quickly and very visual. I consider this language not a means of serious development, but just an interesting hobby that allows you to relax and distract from the main work.