Лабораторная работа № 2. Отображение графиков функций средствами C++ Builder
1. Сохраняем проект (Project_graphics, Unit_graphics_interface), добавляем компоненты Button, ComboBox, Edit, PaintBox, StaticText, TrackBar, как показано на форме.
|
2. Меняем стандартные свойства компонентов (в скобках после названия приводится новое свойство Name).
Form1 (Form_Main): Caption: " Отображение графиков функций средствами C++ Builder "
Button1 (Button_Draw_Axes): Caption: "Рисовать оси"
ComboBox1 (ComboBox_Formula_Coord_X): Items: const a * t + b a * (t ^ b + c) a * sin (b * t + c) a * cos (b * t + c) a * tg (b * t + c) a * ctg (b * t + c) a * e ^ (b * t + c) a * sin (b * t) * sin (c * t) a * sin (b * t) * cos (c * t) a * cos (b * t) * cos (c * t) a * e ^ (b * t) * sin (c * t) Text: "Формула первой координаты"
ComboBox2 (ComboBox_Formula_Coord_Y): Items: (в точности то же, что и для ComboBox_Formula_Coord_X) Text: "Формула второй координаты"
Edit1, Edit2, Edit4, Edit5 (Edit_Coord_X_Coeff_a, Edit_Coord_X_Coeff_b, Edit_Coord_Y_Coeff_a, Edit_Coord_Y_Coeff_b): Text: "1"
Edit3, Edit6 (Edit_Coord_X_Coeff_c, Edit_Coord_Y_Coeff_c): Text: "0"
Edit7, Edit8 (Edit_Scale_X, Edit_Scale_Y): Text: "10"
PaintBox1 (PaintBox_Output): Height: 465 Width: 681
StaticText1: Caption: "X" StaticText2: Caption: "Y" StaticText3, StaticText6: Caption: "a" StaticText4, StaticText7: Caption: "b" StaticText5, StaticText8: Caption: "c" StaticText9: Caption: "deltaX" StaticText10: Caption: "deltaY"
TrackBar1, TrackBar2 (TrackBar_Scale_X, TrackBar_Scale_Y): Frequency: 9 Max: 100 Min: 1 Position: 10
3. Добавляем код обработки событий путем двойного нажатия мыши на вкладке Events соответствующего компонента, например для ввода события:
void __fastcall TForm_Main::TrackBar_Scale_XChange(TObject *Sender)
{
Edit_Scale_X->Text = TrackBar_Scale_X->Position;
Graphic->SetDeltaX(TrackBar_Scale_X->Position);
Graphic->DrawAxis();
Graphic->DrawGraphic();
}
выбираем компонент TrackBar_Scale_X, переходим на вкладку Events инспектора объектов, находим событие OnChange, дважды щелкаем мышью по пустому полю справа, и копируем то, что находится внутри фигурных скобок {…}.
Код событий в файле Unit_Graphics_Interface.cpp
//---------------------------------------------------------------------------
__fastcall TForm_Main::TForm_Main(TComponent* Owner)
: TForm(Owner)
{
Graphic = new GraphicsClass;
}
//---------------------------------------------------------------------------
void __fastcall TForm_Main::FormDestroy(TObject *Sender)
{
if(Graphic != NULL)delete Graphic;
}
//---------------------------------------------------------------------------
void __fastcall TForm_Main::Button_Draw_AxesClick(TObject *Sender)
{
Graphic->DrawAxis();
}
//---------------------------------------------------------------------------
Void __fastcall tForm_Main::TrackBar_Scale_xChange(tObject *Sender)
{
Edit_Scale_X->Text = TrackBar_Scale_X->Position;
Graphic->SetDeltaX(TrackBar_Scale_X->Position);
Graphic->DrawAxis();
Graphic->DrawGraphic();
}
//---------------------------------------------------------------------------
void __fastcall TForm_Main::TrackBar_Scale_YChange(TObject *Sender)
{
Edit_Scale_Y->Text = TrackBar_Scale_Y->Position;
Graphic->SetDeltaY(TrackBar_Scale_Y->Position);
Graphic->DrawAxis();
Graphic->DrawGraphic();
}
//---------------------------------------------------------------------------
void __fastcall TForm_Main::ComboBox_Formula_Coord_XChange(TObject *Sender)
{
Graphic->SetXKind(ComboBox_Formula_Coord_X->ItemIndex);
Graphic->DrawAxis();
Graphic->DrawGraphic();
}
//---------------------------------------------------------------------------
void __fastcall TForm_Main::ComboBox_Formula_Coord_YChange(TObject *Sender)
{
Graphic->SetYKind(ComboBox_Formula_Coord_Y->ItemIndex);
Graphic->DrawAxis();
Graphic->DrawGraphic();
}
//---------------------------------------------------------------------------
void __fastcall TForm_Main::Edit_Coord_X_Coeff_aChange(TObject *Sender)
{
if (Edit_Coord_X_Coeff_a->Text == "")
Edit_Coord_X_Coeff_a->Text = "0";
Graphic->SetAX(Edit_Coord_X_Coeff_a->Text.ToDouble());
Graphic->DrawAxis();
Graphic->DrawGraphic();
}
//---------------------------------------------------------------------------
void __fastcall TForm_Main::Edit_Coord_X_Coeff_bChange(TObject *Sender)
{
if (Edit_Coord_X_Coeff_b->Text == "")
Edit_Coord_X_Coeff_b->Text = "1";
Graphic->SetBX(Edit_Coord_X_Coeff_b->Text.ToDouble());
Graphic->DrawAxis();
Graphic->DrawGraphic();
}
//---------------------------------------------------------------------------
void __fastcall TForm_Main::Edit_Coord_X_Coeff_cChange(TObject *Sender)
{
if (Edit_Coord_X_Coeff_c->Text == "")
Edit_Coord_X_Coeff_c->Text = "0";
Graphic->SetBX(Edit_Coord_X_Coeff_c->Text.ToDouble());
Graphic->DrawAxis();
Graphic->DrawGraphic();
}
//---------------------------------------------------------------------------
void __fastcall TForm_Main::Edit_Coord_Y_Coeff_aChange(TObject *Sender)
{
if (Edit_Coord_Y_Coeff_a->Text == "")
Edit_Coord_Y_Coeff_a->Text = "0";
Graphic->SetBX(Edit_Coord_Y_Coeff_a->Text.ToDouble());
Graphic->DrawAxis();
Graphic->DrawGraphic();
}
//---------------------------------------------------------------------------
void __fastcall TForm_Main::Edit_Coord_Y_Coeff_bChange(TObject *Sender)
{
/*
TEdit* edit = (TEdit*)Sender;
if (edit->Text == "")
edit->Text = "0";
Graphic->SetBX(edit->Text.ToDouble());
Graphic->DrawAxis();
Graphic->DrawGraphic();
*/
if (Edit_Coord_Y_Coeff_b->Text == "")
Edit_Coord_Y_Coeff_b->Text = "1";
Graphic->SetBX(Edit_Coord_Y_Coeff_b->Text.ToDouble());
Graphic->DrawAxis();
Graphic->DrawGraphic();
}
//---------------------------------------------------------------------------
void __fastcall TForm_Main::Edit_Coord_Y_Coeff_cChange(TObject *Sender)
{
if (Edit_Coord_Y_Coeff_c->Text == "")
Edit_Coord_Y_Coeff_c->Text = "0";
Graphic->SetBX(Edit_Coord_Y_Coeff_c->Text.ToDouble());
Graphic->DrawAxis();
Graphic->DrawGraphic();
}
//---------------------------------------------------------------------------
4. Дополните код Unit_graphics_interface.h объвлением класса Graphic (выделено полужирным шрифтом):
//---------------------------------------------------------------------------
#ifndef Unit_graphics_interfaceH
#define Unit_graphics_interfaceH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ComCtrls.hpp>
#include <ExtCtrls.hpp>
//---------------------------------------------------------------------------