Part2 - Math: how calculate the line dots?

these topic is the second part of https://www.cplusplus.com/forum/general/281522/
heres the final code for draw the line:
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include<math.h>
#include<iostream>
#include <windows.h>
#include <vector>

using namespace std;

struct point
{
float x,y,z;
} ;


float distance(point p1,point p2)
{
	return sqrt( (p1.x-p2.x)* (p1.x-p2.x) + (p1.y-p2.y)* (p1.y-p2.y) + (p1.z-p2.z)* (p1.z-p2.z));
}

point lineto(point fp,point p,float length)
{
	point res;
    float L=distance(fp,p);
    res.x=fp.x+length*(p.x-fp.x)/L;
    res.y=fp.y+length*(p.y-fp.y)/L;
    res.z=fp.z+length*(p.z-fp.z)/L;
    return res;
}

vector<point> GetPointsLine(point origin,point destination)
{
    point t=origin;
    vector<point> coordenates;
	float dst=distance(origin,destination);
	for (int i=0;i<=dst;i++)
    {
        t=lineto(t,destination,1);
        coordenates.push_back(t);
    }
    return coordenates;
}

vector<point> DrawLine(HDC WindowHDC,point origin,point destination,COLORREF color=RGB(255,0,0) )
{
    float FocalDistance =100;
    
    //for convert 3D to 2D we must:
    //2D.X = 3D.X * FocalDistance / 3D.Z
    //2D.Y = 3D.Y * FocalDistance / 3D.Z
    
    point t=origin;
    vector<point> coordenates;
	float dst=distance(origin,destination);
	for (int i=0;i<=dst;i++)
    {
        t=lineto(t,destination,1);
        coordenates.push_back(t);
        if(t.z>0)
            SetPixel(WindowHDC,t.x/t.z*FocalDistance,t.y/t.z*FocalDistance,color);
        else
            SetPixel(WindowHDC,t.x,t.y,color);
    }
    return coordenates;
}

int main()
{
     //getting the HDC Console Window:
    HDC WindowHDC=GetDC(GetConsoleWindow());
	point origin={0,100,0};
	point destination={500,110,-1};
	do
    {
        DrawLine(WindowHDC,origin,destination);
    }while(!(GetKeyState(13) & 0x8000));//press escape for exit
    cout<<"Press return to end . . ."<<endl;
    cin.get();
}

these code seems fine and the line is drawed.
but when i use these code for create the fillrectangle() function, i can get these result: https://imgur.com/1xmoPNf
so how can i avoid the black pixels inside the fillrectangle?
I'm confused, if the problem is actually a function you call 'fillrectangle', shouldn't you show us that code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void DrawFilledRectangle(HDC WindowHDC,point Corners[4],COLORREF color=RGB(255,0,0) )
{
    float FocalDistance =100;

    //for convert 3D to 2D we must:
    //2D.X = 3D.X * FocalDistance / 3D.Z
    //2D.Y = 3D.Y * FocalDistance / 3D.Z

    vector<point> GetLeftLinePoints=GetPointsLine(Corners[0], Corners[3]);
    vector<point> GetRightLinePoints=GetPointsLine(Corners[1], Corners[2]);

	for (int y=0;y<=GetLeftLinePoints.size();y++)
    {
        vector<point> GetCenterLinePoints=GetPointsLine(GetLeftLinePoints[y],GetRightLinePoints[y] );
        for (int x=0;x<=GetCenterLinePoints.size();x++)
        {
            SetPixel(WindowHDC,GetCenterLinePoints[x].x,GetCenterLinePoints[x].y,color);
        }
    }
}

used:
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
int main()
{

     //getting the HDC Console Window:
    HDC WindowHDC=GetDC(GetConsoleWindow());
    angle t{0,0,0};
    point origin{0,50,0};
    point destiny{100,50,0};
    point rotorigin;
    point rotdestiny;
    RECT a;

    a.left=0;
    a.right=500;
    a.top=0;
    a.bottom=500;
    do
    {

        t.z+=0.5;
        point Points[4]={{0,0,0},{100,0,0},{100,100,0},{0,100,0}};

        point RotPoints[4];
        RotPoints[0]=RotationPoints(Points[0],t);
        RotPoints[1]=RotationPoints(Points[1],t);
        RotPoints[2]=RotationPoints(Points[2],t);
        RotPoints[3]=RotationPoints(Points[3],t);
        DrawFilledRectangle(WindowHDC,RotPoints,RGB(255,0,0));

        Sleep(250);
        FillRect(WindowHDC,&a, CreateSolidBrush(RGB(0,0,0)));

    }while(!(GetKeyState(VK_ESCAPE) & 0x8000));

    cout<<"Press return to end . . ."<<endl;
    cin.get();
    return 0;
}

result: https://imgur.com/Yeub59K
1 red pixel outside the rectangle and the rectangle have several black pixels.(the rectangle isn't entire printed, because i print the screen before finish the draw)
why i get several black pixels? seem the line points isn't calculated correctly when te line is angled rotated.
Ganado (6376): did i miss somtething? or continues confused? the image.. can you see it?
I didn't attempt to run your code yet, but one idea: Is it better if you round up 'dst' here:
for (int i=0;i<=dst;i++)
So, ceil(dst) instead of truncating?

That would change GetPointsLine, DrawLine.
Last edited on
i'm sorry, but the problem remains :(
what happens if you move the black fill above the rectangle fill?
the black fill is the Window Console Backcolor.
Topic archived. No new replies allowed.