- 1.数值微分法算法
- 2.中点画线法算法
- 3.Bresenham 画线算法
// An highlighted block void MIDPOINT_Line(CDC *&pDC, CPoint startPoint, CPoint endPoint, COLORREF crColor) { if (endPoint.x != startPoint.x && endPoint.y != startPoint.y) //非特殊直线一般直线 { int kFlag = 0; // 0: 斜率 <= 1, 1: 斜率> 1 int sFlag = 1; // 1: 斜率> 0; -1:斜率< 0 if (startPoint.x > endPoint.x) // 先判断两个端点, 使起点 x 小于终点 x { CPoint tem = startPoint; startPoint = endPoint; endPoint = tem; } if (abs(endPoint.y - startPoint.y)> abs(endPoint.x - startPoint.x)) //判断斜率是否大于1 { kFlag = 1; } if (endPoint.y < startPoint.y) //判断斜率正负 { sFlag = -1; } int a, b,tA, tAB, d, x, y; if (sFlag == -1) { endPoint.y = startPoint.y + (startPoint.y - endPoint.y); } a = startPoint.y - endPoint.y; b = endPoint.x - startPoint.x; tA = 2 * a; tAB = 2 * (a + b); d = 2 * a + b; x = startPoint.x; y = startPoint.y; pDC->SetPixel(x, y, crColor); if (kFlag == 0) { for (int i = 0; i < (endPoint.x - startPoint.x); i++) { if (d>= 0) { pDC->SetPixel(x + 1, y, crColor); x += 1; d += tA; } else { pDC->SetPixel(x + 1, y + sFlag, crColor); x += 1; y += sFlag; d += tAB; } } } else //斜率 > 1 { if (kFlag == 1) { tA = 2 * b; d = 2*b + a; } for (int i =0; i< abs(endPoint.y - startPoint.y); i++) { if (d >= 0) { pDC->SetPixel(x + 1, y + sFlag, crColor); y += sFlag; x += 1; d += tAB; } else { // d < 0 pDC->SetPixel(x, y + sFlag, crColor); y += sFlag; d += tAB; } } } } else if (startPoint.x == endPoint.x) //画垂直线 { if (startPoint.y < endPoint.y) { for (int i = startPoint.y; i < endPoint.y; i++) { pDC->SetPixel(startPoint.x, i, crColor); } } else { for (int i = endPoint.y; i < startPoint.y; i++) { pDC->SetPixel(startPoint.x, i, crColor); } } } else if (startPoint.y == endPoint.y) { if (startPoint.x < endPoint.x) { for (int i = startPoint.x; i < endPoint.x; i++) { pDC->SetPixel(i, startPoint.y, crColor); } } else { for (int i = endPoint.x; i < startPoint.x; i++) { pDC->SetPixel(i, startPoint.y, crColor); } } } }2.中点画线法算法
// An highlighted block void DDA_Line(CDC *&pDC, CPoint &startPoint, CPoint &endPoint, COLORREF crColor) { if (endPoint.x != startPoint.x && endPoint.y != startPoint.y) //一般直线 { double x, y; double k; //斜率 k = ((float)(endPoint.y - startPoint.y) - (float)(endPoint.x - startPoint.y)); x = endPoint.x; y = endPoint.y; pDC->SetPixel((int)x, (int)y, crColor); if (fabs(k) <= 1.0) { for (int i =0; i3.Bresenham 画线算法startPoint.x) { x += 1; y += k; } else { x -= 1; y -= k; } pDC->SetPixel((int)x, (int)(y + 0.5), crColor); } } else if (fabs(k) > 1) { for (int i =0; i startPoint.y) { y += 1; x += 1.0 / k; } else { y -= 1; x -= 1.0 / k; } pDC->SetPixel((int)x + 0.5, (int)y, crColor); } } } else if(startPoint.x == endPoint.x) //画垂直线 { if (startPoint.y < endPoint.y) { for (int i =startPoint.y; i SetPixel(startPoint.x, i, crColor); } } else { for (int i = endPoint.y; i < startPoint.y; i++) { pDC->SetPixel(startPoint.x, i, crColor); } } } else if (startPoint.y == endPoint.y) { if (startPoint.x < endPoint.x) { for (int i = startPoint.x; i < endPoint.x; i++) { pDC->SetPixel(i, startPoint.y, crColor); } } else { for (int i = endPoint.x; i< startPoint.x; i++) { pDC->SetPixel(i, startPoint.y, crColor); } } } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)