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