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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
#include "image.h"
const double PI = 3.14159265358979;
image img;
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;
}
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};
}
//Draw a Line:
void DrawLine(HDC WindowHDC,point origin,point destination)
{
//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);
point eyepoint={250,150,300};
//now we draw the line with a color and convert the 3D to 2D:
for (point LinePoints:coordenates)
{
point p=perspective(LinePoints,eyepoint);
COLORREF color= RGB(255,0,0);
SetPixel(WindowHDC,p.x,p.y,color);
}
}
//Draw image pixel a pixel:
void DrawImage(HDC WindowHDC,point TopLeft,point TopRight,point BottomLeft,point BottomRight,HDC image)
{
//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> LeftLine;
LeftLine = GetPointsLine(TopLeft,BottomLeft);
vector<point> RgihtLine;
RgihtLine = GetPointsLine(TopRight,BottomRight);
point eyepoint={250,150,300};
//now we draw the line with a color and convert the 3D to 2D:
for(int PosX=0; PosX<LeftLine.size(); PosX++)
{
vector<point> PixelLine;
PixelLine=GetPointsLine(LeftLine[PosX], RgihtLine[PosX]);
for (int PosY=0; PosY<PixelLine.size(); PosY++)
{
point Point=perspective(PixelLine[PosY],eyepoint);
COLORREF color= ::GetPixel(image,PosY,PosX);
SetPixel(WindowHDC,Point.x,Point.y,color);
}
}
}
//Convert Degrees to Radians:
//formula: Radians = Angle*PI/180
angle ConvertDegreesToRadians(angle Rotation)
{
double deg2Rad;
deg2Rad = PI / 180;
angle ConvertDegrees;
ConvertDegrees.x = Rotation.x * deg2Rad;
ConvertDegrees.y = Rotation.y * deg2Rad;
ConvertDegrees.z = Rotation.z * deg2Rad;
return ConvertDegrees;
}
//Rotation points using Angle, Rotation Point and scale:
point RotationPoints(point pt, angle Angle, point pivot={0,0,0},point scale={1,1,1})
{
angle radians= ConvertDegreesToRadians(Angle);//Radians = Angle*PI/180
Angle.x =radians.x;
Angle.y =radians.y;
Angle.z =radians.z;
//Subtrat the Rotation Center:
point p={pt.x-pivot.x,pt.y-pivot.y,pt.z-pivot.z};
//(best using new variables for don't confuse some results)
//calculate the rotation:
point rot,temp;
temp={(p.y)*cos(Angle.x)+(-p.z)*sin(Angle.x),(p.z)*cos(Angle.x)+(p.y)*sin(Angle.x)};
rot.y=temp.x;rot.z=temp.y;
p.y = rot.y;p.z = rot.z;
temp={(p.z)*cos(Angle.y)+(-p.x)*sin(Angle.y),(p.x)*cos(Angle.y)+(p.z)*sin(Angle.y)};
rot.z=temp.x;rot.x=temp.y;
p.x=rot.x;
temp={(p.x)*cos(Angle.z)+(-p.y)*sin(Angle.z),(p.y)*cos(Angle.z)+(p.x)*sin(Angle.z)};
rot.x=temp.x;rot.y=temp.y;
//scale the new point and
//add the Rotation Center:
return {(scale.x*rot.x+pivot.x),(scale.y*rot.y+pivot.y),(scale.z*rot.z+pivot.z)};
}
int main()
{
//getting the HDC Console Window:
HDC WindowHDC=GetDC(GetConsoleWindow());
point TopLeft={100, 200,0};
point TopRight={200,200,0};
point BottomLeft={100,300,0};
point BottomRight={200,300,0};
point RotationPoint={250,150,0};//center of rectangle
float anglex=0;
float angley=0;
float anglez=0;
img.FromFile("C:\\Users\\Joaquim\\Pictures\\1595973613452.jpg");
do
{
//TopLeft:
point TopLeftRotated=RotationPoints(TopLeft,{anglex,angley,anglez},RotationPoint);
//TopRight:
point TopRightRotated=RotationPoints(TopRight,{anglex,angley,anglez},RotationPoint);
//BottomLeft:
point BottomLeftRotated=RotationPoints(BottomLeft,{anglex,angley,anglez},RotationPoint);
//BottomRight:
point BottomRightRotated=RotationPoints(BottomRight,{anglex,angley,anglez},RotationPoint);
DrawImage(WindowHDC,TopLeftRotated,TopRightRotated,BottomLeftRotated, BottomRightRotated,img.hdcimage);
img.DrawImage(WindowHDC,800,0);
angley+=1;
Sleep(5);
RECT a;
a.left=0;
a.right=500;
a.top=0;
a.bottom=400;
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();
}
|