1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
struct point
{
float x,y,z;
} ;
vector<point> GetLinePoints(int x0, int y0, int z0, int x1, int y1, int z1)
{
vector<point> LinePoints;
point LinePoint;
int dx = abs(x1-x0), sx = x0<x1 ? 1 : -1;
int dy = abs(y1-y0), sy = y0<y1 ? 1 : -1;
int dz = abs(z1-z0), sz = z0<z1 ? 1 : -1;
int dm =std::max( { dx, dy, dz } ), i = dm; /* maximum difference */
x1 = y1 = z1 = dm/2; /* error offset */
do
{
x1 -= dx; if (x1 < 0) { x1 += dm; x0 += sx; }
y1 -= dy; if (y1 < 0) { y1 += dm; y0 += sy; }
z1 -= dz; if (z1 < 0) { z1 += dm; z0 += sz; }
LinePoint.x=x0;
LinePoint.y=y0;
LinePoint.z=z0;
LinePoints.push_back(LinePoint);
i--;
}while(i >=0);
return LinePoints;
}
void DrawLine3D(HDC HDCDestination, int x0, int y0, int z0, int x1, int y1, int z1, COLORREF color=RGB(255,255,255), int lenght=1)
{
//Get Line Dots Positions:
vector <point> LinePoints=GetLinePoints(x0,y0,z0,x1,y1,z1);
//Draw the Line Dots Positions:
for(int LinePosition=0; LinePosition<LinePoints.size(); LinePosition++)
{
//Draw Line Lenght:
for(int LineLenght=0;LineLenght<lenght; LineLenght++)
{
SetPixel(HDCDestination,LinePoints[LinePosition].x,LinePoints[LinePosition+LineLenght].y,color);
}
}
}
| |