Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _USE_MATH_DEFINES
- #include <windows.h> //Plik naglowkowy zawierający definicje funkcji, typy danych oraz makra WinAPI
- #pragma comment(lib, "gdiplus.lib") // Dolaczamy biblioteke gdiplus
- #include <windows.h> //Plik naglowkowy zawierający definicje funkcji, typy danych oraz makra WinAPI
- #include <gdiplus.h> //Plik naglowkowy GDIPlus
- #include <iostream>
- #include <math.h>
- using namespace Gdiplus; //Uzywamy przestrzeni nazw GDI
- using namespace std;
- struct Vector2D{
- double v[3];
- Vector2D(){
- v[0] = v[1] = 0;
- v[2] = 1;
- }
- };
- struct MyMatrix{
- double m[3][3];
- MyMatrix(){
- //Wypełnia macierz zerami
- for (int i = 0; i < 3; i++){
- for (int j = 0; j < 3; j++) m[i][j] = 0;
- }
- }
- void Identity(){
- //Wypełnia macierz zerami
- for (int i = 0; i < 3; i++){
- for (int j = 0; j < 3; j++) m[i][j] = 0;
- }
- //Wypełnia przekątną jedynkami
- m[0][0] = m[1][1] = m[2][2] = 1;
- }
- void Translate(int x, int y){
- //Tworzy macierz jednostkową i ustawia odpowiednie punkty macierzy na wartość translacji
- Identity();
- m[0][2] = x;
- m[1][2] = y;
- }
- void Rotate(float a){
- //Tworzy macierz jednostkową i wypełnia odpowiednimi wartościami według wzoru
- Identity();
- m[0][0] = m[1][1] = cos(a);
- m[1][0] = sin(a);
- m[0][1] = -sin(a);
- }
- };
- void Bresenham(int x0, int y0, int x1, int y1, int blackness);
- void SimpleLine(int x0, int y0, int x1, int y1, int blackness);
- void FloodFill(int x, int y, int borderColor, int fillColor);
- float DegToRad(float a);
- Vector2D Multiply(MyMatrix mat, Vector2D vec);
- MyMatrix MultiplyMatrix(MyMatrix m1, MyMatrix m2);
- LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
- VOID DrawTranslatedPixel(int x, int y, MyMatrix przeksztalcenie);
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
- {
- MSG msg;
- HWND hWnd;
- WNDCLASS wndClass;
- wndClass.style = CS_HREDRAW | CS_VREDRAW; //Styl okna - bedzie sie odmalowywalo przy kazdym przesunieciu lub zmianie rozdzielczosci
- wndClass.lpfnWndProc = WndProc; //Wskazujemy procedure przetwarzajaca komunikaty okna
- wndClass.cbClsExtra = 0;
- wndClass.cbWndExtra = 0;
- wndClass.hInstance = hInstance; //Ustawiamy w oknie instancje naszego programu
- wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); // Ladujemy z zasobow systemowych
- wndClass.hCursor = LoadCursor(NULL, IDC_ARROW); // Domyslny kursor i ikone
- wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); // Kolor tla okna
- wndClass.lpszMenuName = NULL; // Nie tworzymy menu
- wndClass.lpszClassName = TEXT("GettingStarted"); // Nazwa klasy okna, wyswietlana w naglowku okna
- RegisterClass(&wndClass); //Rejestrujemy klase okna w systemie
- hWnd = CreateWindow(
- TEXT("GettingStarted"), // window class name
- TEXT("Getting Started"), // window caption
- WS_OVERLAPPEDWINDOW, // window style
- CW_USEDEFAULT, // initial x position
- CW_USEDEFAULT, // initial y position
- 800, // initial x size
- 600, // initial y size
- NULL, // parent window handle
- NULL, // window menu handle
- hInstance, // program instance handle
- NULL); // creation parameters
- RECT rect = { 0, 0, 800, 600 }; //Tworzymy prostokat o wymiarach 800x600
- AdjustWindowRect(&rect, GetWindowLong(hWnd, GWL_STYLE), FALSE); //Skalujemy okno tak aby obszar roboczy faktycznie mial 800x600px
- SetWindowPos(hWnd, 0, 0, 0, rect.right - rect.left, rect.bottom - rect.top, SWP_NOZORDER | SWP_NOMOVE);
- GdiplusStartupInput gdiplusStartupInput; //Struktura zawierajaca parametry startowe
- ULONG_PTR gdiplusToken; // Wskaznik pod ktory zostanie przypisany token
- // Initializacja GDI
- GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
- ShowWindow(hWnd, nCmdShow); //Wyswietlamy okno
- UpdateWindow(hWnd);
- /*
- Ponizej znajduje sie petla wiadomosci. Odbieramy
- wiadomosci od systemu za pomoca funkcji GetMessage
- i przekazujemy je do procedury okna.
- */
- while (GetMessage(&msg, NULL, 0, 0)) {
- DispatchMessage(&msg);
- }
- GdiplusShutdown(gdiplusToken);
- return (int)msg.wParam;
- }
- struct MyPixel
- {
- MyPixel()
- {
- mBlackness = 0;
- }
- int mBlackness;
- };
- MyPixel mFrontBuffer[40][30];
- void DrawPixel(HDC hdc, int x, int y, int blackness){
- Graphics graphics(hdc);
- SolidBrush solidBrush(Color(blackness, 0, 0, 0));
- graphics.FillRectangle(&solidBrush, x + 1, y + 1, 19, 19);
- };
- void Present(HDC hdc);
- VOID OnPaint(HDC hdc)
- {
- Graphics graphics(hdc);
- Pen pen(Color(255, 0, 0, 0)); //Konstruktor przyjmuje kolor w formacie ARGB
- //FillTable ();
- for (int i = 0; i< 40; i++){
- graphics.DrawLine(&pen, 20 * i, 0, 20 * i, 600); //argumenty to obiekt Pen, x1, y1, x2, y2
- }
- for (int i = 0; i< 30; i++){
- graphics.DrawLine(&pen, 0, i * 20, 800, i * 20); //argumenty to obiekt Pen, x1, y1, x2, y2
- }
- /*SimpleLine(2, 2, 7, 19, 255);
- Bresenham(1, 1, 7, 19, 255);*/
- Present(hdc);
- //DrawPixel(hdc, 20,20,250);
- }
- void Present(HDC hdc) {
- for (int i = 0; i< 40; i++){
- for (int j = 0; j< 30; j++){
- DrawPixel(hdc, i * 20, j * 20, mFrontBuffer[i][j].mBlackness);
- }
- }
- }
- void Bresenham(int x1, int y1, int x2, int y2, int blackness){
- int dx = x2 - x1;
- int dy = y2 - y1;
- int da = 2 * dy;
- int db = 2 * dy - 2 * dx;
- int d = 2 * dy - dx;
- int y = y1;
- for (int x = x1; x <x2; x++){
- mFrontBuffer[x][y].mBlackness = blackness;
- if (d >0){
- d += db;
- y += 1;
- }
- else {
- d += da;
- }
- }
- /**
- for x in range(dx):
- if d > 0:
- d += delta_B
- y += 1
- else:
- d += delta_A
- **/
- }
- void SimpleLine(int x0, int y0, int x1, int y1, int blackness){
- int dx = x1 - x0;
- int dy = y1 - y0;
- float a = dy / dx;
- float b = y0 - a*x0;
- for (int x = x0; x<x1; x++){
- int y = a*x + b;
- mFrontBuffer[x][y].mBlackness = blackness;
- }
- }
- void FloodFill(int x, int y, int borderColor, int fillColor){
- if (mFrontBuffer[x][y].mBlackness != borderColor && mFrontBuffer[x][y].mBlackness != fillColor){
- mFrontBuffer[x][y].mBlackness = fillColor;
- if (x >= 0 && x <= 40 && y - 1 >= 0 && y - 1 <= 40){
- FloodFill(x, y - 1, borderColor, fillColor);
- }
- if (x >= 0 && x <= 40 && y + 1 >= 0 && y + 1 <= 40){
- FloodFill(x, y + 1, borderColor, fillColor);
- }
- if (x - 1 >= 0 && x - 1 <= 40 && y >= 0 && y <= 40){
- FloodFill(x - 1, y, borderColor, fillColor);
- }
- if (x + 1 >= 0 && x + 1 <= 40 && y >= 0 && y <= 40){
- FloodFill(x + 1, y, borderColor, fillColor);
- }
- }
- }
- //Przelicza stopnie na radiany.
- float DegToRad(float a){
- return a*M_PI / 180;
- }
- Vector2D Multiply(MyMatrix mat, Vector2D vec){
- Vector2D result;
- //Mnoży wektor przez macież, jednak bez wsp. Z
- for (int i = 0; i < 2; i++){
- for (int j = 0; j < 3; j++){
- result.v[i] += vec.v[j] * mat.m[i][j];
- }
- }
- return result;
- }
- MyMatrix MultiplyMatrix(MyMatrix m1, MyMatrix m2){
- MyMatrix result;
- for (int i = 0; i < 3; i++){
- for (int j = 0; j < 3; j++)
- {
- for (int k = 0; k < 3; k++) result.m[i][j] += m1.m[i][k] * m2.m[k][j];
- }
- }
- return result;
- }
- VOID DrawTranslatedPixel(int x, int y, MyMatrix przeksztalcenie){
- //Wypelnia kliknięty piksel szarym kolorem
- mFrontBuffer[x][y].mBlackness = 128;
- Vector2D vector;
- vector.v[0] = x;
- vector.v[1] = y;
- //Przekształca wektor i jeśli po przekształceniu wsp. mieszczą się w oknie to wypełnia pikse o tych wsp. czarnym kolorem
- Vector2D result = Multiply(przeksztalcenie, vector);
- if (result.v[0] >= 0 && result.v[0] < 40 && result.v[1] >= 0 && result.v[1] < 30){
- mFrontBuffer[(int)result.v[0]][(int)result.v[1]].mBlackness = 255;
- }
- }
- LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
- {
- HDC hdc;
- PAINTSTRUCT ps;
- int x;
- int y;
- MyMatrix rotatedMat;
- rotatedMat.Rotate(DegToRad(15.0)); //Macierz obrotu o 15 st.
- MyMatrix translatedMat;
- translatedMat.Translate(5, 5); //Macierz translacji
- switch (msg)
- {
- case WM_LBUTTONDOWN:
- x = LOWORD(lParam);
- y = HIWORD(lParam);
- /*for (int i = 0; i <40; i++){
- for (int j = 0; j<30; j++){
- if (x < i * 20 + 20 && x > i * 20 && y < j * 20 + 20 && y > j * 20){
- mFrontBuffer[i][j].mBlackness = 255;
- }
- }
- }*/
- DrawTranslatedPixel((int)x / 20, (int)y / 20, MultiplyMatrix(rotatedMat, translatedMat));
- RedrawWindow(hwnd, NULL, NULL, RDW_ERASE | RDW_INVALIDATE);
- break;
- case WM_RBUTTONDOWN:
- x = LOWORD(lParam);
- y = HIWORD(lParam);
- /*for (int i = 0; i <40; i++){
- for (int j = 0; j<30; j++){
- if (x < i * 20 + 20 && x > i * 20 && y < j * 20 + 20 && y > j * 20){
- FloodFill(i, j, 255, 128);
- }
- }
- }*/
- DrawTranslatedPixel((int)x / 20, (int)y / 20, MultiplyMatrix(translatedMat, rotatedMat));
- RedrawWindow(hwnd, NULL, NULL, RDW_ERASE | RDW_INVALIDATE);
- break;
- case WM_PAINT:
- hdc = BeginPaint(hwnd, &ps);
- OnPaint(hdc);
- EndPaint(hwnd, &ps);
- return 0;
- case WM_DESTROY:
- PostQuitMessage(0); // Ta funkcja dodaje do kolejki wiadomosci WM_QUIT
- return 0;
- }
- // wszystkie inne wiadomosci sa obslugiwane w sposob domyslny
- return DefWindowProc(hwnd, msg, wParam, lParam);
- }
- //////////////////////////////////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement