1 using System; 2 3 using System.Collections.Generic; 4 5 using System.ComponentModel; 6 7 using System.Data; 8 9 using System.Linq; 10 11 using System.Text; 12 13 using System.Windows.Forms; 14 15 using System.Drawing; 16 17 using System.Drawing.Drawing2D; 18 19 20 21 namespace Bresenham 22 23 { 24 25 public partial class Form1 : Form 26 27 { 28 29 Graphics g; 30 31 const int CellSize = 20; 32 33 int iclick = 0; 34 35 int Cols, Rows; 36 37 Point A,B; 38 39 40 41 public Form1() 42 43 { 44 45 InitializeComponent(); 46 47 g = CreateGraphics(); 48 49 Rows = this.Height / 20; 50 51 Cols = this.Width / 20; 52 53 } 54 55 56 57 private void Form1_Load(object sender, EventArgs e) 58 59 { 60 61 } 62 63 64 65 private void Form1_Paint(object sender, PaintEventArgs e)//程序启动时调用一次DrawSence() 66 67 { 68 69 DrawSence(); 70 71 } 72 73 74 75 private void DrawSence()//绘制背景网格 76 77 { 78 79 int i, j; 80 81 Point M, N; 82 83 Pen p = new Pen(Brushes.Black, 1); 84 85 g.Clear(Color.White); 86 87 for (i = 0; i < Rows 1; i ) 88 89 { 90 91 M = new Point(0, i * CellSize); 92 93 N = new Point(Width, i * CellSize); 94 95 g.DrawLine(p, M, N); 96 97 } 98 99 for (j = 0; j < Cols 1; j ) 100 101 { 102 103 M = new Point(j * CellSize, 0); 104 105 N = new Point(j * CellSize, Height); 106 107 g.DrawLine(p, M, N); 108 109 } 110 111 } 112 113 114 115 private void Form1_MouseClick(object sender, MouseEventArgs e)//记录鼠标坐标,并绘制位置 116 117 { 118 119 switch (iclick) 120 121 { 122 123 case 0: 124 125 { 126 127 A = new Point(e.X / CellSize, e.Y / CellSize); 128 129 DrawSence(); 130 131 g.FillEllipse(Brushes.Red, A.X * CellSize, A.Y * CellSize, CellSize, CellSize); 132 133 iclick = 1; 134 135 break; 136 137 } 138 139 case 1: 140 141 { 142 143 B = new Point(e.X / CellSize, e.Y / CellSize); 144 145 DoBresenham(A,B); 146 147 g.FillEllipse(Brushes.Red, B.X * CellSize, B.Y * CellSize, CellSize, CellSize); 148 149 g.FillEllipse(Brushes.Red, A.X * CellSize, A.Y * CellSize, CellSize, CellSize); 150 151 iclick = 0; 152 153 break; 154 155 } 156 157 } 158 159 } 160 161 private void DoBresenham(Point p1,Point p2)//实现Bresenham算法,绘制离散像素点 162 163 { 164 165 int dx = p2.X - p1.X; 166 167 int dy = p2.Y - p1.Y; 168 169 int stepX = 0, stepY = 0; 170 171 if (dx < 0) 172 173 { 174 175 dx = -dx; 176 177 stepX = -1; 178 179 } 180 181 else 182 183 stepX = 1; 184 185 if (dy < 0) 186 187 { 188 189 dy = -dy; 190 191 stepY = -1; 192 193 } 194 195 else 196 197 stepY = 1; 198 199 200 201 if (dx > dy) 202 203 { 204 205 int y = p1.Y; 206 207 int d = 2 * dy - dx; 208 209 for (int i = p1.X; i != p2.X; i = stepX) 210 211 { 212 213 g.FillEllipse(Brushes.Blue, i * CellSize, y * CellSize, 20, 20); 214 215 d = d 2 * dy; 216 217 if (d >= 0) 218 219 { 220 221 y = stepY; 222 223 d = d - 2 * dx; 224 225 } 226 227 } 228 229 } 230 231 else 232 233 { 234 235 int x = p1.X; 236 237 int d = 2 * dx - dy; 238 239 for (int i = p1.Y; i != p2.Y; i = stepY) 240 241 { 242 243 g.FillEllipse(Brushes.Blue, x * CellSize, i * CellSize, 20, 20); 244 245 d = d 2 * dx; 246 247 if (d >= 0) 248 249 { 250 251 x = stepX; 252 253 d = d - 2 * dy; 254 255 } 256 257 } 258 259 } 260 261 } 262 263 } 264 265 }