Zarur ko‘nikmalar
Bu dasturni yozish uchun bir o‘lchovli massivlar ustida ishlashni yaxshi o‘zlashtirgan va ko‘p o‘lchovlilar bilan ishlash tamoyillarini bilishi kerak. Undan tashqari ixtiyoriy ko‘pburchaklarni shaklga chiqarish usullarini bilishi kerak.
Yechish Shakl
Bu dasturda shakl bevosita grafik chiqarish uchun maydon sifatida ishlatiladi. Shuning uchun ham u faqat boshqarish tugmachalaridan tashkil topadi. U tugmachalar yordamida sirtlarni aks ettirish, yangi tasodifiy koordinatalarni tanlash funksiyalari va avvalgi misollardagi tamoyil bo‘yicha ishlaydigan «Pusk/Pauza» tugmachalari ishlatilgan.
Ko‘ptamonlilarni shaklga to‘g‘ridan to‘g‘ri o‘tkazish
Blok sxema
Mazkur blok sxema sirtni approksimatsiya qiluvchi parallelepipedlar majmuini bevosita ekranga chiqarishni aniqlaydi. Bu protseduradagi uchinchi koordinata qiymatlarini aniqlovchi, ikki o‘lchovli Pole[20][20] massiv berilgan deyiladi.
Dastur kodi
#include
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
int Pole[20][20]; // Maydon
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void DrawPole()
{
TPoint P[4];
for(int y = 0; y < 20; y++)
for(int x = 19; x >= 0; x--)
{
P[0].x = x * 20 + y * 10 + 22;
P[0].y = y * 10 - Pole[x][y] + 202;
P[1].x = P[0].x + 16;
P[1].y = P[0].y;
P[2].x = P[0].x + 26;
P[2].y = P[0].y + 6;
P[3].x = P[0].x + 10;
P[3].y = P[0].y + 6;
TPoint P2[4] = {P[0],P[1],P[2],P[3]};
TPoint P1[4] = {P[0],P[1],P[2],P[3]};
P2[1].y = y * 10 + 256;
P2[1].x = P[2].x;
P2[0].y = y * 10 + 256;
P2[0].x = P[3].x;
P1[1].x = P[0].x;
P1[1].y = y * 10 + 250;
P1[2].x = P[3].x;
P1[2].y = y*10 + 256;
Form1->Canvas->Brush->Color = clBlue;
Form1->Canvas->Polygon(P2,3);
Form1->Canvas->Brush->Color = clRed;
Form1->Canvas->Polygon(P1,3);
Form1->Canvas->Brush->Color = clGreen;
Form1->Canvas->Polygon(P,3);
}
}
//---------------------------------------------------------------------------
void RandomPole()
{
for(int y = 0; y < 20; y++)
for(int x = 0; x < 20; x++)
{
Pole[x][y] = random(50)+10;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
for(int i = 0; i < 20; i++)
for(int j = 0; j < 20; j++)
{
Pole[i][j] = 0;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Form1->Refresh();
DrawPole();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
RandomPole();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
if(Button3->Caption == "Pauza")
{Button3->Caption = "Pusk";}
else
{Button3->Caption = "Pauza";}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
if(Button3->Caption == "Pauza")
{
RandomPole();
Form1->Refresh();
DrawPole();
}
}
Dostları ilə paylaş: |