3 Ways to Create Your Christmas Tree with C++ πππ
Including ways in the terminal and also with SFML and OpenGL.
Donβt judge the picture! =) This was one of the first Pixel Art I did with LibreSprite ! π
So, itβs Christmas! How about we take advantage of this unique date of the year to practice a little more of our knowledge in C++?
In this article we will see three ways to create our Christmas Tree with C++, including forms in the terminal and also with OpenGL and SFML. π
1st Form, as basic as possible
If we create a nested loop we can draw different geometric shapes, however, the most practiced is a triangle. In the code below, we managed to make a right triangle, that is, for our solution, it would be half of the problem solved.
Note that I put the separate function so that we can customize the size of the triangle we want
vim 01-christmas-tree.cpp
After compiling and running we will have the possible output below:
That is, only 4 basic lines! If you want it a little bigger, just change the drawTree() function to 1,10 or whatever number you want:
And then, we will have a slightly larger size:
But we really want to have an equilateral triangle, so weβll add two more loops inside the main loop with calculations that create the left part of the triangle and give space to the left, in addition to removing the min parameter so we can show the entire triangle, see the code to notice the difference:
Now itβs cool! We can still customize the size via argc and argv, but thatβs up to you!
2nd Way - A more stylish and animated Christmas Tree!
In this other form, weβre going to increment a few more ASCII characters to make it cooler, weβre also going to use the unistd.h library to get a lag(sleep) of 1 second when changing the animation .
In addition to adding COLORS! For this we will use a library that we created right here on the blog, for more details see the article: Create your own color library for C++ . The cool thing about this library is that it is simple and works well in LSP, see the gif below it informing each string, note that it informs: underscore, italics, normal, * *background** and among others:
That is, copy the post code and save it in a file named colors.hpp and leave it in the same directory as the code below!
The code in summary will be:
vim 02-christmas-tree.cpp
Compile and run: g++ 02-christmas-tree.cpp && ./a.out
The tree will possibly look like this:
3rd Way - Using C++ with SFML and OpenGL
And finally we have SFML and OpenGL, this example I found on the internet, I just made some small modifications!
Read the code for more details, but first prepare a directory for the files:
tree.hpp
tree.cpp
main.cpp
Makefile
Now just compile and run:
Output:
Really good, right?!
I wish Merry Christmas to all of you! π π π