Math: how calculate the line dots?

Dec 26, 2021 at 9:32pm
the line have origin and destion points.
how can i calculate the dots between the line origin and destination.
i create these code:
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
78
79
80
81
82
83
84
#include <iostream>
#include <windows.h>
#include <vector>
#include <math.h>

using namespace std;

HDC WindowHDC=NULL;

struct Coordenates
{
    int X=0;
    int Y=0;
    int Z=0;
};

//get the X, Y and Z distance between the Destination and Origin line points:
Coordenates DefPosition3D(Coordenates Origin, Coordenates Destiny)
{
    Coordenates distance;
    distance.X = Destiny.X - Origin.X;
    distance.Y = Destiny.Y - Origin.Y;
    distance.Z = Destiny.Z - Origin.Z;
    return distance;
}

//get the step incremention:
Coordenates InCrement(Coordenates DefPosition, Coordenates Steps )
{
    Coordenates inc ;
    if(inc.X>0)
        inc.X = DefPosition.X / Steps.X;
    if(inc.Y>0)
        inc.Y = DefPosition.Y / Steps.Y;
    if(inc.Z>0)
        inc.Z = DefPosition.Z / Steps.Z;
    return inc;
}


void DrawLineDots(Coordenates Origin, Coordenates Destination)
{

    Coordenates Steps;
    Coordenates inc;

    //get the steps between Destination and origin:
    Steps = DefPosition3D(Origin, Destination);

    //get increment steps:
    inc =InCrement(Steps, Steps);
    Coordenates nextpoint;
    int X ;
    int Y;
    int Z;

    //get the line dots:
    for (X = 0; X< Steps.X ; X++)
    {
        for (Y = 0; Y< Steps.Y;Y++)
        {
            for (Z = 0; Z< Steps.Z;Z++)
            {
                nextpoint.X = round(nextpoint.X + Steps.X);
                nextpoint.Y = round(nextpoint.Y + Steps.Y);
                nextpoint.Z = round(nextpoint.Z + Steps.Z);
                SetPixel(WindowHDC,0,0,RGB(255,0,0));
            }
        }
    }

}

int main()
{
    //getting the HDC Console Window:
    WindowHDC=GetDC(GetConsoleWindow());

    do
    {
        DrawLineDots({0,10,0},{100,10,0});
    }while(!(GetKeyState(13) & 0x8000));//press escape for exit
    return 0;
}

but no results.
so my question is: how can i calculate the line dots between origin and destination line?
Dec 26, 2021 at 10:21pm
Well, your call to SetPixel doesn't seem to use any information about the point you have just calculated and I'm not sure why your call to InCrement uses two identical arguments.

Also, you seem to be using 3-d coordinates for a 2-d plot, a straight line has ONE increment parameter, not 3, and you seem to be using round() on ints.
Last edited on Dec 26, 2021 at 10:23pm
Dec 27, 2021 at 12:26am
not sure what you are doing, but I would suggest..

dist = distance between start and end.
for(0 to distance)
{
///what you have now, add x,y,z deltas to last point, from start
}
the triple nested loop just looks wrong to me. I could be not seeing something there, but ???
the deltas and points need to be doubles, so you can add like 0.0001 or whatever small change that accumulates to say draw a line from 0,0,0 to 0,1,10000000 or whatever :)
Last edited on Dec 27, 2021 at 12:27am
Dec 27, 2021 at 5:02am
With <windows.h>, you could use the LineDDA function.
https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-linedda
Dec 27, 2021 at 5:08am
Forget figuring it out yourself. Since you are using the Windows GDI API, use MoveToEx|https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-movetoex and LineTo|https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-lineto.

If you really want to put your own pixels/whatever then look up “Bresenham’s Line Algorithm” (fast and simple) and “Wu’s Line Algorithm” (for antialiasing).

Hope this helps.
Last edited on Dec 27, 2021 at 5:08am
Dec 27, 2021 at 6:26pm

Only the calculations.
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
#include<math.h>
#include<iostream>
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;
}


int main()
{ 
	point origin={0,0,0};
	point destination={100,200,50};
	point t=origin;
	float dst=distance(origin,destination);
	for (int i=0;i<=dst;i++)
	{
	t=lineto(t,destination,1);
	cout<<int(t.x)<<" "<<int(t.y)<<" "<<int(t.z)<<endl;		
    }
    cout<<"Press return to end . . ."<<endl;
    cin.get();
} 
Dec 27, 2021 at 7:00pm
i did some changes.... i need ask: how can i convert the Z to X,Y?
i did:
SetPixel(WindowHDC,t.x/t.z,t.y/t.z,color);
X/Z and Y/Z.. is these correct?
heres the result from:
1
2
point origin={0,100,1};
	point destination={500,100,2};

https://imgur.com/Qt7DVth
is these correct? the Z seems more big than i thot :(
heres the entire code now:
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
#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) )
{
    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.x>0 && t.y>0)
            SetPixel(WindowHDC,t.x/t.z,t.y/t.z,color);
    }
    return coordenates;
}

int main()
{
     //getting the HDC Console Window:
    HDC WindowHDC=GetDC(GetConsoleWindow());
	point origin={0,100,1};
	point destination={500,100,2};
	do
    {
        DrawLine(WindowHDC,origin,destination);
    }while(!(GetKeyState(13) & 0x8000));//press escape for exit
    cout<<"Press return to end . . ."<<endl;
    cin.get();
}
Dec 27, 2021 at 9:55pm
What do you mean by "convert the Z to X,Y"?
Dec 27, 2021 at 10:37pm
the screen just use X and Y... so where is the Z?
(yes it's 3D... i'm doing these for 3D)
Dec 27, 2021 at 10:48pm
how convert 3D to 2D?
Dec 27, 2021 at 11:06pm
i found my error...
for convert 3D to 2D we must:
2D.X = 3D.X * FocalDistance / 3D.Z
2D.Y = 3D.Y * FocalDistance / 3D.Z

i belive: the FocalDistance is the distance between our eye and the 3D World.
i accept correction. i use '100' on code.
see these result:
1
2
point origin={0,100,0};
	point destination={500,110,-1};

https://imgur.com/VKFyt4K

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();
}
Last edited on Dec 27, 2021 at 11:09pm
Dec 27, 2021 at 11:09pm
Dec 27, 2021 at 11:15pm
tomorrow i will test the next step and share ;)
Dec 27, 2021 at 11:16pm

You can only plot x and y.
The .z value can be used to simulate 3d, for instance instead of SetPixel, you could use ellipse to draw small circles which change size according to the .z value to give the effect of the dots going into the distance. (getting smaller).




Dec 28, 2021 at 7:27pm
simulated 3d also involves some angle work to make it look right. Something square in 2d looked at in the xz plane for instance is a slanted thing in 3d


  /-------/  //angles and shifting
 /       /
---------
|       |
|       |
|       |
---------
Last edited on Dec 28, 2021 at 7:30pm
Dec 28, 2021 at 8:46pm
3D Rotation:
https://imgur.com/qOgUuPf

2D Rotation:
https://imgur.com/irN1i7N

now i will create a new topic.
thanks for all to all
Last edited on Dec 28, 2021 at 8:47pm
Topic archived. No new replies allowed.