C++: how avoid draw outside the view?

here my code for draw a polygon:
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);
}

my problem is on GetLineZZero():
i'm trying getting the line dots... and the Z must be zero or more for draw it.
1
2
3
4
5
6
7
8
9
10
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;
.............

the 'if' is for test if the line point\dot is outside the view.
if is needed more information, i can give.
but what i'm doing wrong for get the positive Z?
what is the actual value? Your logic is coded so it will fail the conditions ON zero, not just negative.

Also, are you 100% sure about your coordinate system? I know that in 2-d M$ often has the Y axis flipped from normal math cartesian y axis, for example. I haven't done a lot of 3d work but is it possible its just not what you expected there?

finally, are you even sure its not getting into the "true" if block? Is it possible the bug is that you drew the line but not where you can see it somehow, eg DM or SZ sent the new one into outer space?

I don't see anything here, I am just asking the questions I would ask myself from your symptoms.
Topic archived. No new replies allowed.