Unit Unit1;
interface
uses System, System.Drawing, System.Windows.Forms;
type
Form1 = class(Form)
procedure button1_Click(sender: Object; e: EventArgs);
{$region FormDesigner}
internal
{$resource Unit1.Form1.resources}
label1: &Label;
label2: &Label;
textBox1: TextBox;
textBox2: TextBox;
button1: Button;
pictureBox1: PictureBox;
{$include Unit1.Form1.inc}
{$endregion FormDesigner}
public
constructor;
begin
InitializeComponent;
end;
end;
implementation
procedure Form1.button1_Click(sender: Object; e: EventArgs);
var
x, y, h, k, x0, xn: real;
x1, y1, n, c_x, c_y: integer; // Исправлено: yl заменено на y1
gr: Graphics;
bm: Bitmap;
P_C: Pen;
begin
// Создаем буфер для рисования под размеры PictureBox
bm := new Bitmap(PictureBox1.Width, PictureBox1.Height);
gr := Graphics.FromImage(bm);
gr.Clear(Color.White);
// Количество точек для расчета
n := 10000;
// Считываем границы интервала
x0 := StrToFloat(TextBox1.Text);
xn := StrToFloat(TextBox2.Text);
// Находим центр экрана
c_x := PictureBox1.Width div 2;
c_y := PictureBox1.Height div 2;
// Вычисляем масштабный коэффициент
k := PictureBox1.Width / (xn - x0);
h := (xn - x0) / n;
x := x0;
// Рисуем оси координат
P_C := new Pen(Color.Black, 1);
gr.DrawLine(P_C, 0, c_y, 2 * c_x, c_y);
gr.DrawLine(P_C, c_x, 0, c_x, 2 * c_y);
// Цикл расчета и построения графика функции y = x * sin(x)
for var i := 1 to n do
begin
y := x * sin(x);
// Перевод математических координат в экранные
x1 := trunc(x * k) + c_x;
y1 := trunc(-y * k) + c_y;
// Проверяем, попадает ли пиксель в границы PictureBox по вертикали
if (y1 >= 0) and (y1 < 2 * c_y) and (x1 >= 0) and (x1 < 2 * c_x) then
begin
bm.SetPixel(x1, y1, Color.Blue);
end;
// Шаг вперед по оси X (должен выполняться всегда)
x := x + h;
end;
// Отображаем готовый рисунок в PictureBox
pictureBox1.Image := bm;
end;
end.