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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
position GetLineZZero(position Origin, position Destination)
{
position LinePoint;
RECT fd;
GetClientRect(GetConsoleWindow(), &fd);
int dx = abs(Destination.Pos3DX-Origin.Pos3DX), sx = Origin.Pos3DX<Destination.Pos3DX ? 1 : -1;
int dy = abs(Destination.Pos3DY-Origin.Pos3DY), sy = Origin.Pos3DY<Destination.Pos3DY ? 1 : -1;
int dz = abs(Destination.Pos3DZ-Origin.Pos3DZ), sz = Origin.Pos3DZ<Destination.Pos3DZ ? 1 : -1;
int dm =std::max( { dx, dy, dz } ), i = dm; /* maximum difference */
Destination.Pos3DX = Destination.Pos3DY = Destination.Pos3DZ = dm/2; /* error offset */
do
{
Destination.Pos3DX -= dx; if (Destination.Pos3DX < 0) { Destination.Pos3DX += dm; Origin.Pos3DX += sx; }
Destination.Pos3DY -= dy; if (Destination.Pos3DY < 0) { Destination.Pos3DY += dm; Origin.Pos3DY += sy; }
Destination.Pos3DZ -= dz; if (Destination.Pos3DZ < 0) { Destination.Pos3DZ += dm; Origin.Pos3DZ += sz; }
LinePoint.Pos3DX=Origin.Pos3DX;
LinePoint.Pos3DY=Origin.Pos3DY;
LinePoint.Pos3DZ=Origin.Pos3DZ;
if(LinePoint.Pos3DZ<=1) return LinePoint;;
//if(LinePoint.Pos3DX<=0 || LinePoint.Pos3DX>=fd.right) break;
//if(LinePoint.Pos3DY<=0 || LinePoint.Pos3DY>=fd.bottom) break;
i--;
}while(i>0);
return LinePoint;
}
void DrawPolygon(HDC HDCDestination, vector<position> points, HPEN LineColor, HBRUSH PlaneColor)
{
RECT fd;
GetClientRect(GetConsoleWindow(), &fd);
HPEN hOldPen = (HPEN)SelectObject(HDCDestination, LineColor);
HBRUSH hOldBrush = (HBRUSH)SelectObject(HDCDestination, PlaneColor);
//testing if the line limits are outside the window:
{
if(points[0].Pos3DZ<=0 )
{
points[0]=GetLineZZero(points[0],points[1]);
}
if(points[3].Pos3DZ<=0 )
{
points[3]=GetLineZZero(points[3],points[2]);
}
}
vector<POINT> polygon;
for(int index=0; index<points.size(); index++)
{
polygon.push_back(points[index].Perspective(300,{fd.right/2, fd.bottom/2 }));
}
Polygon(HDCDestination,polygon.data(),points.size());
SelectObject(HDCDestination, hOldPen);
SelectObject(HDCDestination, hOldBrush);
}
| |