The document describes an algorithm to draw a line between two points using a direct line algorithm. It involves dragging and dropping points onto a form, clicking a button to draw the line, and using the line equation y=mx+b to calculate the y-coordinate for each x-coordinate between the start and end points, coloring those pixel locations red on a bitmap to display the line in a picture box.
2. 盒潮 悋
攵 惆悋 悋 惆 惆Button 悋 攵 攵 悋
惘慍 悋 攵 悋 Pixels 悋 攵 惘悸 悋 悋 攵
//Create New BitMap Image To Put Pixels In And Set Its Width And Height
Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
悋 悋 惘悸 攵 悋 悋 悋 悋PictureBox 慍 愀 惷 悋 惘悸 愀 悋
Pixels惘悸 悋 惘慍 攵 悋悋 攵
//Fill BMP Image Pixels With Points On The Line
bmp.SetPixel(x, y, Color.Red);
PictureBox 悋 惘悸 悋 惘慍 攵 悋
//Show BMP Image In The PictureBox
Graphics g = pictureBox1.CreateGraphics();
g.DrawImage(bmp, new Point(0, 0));
***
惆
Direct Line Algorithm
惘 惺悋 悋 惆 悋
3. Drag & Drop 攵
惆Draw Line 惡 悋Button 悋 攵
悋攵 悋 悋 悋 惆
private void btnDraw_Click(object sender, EventArgs e)
{
//Create New BitMap Image To Put Pixels In And Set Its Width And Height
Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
//Start Algorithm
// The Two Points Vertices
int x1 = int.Parse(txtX1.Text),
x2 = int.Parse(txtX2.Text),
y1 = int.Parse(txtY1.Text),
y2 = int.Parse(txtY2.Text);
//m >> Slope
double m = (x2 - x1) / (y2 - y1);
//b >> Intersection With Y
double b = y1 - (m * x1);
//Find All Points Between The Two Points
for (int x = x1; x <= x2; x++)
{
//From Line Equation >> (y = m*x + b)
int y = (int)(m * x + b);
//Fill BMP Image Pixels With Points On The Line
bmp.SetPixel(x, y, Color.Red);
}
//End Algorithm
//Show BMP Image In The PictureBox
Graphics g = pictureBox1.CreateGraphics();
g.DrawImage(bmp, new Point(0, 0));
}
惘惡 悋 悋