using System;
using System.Drawing;
using System.Windows.Forms;

namespace _12_5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public static Bitmap DrawLineDDA(int x_0, int y_0,
                                         int x_1, int y_1)
        {
            Bitmap image = new Bitmap(400, 400);
            int x0 = x_0, y0 = y_0, x1 = x_1, y1 = y_1;

            if (x0 > x1)
            {
                x0 = x_0;
                y0 = y_0;
                x1 = x_1;
                y1 = y_1;
            }

            int dx = x1 - x0, dy = y1 - y0, step, k;
            float xIncrement, yIncrement, x = x0, y = y0;

            if (Math.Abs(dx) > Math.Abs(dy))
                step = Math.Abs(dx);
            else
                step = Math.Abs(dy);

            xIncrement = (float)dx / (float)step;
            yIncrement = (float)dy / (float)step;

            image.SetPixel((int)Math.Round(x), (int)Math.Round(y),
                           Color.Black);

            for (k = 0; k < step; k++)
            {
                x += xIncrement;
                y += yIncrement;
                image.SetPixel((int)Math.Round(x), (int)Math.Round(y),
                               Color.Black);
            }

            return image;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int x0 = Convert.ToInt32(textBox1.Text);
            int y0 = Convert.ToInt32(textBox2.Text);

            int x1 = Convert.ToInt32(textBox3.Text);
            int y1 = Convert.ToInt32(textBox4.Text);

            pictureBox1.Image = DrawLineDDA(x0, y0, x1, y1);
        }
    }
}