| void CCGPainterView::DrawLine(CDC *pDC, CPoint ptStartPoint, CPoint ptEndPoint, COLORREF cLineColor) |
| { |
| |
| write the Bresenham' line algorithm for drawing the line |
| use function: pDC->SetPixelV (point, cLineColor); to drawing a pixel |
| 编码直线生成算法,调用函数 pDC->SetPixelV (point, cLineColor) 画像素。 |
| *************************************************************/ |
| |
| int x0 = ptStartPoint.x, y0 = ptStartPoint.y; |
| int xEnd = ptEndPoint.x, yEnd = ptEndPoint.y; |
| int dx = xEnd - x0; |
| int dy = yEnd - y0; |
| int converse = 0; |
| int dxabs = fabs(dx); |
| int dyabs = fabs(dy); |
| int s1 = xEnd > x0 ? 1 : -1; |
| int s2 = yEnd > y0 ? 1 : -1; |
| int x = x0, y = y0; |
| if (dyabs > dxabs) |
| { |
| int tmp = dxabs; |
| dxabs = dyabs; |
| dyabs = tmp; |
| converse = 1; |
| } |
| int p = 2 * dyabs - dxabs; |
| for (int i = 0; i < dxabs; i++) |
| { |
| CPoint point(x, y); |
| pDC->SetPixelV(point, cLineColor); |
| if (p >= 0) |
| { |
| if (converse) |
| { |
| x += s1; |
| } |
| else |
| { |
| y += s2; |
| } |
| p -= 2 * dxabs; |
| } |
| if (converse) |
| { |
| y += s2; |
| } |
| else |
| { |
| x += s1; |
| } |
| p += 2 * dyabs; |
| } |
| } |