ブレゼンハムアルゴリズムを書きました
0<Angular coefficient<1
C# のグラフィックスについてはよくわかりませんが、ピクセルを描画するには、座標 1,1のFillrectangel関数を使用できることに気付きました
私は自分のコードを書きたかったので、パネル上でマウスをクリックすると、2 つの位置でx0、y0 から xEnd、yEnd まで線を引きました。
だからここに例外がある私のコードがあります
null 参照例外は未処理でした オブジェクト参照がオブジェクトのインスタンスに設定されていません。 この例外は e.Graphics.FillRectangle(new SolidBrush(grad1), x, y, 1, 1); の行にあります。
問題は、オブジェクト e が Null であり、それを新しくする必要があることだと思いますが、どうすればよいですか?
線を描画するようにコードを修正するにはどうすればよいですか?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
Line l=new Line();
l.LineBres(Cursor.Position.X, Cursor.Position.Y, Cursor.Position.X, Cursor.Position.Y);
}
}
}
public class Line
{
System.Windows.Forms.DrawItemEventArgs e;
Color grad1 = Color.FromArgb(165, 194, 245);
public void LineBres(int x0, int y0, int xEnd, int yEnd)
{
int dx = xEnd - x0;
int dy = yEnd = y0;
int p = 2 * dy - dx;
int twoDy = 2 * dy;
int twoDyMinusDx = 2 * (dy - dx);
int x, y;
if (x0 > xEnd)
{
x = xEnd;
y = yEnd;
xEnd = x0;
}
else
{
x = x0;
y = y0;
}
e.Graphics.FillRectangle(new SolidBrush(grad1), x, y, 1, 1);
while (x < xEnd)
{
x++;
if (p < 0)
p += twoDy;
else
{
y++;
p += twoDyMinusDx;
}
e.Graphics.FillRectangle(new SolidBrush(grad1), x, y, 1, 1);
}
}
}