Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <tchar.h>
- #include <windows.h>
- #include <iostream>
- #include <conio.h>
- #include <math.h>
- using namespace std;
- char buffer[480][640] = {0};
- double buffer_z[480][640] = {0};
- COORD Cursor;
- HANDLE output;
- struct Point;
- double lenght(Point, Point);
- LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
- TCHAR szClassName[ ] = _T("GraphNeo");
- HWND hwnd_window;
- HBRUSH brush,brush1;
- HDC hwnd_dc,mem_dc;
- RECT rect;
- PAINTSTRUCT pnt;
- PDWORD thread;
- struct Point{
- double x,y,z;
- void Set(double xa, double ya, double za){
- x = xa; y = ya; z = za;
- }
- Point(): x(0), y(0), z(0){}
- Point(double xa, double ya, double za){
- x = xa; y = ya; z = za;
- }
- friend double lenght(Point temp2, Point temp3 );
- bool operator != ( Point p ){
- if (( this->x == p.x ) && ( this->y == p.y ) && ( this->z == p.z ))
- return 0;
- return 1;
- }
- Point& operator = (Point temp1)
- {
- x = temp1.x; y = temp1.y; z = temp1.z;
- return *this;
- }
- Point operator - (Point temp1){
- Point temp;
- temp.x = this->x - temp1.x; temp.y = this->y - temp1.y; temp.z = this->z - temp1.z;
- return temp;
- }
- Point operator + (Point temp1){
- Point temp;
- temp.x = this->x + temp1.x; temp.y = this->y + temp1.y; temp.z = this->z + temp1.z;
- return temp;
- }
- } NullPoint;
- class Triangle{
- private:
- double lenght = 0; double GlobalLenght = 0;
- Point center,new_p1,new_p2,new_p3;
- Point step( Point temp1, Point temp2, Point temp3 ){
- Point temp = temp1;
- lenght = round(sqrt(abs(pow(temp2.x - temp3.x, 2)+pow(temp2.y - temp3.y, 2)+pow(temp2.z - temp3.z, 2))));
- lenght *= 2.5;
- temp.x = temp.x / lenght; temp.y = temp.y / lenght; temp.z = temp.z / lenght;
- return temp;
- }
- void draw_line( Point temp2 ){
- Point temp1,temp3;
- temp1 = temp2 - new_p1;
- temp1 = step(temp1, temp2,new_p1);
- temp3 = new_p1;
- for(int i = 0; i < lenght; i++ ){
- if ( temp3.z >= 0 ){
- if( (int)temp3.y < 480 && (int)temp3.x < 640 ){
- if ( buffer_z[(int)temp3.y][(int)temp3.x] > temp3.z ){
- SetPixel(mem_dc,(int)temp3.x,(int)temp3.y,RGB(t,0,100));
- buffer_z[(int)temp3.y][(int)temp3.x] = temp3.z;
- }
- }
- }
- temp3 = temp3 + temp1;
- }
- }
- public:
- char t = '#';
- Point p1,p2,p3;
- Triangle(){ t = '#'; }
- Triangle(char ch){
- t = ch;
- }
- Triangle(Point A, Point B, Point C){
- Set(A,B,C);
- }
- void Set( Point A, Point B, Point C ){
- p1 = A; p2 = B; p3 = C;
- double l1,l2;
- l1 = ::lenght(A,C);
- l2 = ::lenght(B,C);
- if ( l1 > l2 ){
- p1= B; p2 = A; p3 = C;
- }
- else if ( ::lenght(A,B) > l2) {
- p1 = C; p2 = A; p3 = B;
- }
- }
- void draw_to_buffer()
- {
- Point temp1, temp2;
- perspective();
- temp1 = new_p3 - new_p2;
- temp1 = step(temp1,new_p3,new_p2);
- GlobalLenght = lenght;
- temp2 = new_p2;
- for(int ai = 0; ai <= GlobalLenght; ai++ )
- {
- draw_line(temp2);
- temp2 = temp2 + temp1;
- }
- }
- void perspective(){
- center.Set(640/2,480/2,0);
- new_p1 = p1;
- new_p2 = p2;
- new_p3 = p3;
- new_p3 = new_p3 - center;
- new_p3.x = new_p3.x / new_p3.z;
- new_p3.y = new_p3.y / new_p3.z;
- new_p2 = new_p2 - center;
- new_p2.x = new_p2.x / new_p2.z;
- new_p2.y = new_p2.y / new_p2.z;
- new_p1 = new_p1 - center;
- new_p1.x = new_p1.x / new_p1.z;
- new_p1.y = new_p1.y / new_p1.z;
- new_p1 = new_p1 + center;
- new_p2 = new_p2 + center;
- new_p3 = new_p3 + center;
- }
- };
- class Rectangl{
- private:
- Triangle t1,t2;
- Point p[4];
- public:
- Rectangl(){}
- Rectangl(Point A, Point B, Point C, Point D){
- p[0] = A; p[1] = B; p[2] = C; p[3] = D;
- t1.Set(A,B,C); t2.Set(A,C,D);
- }
- void Set(Point A, Point B, Point C, Point D){
- p[0] = A; p[1] = B; p[2] = C; p[3] = D;
- t1.Set(A,B,C); t2.Set(A,C,D);
- }
- void SetColor(char ch){
- t1.t = ch; t2.t = ch;
- }
- void Draw(){
- t1.draw_to_buffer();
- t2.draw_to_buffer();
- }
- };
- class Box{
- public:
- Rectangl r[6];
- Box(){}
- Box(double x, double y, double z, Point position ){
- Point A(0,y,0),B(0,y,z),C(x,y,z),D(x,y,0),A1(0,0,0),B1(0,0,z),C1(x,0,z),D1(x,0,0);
- A = A + position; B = B + position; C = C + position; D = D + position;
- A1 = A1 + position; B1 = B1 + position; C1 = C1 + position; D1 = D1 + position;
- r[0].Set(A,A1,D,D1);
- r[1].Set(A,A1,B,B1);
- r[2].Set(D,D1,C,C1);
- r[3].Set(B,B1,C,C1);
- r[4].Set(A,B,C,D);
- r[5].Set(A1,B1,C1,D1);
- }
- void Set(double x, double y, double z, Point position ){
- Point A(0,y,0),B(0,y,z),C(x,y,z),D(x,y,0),A1(0,0,0),B1(0,0,z),C1(x,0,z),D1(x,0,0);
- A = A + position; B = B + position; C = C + position; D = D + position;
- A1 = A1 + position; B1 = B1 + position; C1 = C1 + position; D1 = D1 + position;
- r[0].Set(A1,A,D,D1); r[0].SetColor('1');
- r[1].Set(A1,A,B,B1); r[1].SetColor('2');
- r[2].Set(D1,D,C,C1); r[2].SetColor('5');
- r[3].Set(B1,B,C,C1); r[3].SetColor('4');
- r[4].Set(A,B,C,D); r[4].SetColor('5');
- r[5].Set(A1,B1,C1,D1); r[5].SetColor('6');
- }
- void SetColor(char ch){
- for(int i = 0; i < 6; i++ )
- r[i].SetColor(ch);
- }
- void Draw(){
- for(int i = 0; i < 6; i++ )
- r[i].Draw();
- }
- };
- double lenght(Point temp2, Point temp3 ){
- return round(sqrt(abs(pow(temp2.x - temp3.x, 2)+pow(temp2.y - temp3.y, 2)+pow(temp2.z - temp3.z, 2))));
- }
- void draw_display(){
- GetWindowRect(hwnd_window,&rect);
- GetClientRect(hwnd_window,&rect);
- InvalidateRect(hwnd_window,&rect,0);
- for(int y = 0; y < 480; y++ )
- for( int x = 0; x < 640; x++ )
- buffer_z[y][x] = 999999;
- }
- void GraphInit(){
- Cursor.X = 0; Cursor.Y = 0;
- output = GetStdHandle(STD_OUTPUT_HANDLE);
- draw_display();
- }
- int WINAPI WinMain (HINSTANCE hThisInstance,HINSTANCE hPrevInstance,LPSTR lpszArgument,int nCmdShow)
- {
- MSG messages;
- WNDCLASSEX wincl;
- wincl.hInstance = hThisInstance;
- wincl.lpszClassName = szClassName;
- wincl.lpfnWndProc = WindowProcedure;
- wincl.style = CS_DBLCLKS;
- wincl.cbSize = sizeof (WNDCLASSEX);
- wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
- wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
- wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
- wincl.lpszMenuName = NULL;
- wincl.cbClsExtra = 0;
- wincl.cbWndExtra = 0;
- wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
- if (!RegisterClassEx (&wincl))
- return 0;
- hwnd_window = CreateWindowEx(0,szClassName,_T("Test"),WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,640,480,HWND_DESKTOP,NULL,hThisInstance,NULL);
- ShowWindow (hwnd_window, nCmdShow);
- while (GetMessage (&messages, NULL, 0, 0))
- {
- TranslateMessage(&messages);
- DispatchMessage(&messages);
- }
- return messages.wParam;
- }
- DWORD WINAPI ThreadProc(CONST LPVOID lpParam);
- LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- switch (message)
- {
- case WM_DESTROY:
- PostQuitMessage (0);
- break;
- case WM_CREATE:
- hwnd_dc = GetDC(hwnd_window);
- mem_dc = CreateCompatibleDC(hwnd_dc);
- SelectObject(mem_dc,CreateCompatibleBitmap(hwnd_dc,640,480));
- brush = CreateSolidBrush(0x0);
- brush1 = CreateSolidBrush(0x100);
- SelectObject(mem_dc,brush);
- PatBlt(mem_dc,0,0,640,480,PATCOPY);
- ReleaseDC(hwnd,hwnd_dc);
- CreateThread(0,0,&ThreadProc,0,0,thread);
- SetThreadPriority(thread,HIGH_PRIORITY_CLASS);
- break;
- case WM_PAINT:
- hwnd_dc = BeginPaint(hwnd_window,&pnt);
- BitBlt(hwnd_dc,0,0,640,480,mem_dc,0,0,SRCCOPY);
- EndPaint(hwnd,&pnt);
- SelectObject(mem_dc,brush);
- PatBlt(mem_dc,0,0,640,480,PATCOPY);
- break;
- case WM_LBUTTONDOWN:
- break;
- default:
- return DefWindowProc (hwnd, message, wParam, lParam);
- }
- return 0;
- }
- DWORD WINAPI ThreadProc(CONST LPVOID lpParam){
- GraphInit();
- Box box1;
- Point pos1(200,10,1);
- int x = 0;
- double turn = 2;
- while(true){
- pos1.Set((double)x,10.0,1.0);
- box1.Set(100,15*10,1.4,pos1);
- box1.Draw();
- draw_display();
- x++;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement