Making a Christmas Tree

Why make a Christmas tree in April? I don’t know but it’s harder than you might think, if you want a good one at least. To make a good Christmas tree you have to have at least four components the trunk, leaves, pot and baubles.

//Trunk
colour…


This content originally appeared on DEV Community and was authored by Sean Watkins

Why make a Christmas tree in April? I don't know but it's harder than you might think, if you want a good one at least. To make a good Christmas tree you have to have at least four components the trunk, leaves, pot and baubles.

Christmas tree drawn entirely with C.

//Trunk
    colour(150, 75, 10);
    rect(378, 150, 44, 425);

The trunk is simple, a brown coloured rectangle. To make brown with RGB you simple have to make orange, red plus a little green. Then just simply reduce the numbers to make brown, which is, to the best of my knowledge, a dark orange.

//Leaves
    for (int y = 500; y >= 100; y = y - 20) {
        int w = y / 2;
        int h = y / 3;
                int green = 250 - y * 200 / 500; 
        colour(0, green, 0);
        tri(400 - w / 2, y, 400 + w / 2, y, 400, y - h);
    }

The Leaves are not as simple, I did some special things to make the trees leaves look like a golden cyprus.

I am making the tree out of triangles. The loop counts backwards from y = 500 to y = 100, 20 pixels at a time. I am starting at 500, which is near the bottom of the 800 × 600 canvas.

I calculate the width w and height h of the triangle based on the y coordinate, so that the triangles get smaller towards the top of the tree.

To make the tree become gradually lighter closer to the top of the canvas, I vary the amount of green based on the y coordinate. The y coordinate ranges from 100 to 500. First divide by 500 to make it approximately between 0 and 1. Then multiply by 200 to put it on the RGB scale from 0 to 255. Subtract from 250 so that the amount of green ranges roughly between 250 for the light green at the top of the tree, and 50 for the dark green at the bottom of the tree.

//Pot
    colour(75, 37, 10);
    quad(360, 530, 440, 530, 425, 590, 375, 590);

    colour(90, 45, 12);
    rect(355, 528, 90, 10);

Now we have a basic pine tree but it doesn't have a place to live... So let's put it in a comically small pot! All we need to do is put a quadrilateral with a smaller base for the body of the pot and a skinny rectangle at the top to be the brim.

//BAUBLES
    srandom(6);
    for (int i = 0; i < 36; i++) {
        if (i % 2) {
            colour(255, 0, 0);
        } else {
            colour(255, 200, 0);
        }
        int x, y;
        int w;
        do {
            y = random() % 433 + 67;
            x = random() % 250 + 275;
            w = y / 2;
        } while (x < 400 - w/2 || x > 400 + w/2);
        circle(x, y, 10);
    }

Finally, we have to show that is isn't just a pine tree, so let's add some baubles. To be honest I didn't program this, my dad did, and I have no idea how this was done. So let's hand over to dad:

We want to draw 36 baubles, so we have a loop to count from i = 0 to 35. The odd numbered ones where i % 2 == 1 are red, and the even numbered ones are gold, but we can't make them shiny, at least not today.

We choose a random point in a rectangle that surrounds the tree, its "bounding box", then work out the width of the tree at that y position. If the point is to the left or right of the tree, it tries again with a new random point using a do ... while loop.

Finally, we draw a small circle of radius 10 pixels at the chosen point.

Here is the full code to draw the Christmas tree.


This content originally appeared on DEV Community and was authored by Sean Watkins


Print Share Comment Cite Upload Translate Updates
APA

Sean Watkins | Sciencx (2022-04-21T12:34:25+00:00) Making a Christmas Tree. Retrieved from https://www.scien.cx/2022/04/21/making-a-christmas-tree/

MLA
" » Making a Christmas Tree." Sean Watkins | Sciencx - Thursday April 21, 2022, https://www.scien.cx/2022/04/21/making-a-christmas-tree/
HARVARD
Sean Watkins | Sciencx Thursday April 21, 2022 » Making a Christmas Tree., viewed ,<https://www.scien.cx/2022/04/21/making-a-christmas-tree/>
VANCOUVER
Sean Watkins | Sciencx - » Making a Christmas Tree. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/04/21/making-a-christmas-tree/
CHICAGO
" » Making a Christmas Tree." Sean Watkins | Sciencx - Accessed . https://www.scien.cx/2022/04/21/making-a-christmas-tree/
IEEE
" » Making a Christmas Tree." Sean Watkins | Sciencx [Online]. Available: https://www.scien.cx/2022/04/21/making-a-christmas-tree/. [Accessed: ]
rf:citation
» Making a Christmas Tree | Sean Watkins | Sciencx | https://www.scien.cx/2022/04/21/making-a-christmas-tree/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.