void CCGPainterView::DrawLine(CDC *pDC, CPoint ptStartPoint, CPoint ptEndPoint, COLORREF cLineColor) | |
{ | |
/************************************************************* | |
write the DDA 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 steps; | |
int dx = xEnd - x0; | |
int dy = yEnd - y0; | |
float x = x0, y = y0, xIn, yIn; | |
if (fabs(dx) < fabs(dy)) | |
{ | |
steps = fabs(dy); | |
} | |
else | |
{ | |
steps = fabs(dx); | |
} | |
xIn = float(dx) / float(steps); | |
yIn = float(dy) / float(steps); | |
CPoint point(int(x + 0.5), int(y + 0.5)); | |
pDC->SetPixelV(point, cLineColor); | |
for (int i = 0; i < steps; i++) | |
{ | |
x += xIn; | |
y += yIn; | |
CPoint point(int(x + 0.5), int(y + 0.5)); | |
pDC->SetPixelV(point, cLineColor); | |
} | |
} |
跟着文字,去往远方