TX Library Help – Version: 00173a, Revision: 174
 ALL  Windows graphics in a sandbox

Tree.cpp

См. документацию.
00001 //{=========================================================================== 
00008 //          (C) Кирилл Кулешов, 7 класс, 2010
00009 //}===========================================================================
00010 
00011 #include "TXLib.h"
00012 
00013 //----------------------------------------------------------------------------
00014 
00015 void DrawTree (double x, double y, double length, double angle);
00016 double Random (double min, double max);
00017 
00018 //----------------------------------------------------------------------------
00019 
00020 int main ()
00021     {
00022     txCreateWindow (800, 600);
00023     txBegin();
00024 
00025     while (!GetAsyncKeyState (VK_ESCAPE))
00026         {
00027         txSetFillColor (TX_BLACK);
00028         txClear();
00029 
00030         DrawTree (400, 0, 100, txPI/2);
00031 
00032         txSleep (100);
00033         }
00034 
00035     txEnd();
00036     return 0;
00037     }
00038 
00039 //----------------------------------------------------------------------------
00040 
00041 void DrawTree (double x, double y, double length, double angle)
00042     {
00043     if (length <= 0) return;
00044 
00045     double wind = 0;
00046     int  height = 0;
00047 
00048     if (GetAsyncKeyState (VK_LEFT))  wind   =               +0.2;
00049     if (GetAsyncKeyState (VK_RIGHT)) wind   =               -0.2;
00050     if (GetAsyncKeyState (VK_SHIFT)) wind   = Random (-0.7, +0.7);
00051     if (GetAsyncKeyState (VK_UP))    height =               +5;
00052     if (GetAsyncKeyState (VK_DOWN))  height =               -5;
00053 
00054     double leaves = 0;
00055     if (height ==  0) leaves = 20;
00056     if (height ==  5) leaves = 25;
00057     if (height == -5) leaves = 15;
00058 
00059     txSetColor ((length > leaves)? RGB (115, 65, 10) : RGB (35, 255, 10),
00060                 (int) (pow (length/7, 0.9) + 0.5));
00061 
00062     // Here comes the tree
00063                 
00064     double x1 = x + length * cos (angle),
00065            y1 = y + length * sin (angle);
00066 
00067     txLine (x, 600 - y, x1, 600 - y1);
00068 
00069     DrawTree (x1, y1, length - 10 + Random (-1, +1) + height, angle + 0.35 + Random (-0.2, +0.2) + wind);
00070     DrawTree (x1, y1, length - 10                           , angle - 0.35 - Random (-0.2, +0.2) + wind);
00071     }
00072 
00073 //----------------------------------------------------------------------------
00074 
00075 inline double Random (double min, double max)
00076     {
00077     return min + (max - min) * rand() / RAND_MAX;
00078     }