Advertisement
Guest User

Untitled

a guest
Mar 29th, 2015
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.58 KB | None | 0 0
  1.  
  2.  
  3. #define _USE_MATH_DEFINES
  4.  
  5. #include <windows.h> //Plik naglowkowy zawierający definicje funkcji, typy danych oraz makra WinAPI
  6. #pragma comment(lib, "gdiplus.lib") // Dolaczamy biblioteke gdiplus
  7.  
  8. #include <windows.h> //Plik naglowkowy zawierający definicje funkcji, typy danych oraz makra WinAPI
  9. #include <gdiplus.h> //Plik naglowkowy GDIPlus
  10. #include <iostream>
  11. #include <math.h>
  12.  
  13. using namespace Gdiplus;        //Uzywamy przestrzeni nazw GDI
  14. using namespace std;
  15.  
  16. struct Vector2D{
  17.     double v[3];
  18.  
  19.     Vector2D(){
  20.         v[0] = v[1] = 0;
  21.         v[2] = 1;
  22.     }
  23. };
  24.  
  25. struct MyMatrix{
  26.     double m[3][3];
  27.  
  28.     MyMatrix(){
  29.         //Wypełnia macierz zerami
  30.         for (int i = 0; i < 3; i++){
  31.             for (int j = 0; j < 3; j++) m[i][j] = 0;
  32.         }
  33.     }
  34.  
  35.     void Identity(){
  36.         //Wypełnia macierz zerami
  37.         for (int i = 0; i < 3; i++){
  38.             for (int j = 0; j < 3; j++) m[i][j] = 0;
  39.         }
  40.         //Wypełnia przekątną jedynkami
  41.         m[0][0] = m[1][1] = m[2][2] = 1;
  42.     }
  43.  
  44.     void Translate(int x, int y){
  45.         //Tworzy macierz jednostkową i ustawia odpowiednie punkty macierzy na wartość translacji
  46.         Identity();
  47.         m[0][2] = x;
  48.         m[1][2] = y;
  49.     }
  50.  
  51.     void Rotate(float a){
  52.         //Tworzy macierz jednostkową i wypełnia odpowiednimi wartościami według wzoru
  53.         Identity();
  54.         m[0][0] = m[1][1] = cos(a);
  55.         m[1][0] = sin(a);
  56.         m[0][1] = -sin(a);
  57.     }
  58. };
  59.  
  60. void Bresenham(int x0, int y0, int x1, int y1, int blackness);
  61. void SimpleLine(int x0, int y0, int x1, int y1, int blackness);
  62. void FloodFill(int x, int y, int borderColor, int fillColor);
  63. float DegToRad(float a);
  64. Vector2D Multiply(MyMatrix mat, Vector2D vec);
  65. MyMatrix MultiplyMatrix(MyMatrix m1, MyMatrix m2);
  66.  
  67.  
  68. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  69. VOID DrawTranslatedPixel(int x, int y, MyMatrix przeksztalcenie);
  70.  
  71. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  72. {
  73.     MSG  msg;
  74.     HWND hWnd;
  75.     WNDCLASS wndClass;
  76.  
  77.  
  78.     wndClass.style = CS_HREDRAW | CS_VREDRAW;       //Styl okna - bedzie sie odmalowywalo przy kazdym przesunieciu lub zmianie rozdzielczosci  
  79.     wndClass.lpfnWndProc = WndProc;                                                         //Wskazujemy procedure przetwarzajaca komunikaty okna
  80.     wndClass.cbClsExtra = 0;
  81.     wndClass.cbWndExtra = 0;
  82.     wndClass.hInstance = hInstance;                                                 //Ustawiamy w oknie instancje naszego programu
  83.     wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);               // Ladujemy z zasobow systemowych
  84.     wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);                 // Domyslny kursor i ikone
  85.     wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);   // Kolor tla okna
  86.     wndClass.lpszMenuName = NULL;                                                                   // Nie tworzymy menu
  87.     wndClass.lpszClassName = TEXT("GettingStarted");                                // Nazwa klasy okna, wyswietlana w naglowku okna
  88.  
  89.     RegisterClass(&wndClass);               //Rejestrujemy klase okna w systemie
  90.  
  91.  
  92.     hWnd = CreateWindow(
  93.         TEXT("GettingStarted"),   // window class name
  94.         TEXT("Getting Started"),  // window caption
  95.         WS_OVERLAPPEDWINDOW,      // window style
  96.         CW_USEDEFAULT,            // initial x position
  97.         CW_USEDEFAULT,            // initial y position
  98.         800,                                      // initial x size
  99.         600,                                      // initial y size
  100.         NULL,                     // parent window handle
  101.         NULL,                     // window menu handle
  102.         hInstance,                // program instance handle
  103.         NULL);                    // creation parameters
  104.  
  105.  
  106.  
  107.     RECT rect = { 0, 0, 800, 600 }; //Tworzymy prostokat o wymiarach 800x600
  108.     AdjustWindowRect(&rect, GetWindowLong(hWnd, GWL_STYLE), FALSE); //Skalujemy okno tak aby obszar roboczy faktycznie mial 800x600px
  109.     SetWindowPos(hWnd, 0, 0, 0, rect.right - rect.left, rect.bottom - rect.top, SWP_NOZORDER | SWP_NOMOVE);
  110.  
  111.     GdiplusStartupInput gdiplusStartupInput;        //Struktura zawierajaca parametry startowe
  112.     ULONG_PTR           gdiplusToken;               // Wskaznik pod ktory zostanie przypisany token
  113.  
  114.     // Initializacja GDI
  115.     GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
  116.  
  117.  
  118.     ShowWindow(hWnd, nCmdShow);     //Wyswietlamy okno
  119.     UpdateWindow(hWnd);
  120.  
  121.     /*
  122.     Ponizej znajduje sie petla wiadomosci. Odbieramy
  123.     wiadomosci od systemu za pomoca funkcji GetMessage
  124.     i przekazujemy je do procedury okna.
  125.     */
  126.     while (GetMessage(&msg, NULL, 0, 0)) {
  127.         DispatchMessage(&msg);
  128.     }
  129.     GdiplusShutdown(gdiplusToken);
  130.     return (int)msg.wParam;
  131. }
  132.  
  133.  
  134. struct MyPixel
  135. {
  136.     MyPixel()
  137.     {
  138.         mBlackness = 0;
  139.     }
  140.  
  141.     int mBlackness;
  142. };
  143.  
  144. MyPixel mFrontBuffer[40][30];
  145.  
  146.  
  147. void DrawPixel(HDC hdc, int x, int y, int blackness){
  148.     Graphics graphics(hdc);
  149.     SolidBrush solidBrush(Color(blackness, 0, 0, 0));
  150.     graphics.FillRectangle(&solidBrush, x + 1, y + 1, 19, 19);
  151. };
  152.  
  153. void Present(HDC hdc);
  154.  
  155.  
  156. VOID OnPaint(HDC hdc)
  157. {
  158.  
  159.  
  160.     Graphics graphics(hdc);
  161.     Pen      pen(Color(255, 0, 0, 0)); //Konstruktor przyjmuje kolor w formacie ARGB
  162.     //FillTable ();
  163.  
  164.     for (int i = 0; i< 40; i++){
  165.         graphics.DrawLine(&pen, 20 * i, 0, 20 * i, 600); //argumenty to obiekt Pen, x1, y1, x2, y2
  166.     }
  167.     for (int i = 0; i< 30; i++){
  168.         graphics.DrawLine(&pen, 0, i * 20, 800, i * 20); //argumenty to obiekt Pen, x1, y1, x2, y2
  169.     }
  170.  
  171.     /*SimpleLine(2, 2, 7, 19, 255);
  172.     Bresenham(1, 1, 7, 19, 255);*/
  173.     Present(hdc);
  174.     //DrawPixel(hdc, 20,20,250);
  175.  
  176.  
  177.  
  178. }
  179.  
  180. void Present(HDC hdc) {
  181.     for (int i = 0; i< 40; i++){
  182.         for (int j = 0; j< 30; j++){
  183.  
  184.             DrawPixel(hdc, i * 20, j * 20, mFrontBuffer[i][j].mBlackness);
  185.         }
  186.     }
  187. }
  188.  
  189. void Bresenham(int x1, int y1, int x2, int y2, int blackness){
  190.  
  191.  
  192.  
  193.     int dx = x2 - x1;
  194.     int dy = y2 - y1;
  195.  
  196.     int da = 2 * dy;
  197.  
  198.     int db = 2 * dy - 2 * dx;
  199.  
  200.     int d = 2 * dy - dx;
  201.  
  202.     int y = y1;
  203.  
  204.     for (int x = x1; x <x2; x++){
  205.         mFrontBuffer[x][y].mBlackness = blackness;
  206.         if (d >0){
  207.             d += db;
  208.             y += 1;
  209.         }
  210.         else {
  211.             d += da;
  212.  
  213.         }
  214.  
  215.  
  216.     }
  217.  
  218.     /**
  219.     for  x in range(dx):
  220.  
  221.     if d > 0:
  222.     d += delta_B
  223.     y += 1
  224.     else:
  225.     d += delta_A
  226.     **/
  227. }
  228.  
  229. void SimpleLine(int x0, int y0, int x1, int y1, int blackness){
  230.  
  231.     int dx = x1 - x0;
  232.     int dy = y1 - y0;
  233.     float a = dy / dx;
  234.     float b = y0 - a*x0;
  235.  
  236.     for (int x = x0; x<x1; x++){
  237.         int y = a*x + b;
  238.         mFrontBuffer[x][y].mBlackness = blackness;
  239.  
  240.     }
  241.  
  242.  
  243.  
  244. }
  245.  
  246. void FloodFill(int x, int y, int borderColor, int fillColor){
  247.     if (mFrontBuffer[x][y].mBlackness != borderColor && mFrontBuffer[x][y].mBlackness != fillColor){
  248.         mFrontBuffer[x][y].mBlackness = fillColor;
  249.  
  250.         if (x >= 0 && x <= 40 && y - 1 >= 0 && y - 1 <= 40){
  251.             FloodFill(x, y - 1, borderColor, fillColor);
  252.         }
  253.  
  254.         if (x >= 0 && x <= 40 && y + 1 >= 0 && y + 1 <= 40){
  255.             FloodFill(x, y + 1, borderColor, fillColor);
  256.         }
  257.  
  258.         if (x - 1 >= 0 && x - 1 <= 40 && y >= 0 && y <= 40){
  259.             FloodFill(x - 1, y, borderColor, fillColor);
  260.         }
  261.  
  262.         if (x + 1 >= 0 && x + 1 <= 40 && y >= 0 && y <= 40){
  263.             FloodFill(x + 1, y, borderColor, fillColor);
  264.         }
  265.     }
  266. }
  267.  
  268. //Przelicza stopnie na radiany.
  269. float DegToRad(float a){
  270.     return a*M_PI / 180;
  271. }
  272.  
  273. Vector2D Multiply(MyMatrix mat, Vector2D vec){
  274.     Vector2D result;
  275.     //Mnoży wektor przez macież, jednak bez wsp. Z
  276.     for (int i = 0; i < 2; i++){
  277.         for (int j = 0; j < 3; j++){
  278.             result.v[i] += vec.v[j] * mat.m[i][j];
  279.         }
  280.     }
  281.  
  282.     return result;
  283. }
  284.  
  285. MyMatrix MultiplyMatrix(MyMatrix m1, MyMatrix m2){
  286.     MyMatrix result;
  287.  
  288.     for (int i = 0; i < 3; i++){
  289.         for (int j = 0; j < 3; j++)
  290.         {
  291.             for (int k = 0; k < 3; k++) result.m[i][j] += m1.m[i][k] * m2.m[k][j];
  292.         }
  293.     }
  294.  
  295.     return result;
  296. }
  297.  
  298. VOID DrawTranslatedPixel(int x, int y, MyMatrix przeksztalcenie){
  299.     //Wypelnia kliknięty piksel szarym kolorem
  300.     mFrontBuffer[x][y].mBlackness = 128;
  301.  
  302.     Vector2D vector;
  303.     vector.v[0] = x;
  304.     vector.v[1] = y;
  305.  
  306.     //Przekształca wektor i jeśli po przekształceniu wsp. mieszczą się w oknie to wypełnia pikse o tych wsp. czarnym kolorem
  307.     Vector2D result = Multiply(przeksztalcenie, vector);
  308.     if (result.v[0] >= 0 && result.v[0] < 40 && result.v[1] >= 0 && result.v[1] < 30){
  309.         mFrontBuffer[(int)result.v[0]][(int)result.v[1]].mBlackness = 255;
  310.     }
  311. }
  312.  
  313. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  314. {
  315.  
  316.     HDC          hdc;
  317.     PAINTSTRUCT  ps;
  318.     int x;
  319.     int y;
  320.     MyMatrix rotatedMat;
  321.     rotatedMat.Rotate(DegToRad(15.0)); //Macierz obrotu o 15 st.
  322.     MyMatrix translatedMat;
  323.     translatedMat.Translate(5, 5); //Macierz translacji
  324.  
  325.     switch (msg)
  326.     {
  327.     case WM_LBUTTONDOWN:
  328.         x = LOWORD(lParam);
  329.         y = HIWORD(lParam);
  330.  
  331.         /*for (int i = 0; i <40; i++){
  332.         for (int j = 0; j<30; j++){
  333.         if (x < i * 20 + 20 && x > i * 20 && y < j * 20 + 20 && y > j * 20){
  334.         mFrontBuffer[i][j].mBlackness = 255;
  335.         }
  336.         }
  337.         }*/
  338.  
  339.         DrawTranslatedPixel((int)x / 20, (int)y / 20, MultiplyMatrix(rotatedMat, translatedMat));
  340.         RedrawWindow(hwnd, NULL, NULL, RDW_ERASE | RDW_INVALIDATE);
  341.         break;
  342.  
  343.     case WM_RBUTTONDOWN:
  344.  
  345.         x = LOWORD(lParam);
  346.         y = HIWORD(lParam);
  347.  
  348.         /*for (int i = 0; i <40; i++){
  349.         for (int j = 0; j<30; j++){
  350.         if (x < i * 20 + 20 && x > i * 20 && y < j * 20 + 20 && y > j * 20){
  351.         FloodFill(i, j, 255, 128);
  352.         }
  353.         }
  354.         }*/
  355.  
  356.         DrawTranslatedPixel((int)x / 20, (int)y / 20, MultiplyMatrix(translatedMat, rotatedMat));
  357.         RedrawWindow(hwnd, NULL, NULL, RDW_ERASE | RDW_INVALIDATE);
  358.         break;
  359.     case WM_PAINT:
  360.         hdc = BeginPaint(hwnd, &ps);
  361.         OnPaint(hdc);
  362.         EndPaint(hwnd, &ps);
  363.         return 0;
  364.  
  365.     case WM_DESTROY:
  366.         PostQuitMessage(0); // Ta funkcja dodaje do kolejki wiadomosci WM_QUIT
  367.         return 0;
  368.     }
  369.  
  370.     // wszystkie inne wiadomosci sa obslugiwane w sposob domyslny
  371.     return DefWindowProc(hwnd, msg, wParam, lParam);
  372. }
  373.  
  374. //////////////////////////////////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement