Math: how rotate a 3D point?

Pages: 12
Dec 28, 2021 at 8:52pm
from these image i did a rotation function:
https://imgur.com/mNWt6GR
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
struct point
{
    float x,y,z;
} ;

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

point RotationPoints(point Coordenate, angle AngleCoordenate)
{
    point RotatedPoint;

    //First we rotate the Z:
    RotatedPoint.x=Coordenate.x * cos(AngleCoordenate.z)-Coordenate.y*sin(AngleCoordenate.z);
    RotatedPoint.y=Coordenate.x * sin(AngleCoordenate.z)+Coordenate.y*cos(AngleCoordenate.z);
    RotatedPoint.z=Coordenate.z;

    //Second we rotate the Y:
    RotatedPoint.x=RotatedPoint.x * cos(AngleCoordenate.y)+RotatedPoint.z*sin(AngleCoordenate.y);
    RotatedPoint.y=RotatedPoint.y;
    RotatedPoint.z=RotatedPoint.y * sin(AngleCoordenate.y)+RotatedPoint.z*cos(AngleCoordenate.y);

    //Third we rotate the X:
    RotatedPoint.x=RotatedPoint.x;
    RotatedPoint.y=RotatedPoint.y * cos(AngleCoordenate.x)-RotatedPoint.z*sin(AngleCoordenate.x);
    RotatedPoint.z=RotatedPoint.y * sin(AngleCoordenate.x)+RotatedPoint.z*cos(AngleCoordenate.x);
    return RotatedPoint;
}

//using it:
//getting the HDC Console Window:
    HDC WindowHDC=GetDC(GetConsoleWindow());
    point origin ={100,50,0};
    point destination ={500,50,0};

    //destination=RotationPoints(origin,{0,0,10});
    float anglez=0;
	do
    {
        origin=RotationPoints(origin,{0,0,anglez});
        DrawLine(WindowHDC,origin,destination);
        anglez+=1;
        if(anglez>=1000) anglez=0;
        //cout << "\t"<<anglez;
        Sleep(500);
        RECT a;
        a.left=0;
        a.right=1000;
        a.top=0;
        a.bottom=1000;
        FillRect(WindowHDC,&a, CreateSolidBrush(RGB(0,0,0)));
    }while(!(GetKeyState(VK_ESCAPE) & 0x8000));//press escape for exit
    cout<<"Press return to end . . ."<<endl;
    cin.get();

but the angle seems limited for 75º or something:
https://imgur.com/9VQfn2q
what i'm doing wrong with my rotation calculation?
Dec 28, 2021 at 10:16pm
i miss two important things:
1 - the computer use Radians and not Degrees:
1
2
//Degrees to Radians:
        anglez=anglez*3.24/180;

2 - how i add a rotation origin? by default is zero, but i need from it point
Last edited on Dec 28, 2021 at 10:37pm
Dec 28, 2021 at 10:42pm
https://en.wikipedia.org/wiki/Rotation_matrix

what you generally have to do ends up being shorthand for 'move to the origin, rotate, move back'.
https://math.stackexchange.com/questions/2093314/rotation-matrix-of-rotation-around-a-point-other-than-the-origin

you need the first link to fully comprehend the second, though.
Last edited on Dec 28, 2021 at 10:45pm
Dec 28, 2021 at 10:46pm
jonnin (9974): i'm sorry, but i need ask 1 thing: why i have 'scare' when i found, several searchs, use Matrix?
Dec 28, 2021 at 10:55pm
https://www.google.com/url?sa=i&url=https%3A%2F%2Fmemegenerator.net%2Finstance%2F57737401%2Fbarbie-math-class-is-tough-lets-go-shopping&psig=AOvVaw1dN5ez9yUZxQUAS9-6KxP_&ust=1640818508243000&source=images&cd=vfe&ved=0CAsQjRxqFwoTCPjqirTLh_UCFQAAAAAdAAAAABAD


the links get you where you need to be. Getting there isn't easy; I remember spending some real time with these back when. But they actually just give you the answer -- its a fill in the blank 3x3 grid, you just need to trig of the angle and the offset point.
Last edited on Dec 28, 2021 at 10:58pm
Dec 29, 2021 at 3:43am
Cambalinho wrote:
1
2
//Degrees to Radians:
        anglez=anglez*3.24/180;


Pi to 2 decimal places is 3.14. It would be better IMO to have 6 decimal places for a float.

C++20 has numeric constants https://en.cppreference.com/w/cpp/numeric/constants
Feature testing https://en.cppreference.com/w/cpp/feature_test

So it could be written :

1
2
3
4
5
6
7
8
#if __cpp_lib_math_constants < 201907L  // check for suitable compiler version requires c++20
#error "Requires C++20 for numeric constants" // compiler error if wrong version of compiler is used

#include <numbers>

using namespace std::numbers;
constexpr double DegToRad {pi/ 180.0}
float anglezAsrad = anglez * DegToRad; // new variable name instead of overwriting the existing one. 


If you don't have c++20:

const double DegToRad {std::acos(-1.0) / 180.0}

I used double it doesn't hurt to have too much precision, the constant is calculated once at compile time. I guess one could have it as a float if is really going to be a worry. Change the 1.0 to 1.0f and 180.0 to 180.0f as well.

I always put floating point numbers with a figure after the decimal point, as in 180.0 not 180. If nothing else, it reinforces the idea the value is floating point, not an integer. It helps prevent errors such as integer division.
Last edited on Dec 29, 2021 at 4:58am
Dec 29, 2021 at 5:14am
.0174533 is good enough for anything short of nasa levels.
Dec 29, 2021 at 6:57am
@jonnin

As long as there are no typos, my method helps with that.

And as long as it is not applied to world coordinates. I routinely work with coordinates in the range of 10'000'000.000 , ideally one should calc accurately with the world coordinates before transforming to screen coordinates.

If the OP is just using screen coordinates throughout, then that is probably fine.
Dec 29, 2021 at 12:29pm
@jonnin: you said: "what you generally have to do ends up being shorthand for 'move to the origin, rotate, move back'."
but see these sample: a line have 1 points(origin and destination)... imagine if the origin rotation center is the destination point.... how i add the rotation point to the origin?
the RotationPoints() functions have the rotation calculations, but i don't know add the rotation center.. you said right, i did that on past... but i'm using a different rotation center.

@TheIdeasMan: i was doing faster, that's why i used '3.14' and not 6 decimals places. but i can change it ;)
Last edited on Dec 29, 2021 at 12:32pm
Dec 29, 2021 at 1:39pm
sorry, that did not make much sense. you have 2 or more points to make a line, not 1.
you don't add the destination to the origin; its handled in the matrix multiply, so you fill in the blanks in the matrix.
if you wanted to do it by hand, you subtract every point ... point-destination puts the new origin at destination, rotate, and then add destination back. points are added component wise:
p3.x = p.x + p2.x
p3.y = p.y + p2.y
p3.z = p.z + p2.z

so the whole thing is
p3 = p-destination //OOP point class
p4 = rotate p3 (sin & cos of your angle find the new locations)
p3 = p4+destination;

p3 is now the rotated point in the original space.



Last edited on Dec 29, 2021 at 1:41pm
Dec 29, 2021 at 1:56pm
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
point RotationPoints(point Coordenate, angle AngleCoordenate, point RotationCenter={0,0,0})
{
    point RotatedPoint;
    
    //for add the Rotation Point
    //we must sub the Rotation point with rotation center:
    Coordenate.x-=RotationCenter.x;
    Coordenate.y-=RotationCenter.y;
    Coordenate.z-=RotationCenter.z;
    
    
    //First we rotate the Z:
    RotatedPoint.x=Coordenate.x * cos(AngleCoordenate.z)-Coordenate.y*sin(AngleCoordenate.z);
    RotatedPoint.y=Coordenate.x * sin(AngleCoordenate.z)+Coordenate.y*cos(AngleCoordenate.z);
    RotatedPoint.z=Coordenate.z;

    //Second we rotate the Y:
    RotatedPoint.x=RotatedPoint.x * cos(AngleCoordenate.y)+RotatedPoint.z*sin(AngleCoordenate.y);
    RotatedPoint.y=RotatedPoint.y;
    RotatedPoint.z=RotatedPoint.y * sin(AngleCoordenate.y)+RotatedPoint.z*cos(AngleCoordenate.y);

    //Third we rotate the X:
    RotatedPoint.x=RotatedPoint.x;
    RotatedPoint.y=RotatedPoint.y * cos(AngleCoordenate.x)-RotatedPoint.z*sin(AngleCoordenate.x);
    RotatedPoint.z=RotatedPoint.y * sin(AngleCoordenate.x)+RotatedPoint.z*cos(AngleCoordenate.x);
    
    //after rotate the point
    //we add the rotation center:
    RotatedPoint.x+=RotationCenter.x;
    RotatedPoint.y+=RotationCenter.y;
    RotatedPoint.z+=RotationCenter.z;

    return RotatedPoint;
}

now seems to work.
but i need ask:
- the line is horizontal;
- the Z rotation make the line rotate like a clock(by it's rotation center);
- the X and Y rotation is ignored.... correct me: is these normal?
Dec 29, 2021 at 2:15pm
Cambalinho wrote:
- the X and Y rotation is ignored.... correct me: is these normal?

Of course it's not normal - but then neither is your approach.


You specified AngleCoordenate.x and AngleCoordenate.y both to be 0, so it will make a zero rotation about either of these axes.


Just use a rotation matrix to rotate about the origin. There's one here:
https://www.cplusplus.com/forum/beginner/268330/#msg1154590


If you want to rotate about a point other than the origin, then (as @jonnin explained):
- subtract the rotation centre (so that you get a relative displacement)
- rotate
- add back the vector describing the rotation centre.


BTW, you have largely ignored most of the advice given to you in both this thread and your previous one.

Dec 29, 2021 at 2:28pm
Edit: didn't see lastchance post

- the X and Y rotation is ignored.... correct me: is these normal?


What values did you have for the angles? Can you prove with the debugger they haven't changed?

Or are you just looking at the graphics?

Show us what you called the function with.

By the way, the angles are not coordinates, IMO the variable would less confusing if it was named XYZrotations
Last edited on Dec 29, 2021 at 2:34pm
Dec 29, 2021 at 3:00pm
using these values:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
point origin ={200,100,0};
    point destination ={200,200,0};

    //destination=RotationPoints(origin,{0,0,10});
    float anglez=0;
	do
    {
        //Degrees to Radians:
        anglez=anglez*3.24/180;
        origin=RotationPoints(origin,{0,0,anglez},destination);
        DrawLine(WindowHDC,origin,destination);
        anglez+=1;
        if(anglez>=360) anglez=0;
        //cout << "\t"<<anglez;
        Sleep(100);
        RECT a;
        a.left=0;
        a.right=1000;
        a.top=0;
        a.bottom=1000;
        FillRect(WindowHDC,&a, CreateSolidBrush(RGB(0,0,0)));
    }while(!(GetKeyState(VK_ESCAPE) & 0x8000));//press escape for exit 

rotate like a clock:
https://imgur.com/ocaF3vM
is a line... so isn't good test the X and Y rotation.... the best for test is a rectangle.
Dec 29, 2021 at 3:46pm
see the difference on main:
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
int main()
{
     //getting the HDC Console Window:
    HDC WindowHDC=GetDC(GetConsoleWindow());

    point TopOrigin ={200,100,0};
    point TopDestination ={300,100,0};

    point LeftOrigin ={200,100,0};
    point LeftDestination ={200,200,0};

    point RightOrigin ={300,100,0};
    point RightDestination ={300,200,0};

    point BottomOrigin ={200,200,0};
    point BottomDestination ={300,200,0};

    point RotationPoint={250,150,0};
    float anglex=0;
    float angley=0;
    float anglez=0;
	do
    {
        //Degrees to Radians:
        anglez=anglez*3.24/180;
        anglex=anglex*3.24/180;
        angley=angley*3.24/180;

        //Top:
        TopOrigin=RotationPoints(TopOrigin,{anglex,angley,anglez},RotationPoint);
        TopDestination=RotationPoints(TopDestination,{anglex,angley,anglez},RotationPoint);
        DrawLine(WindowHDC,TopOrigin,TopDestination);

        //Left:
        LeftOrigin=RotationPoints(LeftOrigin,{anglex,angley,anglez},RotationPoint);
        LeftDestination=RotationPoints(LeftDestination,{anglex,angley,anglez},RotationPoint);
        DrawLine(WindowHDC,LeftOrigin,LeftDestination);

        //Right:
        RightOrigin=RotationPoints(RightOrigin,{anglex,angley,anglez},RotationPoint);
        RightDestination=RotationPoints(RightDestination,{anglex,angley,anglez},RotationPoint);
        DrawLine(WindowHDC,RightOrigin,RightDestination);

        //Bottom:
        BottomOrigin=RotationPoints(BottomOrigin,{anglex,angley,anglez},RotationPoint);
        BottomDestination=RotationPoints(BottomDestination,{anglex,angley,anglez},RotationPoint);
        DrawLine(WindowHDC,BottomOrigin,BottomDestination);


        anglex+=1;
        if(anglex>=360) anglex=0;
        //cout << "\t"<<anglez;
        Sleep(100);
        RECT a;
        a.left=0;
        a.right=1000;
        a.top=0;
        a.bottom=1000;
        FillRect(WindowHDC,&a, CreateSolidBrush(RGB(0,0,0)));
    }while(!(GetKeyState(VK_ESCAPE) & 0x8000));//press escape for exit
    cout<<"Press return to end . . ."<<endl;
    cin.get();
}

the rest of code is the same.
i'm changing the x angle: the rotation is done... but the rectangle top isn't showed :(
the rotation center it's rectangle center
https://imgur.com/kIToVjq
Dec 29, 2021 at 4:03pm
i fix that, but i need more help: using float i can't control the angle :(
https://imgur.com/TRzEjQa
after, thinking on what i see, 180º, i get that red lines outside the rectangle :(
but you see that float value angle :(
Last edited on Dec 29, 2021 at 4:04pm
Dec 29, 2021 at 11:52pm

Here is my rotate with perspective.
I use g++ 64 bits.
Command line (needed in g++ to access Gdi)
-std=c++17 -l Gdi32 -l Gdiplus
Rotate the corners of a cube using a very basic Gdi graphics screen.
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124

#include <math.h>
#include <windows.h>
#include<gdiplus.h>
#define map(a,b,x,c,d) ((d)-(c))*((x)-(a))/((b)-(a))+(c)

using namespace std;
using namespace Gdiplus;
struct point
{
    float x,y,z;
} ;

struct angle
{
    float sx,sy,sz;
    float cx,cy,cz;
} ;


void construct(angle &res,const point &a)
{
	res={sin(a.x),sin(a.y),sin(a.z),cos(a.x),cos(a.y),cos(a.z)};
}

point rotate(const point &c,const point &p,const angle &a,const point &scale={1,1,1})
{
    float dx=p.x-c.x,dy=p.y-c.y,dz=p.z-c.z;
    return{(scale.x)*((a.cy*a.cz)*dx+(-a.cx*a.sz+a.sx*a.sy*a.cz)*dy+(a.sx*a.sz+a.cx*a.sy*a.cz)*dz)+c.x,
           (scale.y)*((a.cy*a.sz)*dx+(a.cx*a.cz+a.sx*a.sy*a.sz)*dy+(-a.sx*a.cz+a.cx*a.sy*a.sz)*dz)+c.y,
           (scale.z)*((-a.sy)*dx+(a.sx*a.cy)*dy+(a.cx*a.cy)*dz)+c.z};
}

point perspective(const point &p,const point &eyepoint)
{
    float   w=1+(p.z/eyepoint.z);
    return {(p.x-eyepoint.x)/w+eyepoint.x,
    (p.y-eyepoint.y)/w+eyepoint.y,
    (p.z-eyepoint.z)/w+eyepoint.z};
}

void bubblesort(point p[])
{
	for (int i=0;i<=6;i++)
	{
		for(int j=i+1;j<=7;j++)
		{
			if(p[i].z<p[j].z) swap (p[i],p[j]);
		}
	}		
}

void circle(const HDC &WinHDC,int x,int y,int r)
{
 Ellipse(WinHDC,(x-r),(y-r),(x+r),(y+r));	
}

void draw(const HDC &WinHDC,const point rot[])
{
	for (int i=0 ;i<8;i++)
	{
	int rad=map(-200,200,rot[i].z,15,10);
	circle(WinHDC,rot[i].x,rot[i].y,rad);
}
}

void initpaint(HDC WinHDC)
{
SelectObject(WinHDC,GetStockObject(DC_BRUSH));
SelectObject(WinHDC,GetStockObject(DC_PEN));
  SetDCBrushColor(WinHDC,RGB(200,0,0));
  SetDCPenColor(WinHDC,RGB(0,0,200));
}

int main()
 {
 	point pts[8],rot[8];
 	pts[0]={200,100,200};
 	pts[1]={600,100,200};
 	pts[2]={200,500,200};
 	pts[3]={600,500,200};
 	
 	pts[4]={200,100,-200};
 	pts[5]={600,100,-200};
 	pts[6]={200,500,-200};
 	pts[7]={600,500,-200};
 	
 HWND mainwin;
 MSG emsg;
 mainwin=CreateWindowEx(0,"#32770","Basic GDI. Press <escape> to end.",WS_OVERLAPPEDWINDOW | WS_VISIBLE,200,200,800,600,0,0,0,0);
 PAINTSTRUCT ps;
 HDC hdc2;
 hdc2=BeginPaint(mainwin, &ps);
 initpaint(hdc2);
 point ang;
 angle A;
 while (!(GetKeyState(VK_ESCAPE) & 0x8000))
 {
 while (PeekMessage (&emsg, NULL, 0, 0, PM_REMOVE) > 0)
 {
            TranslateMessage (&emsg);
            DispatchMessage (&emsg);    
}
// do the graphics
BitBlt(GetDC(mainwin), 0,0, 800,600, 0,0,0, WHITENESS); 
 ang.x+=.01;
 ang.y+=.01/2;
 ang.z+=.01/3;
 construct(A,ang);
 for(int n=0;n<=7;n++)
 {
 	rot[n]=rotate({400,300,0},pts[n],A,{.5,.5,.5});
 	rot[n]=perspective(rot[n],{400,300,600});
 }
 bubblesort(rot);

 draw(hdc2,rot);
 
 Sleep(1);
}
 
 }

 
Dec 30, 2021 at 2:27pm
it's working fine your code...
instead copy your code for my code, i need understand:
- why my rotation isn't working correctly? i don't understand what i miss :(
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include<math.h>
#include<iostream>
#include <windows.h>
#include <vector>

using namespace std;

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

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


float GetLineLength(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));
}

//Get Points Line:
point lineto(point fp,point p,float length)
{
	point res;
    float L=GetLineLength(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=GetLineLength(origin,destination);
	for (int i=0;i<=dst;i++)
    {
        t=lineto(t,destination,1);
        coordenates.push_back(t);
    }
    return coordenates;
}


//Draw a Line:
void DrawLine(HDC WindowHDC,point origin,point destination,COLORREF color=RGB(255,0,0) )
{

    //for convert 3D to 2D we must:
    //have Focal Distance, in these case is 100
    //2D.X = 3D.X * FocalDistance / 3D.Z
    //2D.Y = 3D.Y * FocalDistance / 3D.Z
    float FocalDistance =100;

    //Getting the Points of a line:
    vector<point> coordenates;

    origin.z=-origin.z;
    destination.z=-destination.z;
    coordenates = GetPointsLine(origin,destination);

    //now we draw the line with a color and convert the 3D to 2D:
	for (point LinePoints:coordenates)
    {

        if(LinePoints.z> 0)
        {
            float Coordenate2DX=LinePoints.x/LinePoints.z*FocalDistance;
            float Coordenate2DY=LinePoints.y/LinePoints.z*FocalDistance;
            if((Coordenate2DX>=0 || Coordenate2DY>=0) || (Coordenate2DX<=100 || Coordenate2DY<=100))
                SetPixel(WindowHDC,Coordenate2DX,Coordenate2DY,color);
        }
        else
        {
            if((LinePoints.x>=0 || LinePoints.y>=0) || (LinePoints.x<=100 || LinePoints.y<=0))
                SetPixel(WindowHDC,LinePoints.x,LinePoints.y,color);
        }
    }
}

point RotationPoints(point Coordenate, angle AngleCoordenate, point RotationCenter={0,0,0})
{
    point RotatedPoint;

    //for add the Rotation Point
    //we must sub the Rotation point with rotation center:
    Coordenate.x-=RotationCenter.x;
    Coordenate.y-=RotationCenter.y;
    Coordenate.z-=RotationCenter.z;


    //First we rotate the Z:
    RotatedPoint.x=Coordenate.x * cos(AngleCoordenate.z)-Coordenate.y*sin(AngleCoordenate.z);
    RotatedPoint.y=Coordenate.x * sin(AngleCoordenate.z)+Coordenate.y*cos(AngleCoordenate.z);
    RotatedPoint.z=Coordenate.z;

    //Second we rotate the Y:
    RotatedPoint.x=RotatedPoint.x * cos(AngleCoordenate.y)+RotatedPoint.z*sin(AngleCoordenate.y);
    RotatedPoint.y=RotatedPoint.y;
    RotatedPoint.z=-RotatedPoint.x * sin(AngleCoordenate.y)+RotatedPoint.z*cos(AngleCoordenate.y);

    //Third we rotate the X:
    RotatedPoint.x=RotatedPoint.x;
    RotatedPoint.y=RotatedPoint.y * cos(AngleCoordenate.x)-RotatedPoint.z*sin(AngleCoordenate.x);
    RotatedPoint.z=RotatedPoint.y * sin(AngleCoordenate.x)+RotatedPoint.z*cos(AngleCoordenate.x);

    //after rotate the point
    //we add the rotation center:
    RotatedPoint.x+=RotationCenter.x;
    RotatedPoint.y+=RotationCenter.y;
    RotatedPoint.z+=RotationCenter.z;

    return RotatedPoint;
}

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

    point TopOrigin ={200,200,0};
    point TopDestination ={300,200,0};

    point LeftOrigin ={200,200,0};
    point LeftDestination ={200,300,0};

    point RightOrigin ={300,200,0};
    point RightDestination ={300,300,0};

    point BottomOrigin ={200,300,0};
    point BottomDestination ={300,300,0};

    point RotationPoint={250,250,0};
    float anglex=0;
    float angley=0;
    float anglez=0;
    float PI =3.14159265358979323846;
    int angle=0;
	do
    {

        //Top:
        TopOrigin=RotationPoints(TopOrigin,{anglex,angley,anglez},RotationPoint);
        TopDestination=RotationPoints(TopDestination,{anglex,angley,anglez},RotationPoint);
        DrawLine(WindowHDC,TopOrigin,TopDestination);

        //Left:
        LeftOrigin=RotationPoints(LeftOrigin,{anglex,angley,anglez},RotationPoint);
        LeftDestination=RotationPoints(LeftDestination,{anglex,angley,anglez},RotationPoint);
        DrawLine(WindowHDC,LeftOrigin,LeftDestination);

        //Right:
        RightOrigin=RotationPoints(RightOrigin,{anglex,angley,anglez},RotationPoint);
        RightDestination=RotationPoints(RightDestination,{anglex,angley,anglez},RotationPoint);
        DrawLine(WindowHDC,RightOrigin,RightDestination);

        //Bottom:
        BottomOrigin=RotationPoints(BottomOrigin,{anglex,angley,anglez},RotationPoint);
        BottomDestination=RotationPoints(BottomDestination,{anglex,angley,anglez},RotationPoint);
        DrawLine(WindowHDC,BottomOrigin,BottomDestination);


        anglex+=1;
        angle+=1;
        if (angle>=360)
        {
            angle=0;
            anglex=0;
            angley=0;
            anglez=0;
        }

        //Degrees to Radians:
        anglez=anglez*PI/180;
        anglex=anglex*PI/180;
        angley=angley*PI/180;
        cout << anglex;
        //cout << "\t"<<anglez;
        Sleep(100);
        RECT a;
        a.left=0;
        a.right=1000;
        a.top=0;
        a.bottom=1000;
        FillRect(WindowHDC,&a, CreateSolidBrush(RGB(0,0,0)));
    }while(!(GetKeyState(VK_ESCAPE) & 0x8000));//press escape for exit
    cout<<"Press return to end . . ."<<endl;
    cin.get();
}
Dec 30, 2021 at 2:37pm
"isn't working correctly" isn't much help when it's difficult to see what you want.

angley and anglez are always 0 in your code, but who knows if that is relevant.
Dec 30, 2021 at 2:38pm
You really should be able to use the debugger by this stage. What is changing? What is not changing?

Edit:

I mentioned this before, it's not related to your problem. PI/180 is being calculated 3 times every time the code loops. Put this before the loop:

constexpr float DegToRad = PI / 180.0;
Last edited on Dec 30, 2021 at 2:41pm
Pages: 12