Advertisement
towniyan

Untitled

Apr 26th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.57 KB | None | 0 0
  1. #include <iostream>
  2. #include <SFML/Graphics.hpp>
  3.  
  4. using namespace std;
  5.  
  6. bool pointInsideTriangle (sf::Vector2f t1, sf::Vector2f t2, sf::Vector2f t3, int x, int y) {
  7.     float w1 = (t1.x * (t3.y - t1.y) + (y - t1.y) * (t3.x - t1.x) - x * (t3.y - t1.y))
  8.         / (((t2.y - t1.y) * (t3.x - t1.x)) - ((t2.x - t1.x) * (t3.y - t1.y)));
  9.     float w2 = (y - t1.y - w1 * (t2.y - t1.y)) / (t3.y - t1.y);
  10.  
  11.     if (w1 >= 0 && w2 >= 0 && (w1 + w2) <= 1)
  12.         return true;
  13.     return false;
  14. }
  15.  
  16. int main () {
  17.     // create the window
  18.     sf::ContextSettings settings;
  19.     settings.antialiasingLevel = 8;
  20.     sf::RenderWindow window(sf::VideoMode(4*300, 3*300), "New document", sf::Style::Default, settings);
  21.     window.setFramerateLimit(60);
  22.  
  23.     int width = 52, height = 16, xOffset = 50, yOffset = 50, size = 50, thickness = 1;
  24.     sf::ConvexShape* t[height][width];
  25.    
  26.     sf::Color palette[] = {
  27.         sf::Color(255, 255, 255),
  28.         sf::Color(100, 100, 100),
  29.         sf::Color(50, 50, 100),
  30.         sf::Color(255, 10, 10),
  31.         sf::Color(50, 100, 50)
  32.     };
  33.     sf::Color* activeColor = &palette[1];
  34.     sf::Color* backgroundColor = &palette[0];
  35.     short activeColorIndex = 1;
  36.  
  37.     short colorMap[height][width];
  38.  
  39.     for (int i = 0; i < height; i++) {
  40.         for (int j = 0; j < width; j++) {
  41.             colorMap[i][j] = -1;
  42.             int startX = xOffset + (((int) (j / 4)) * (2 * 0.866 * size)),
  43.                 startY = yOffset + (i * size);
  44.  
  45.             t[i][j] = new sf::ConvexShape;
  46.             t[i][j]->setPointCount(3);
  47.             t[i][j]->setFillColor(*backgroundColor);
  48.             t[i][j]->setOutlineColor(sf::Color(50, 50, 100));
  49.             t[i][j]->setOutlineThickness(thickness);
  50.  
  51.             switch (j % 4) {
  52.                 case 0:
  53.                     t[i][j]->setPoint(0, sf::Vector2f(startX, startY));
  54.                     t[i][j]->setPoint(1, sf::Vector2f(startX + (0.866 * size), startY + (size / 2)));
  55.                     t[i][j]->setPoint(2, sf::Vector2f(startX, startY + size));
  56.                     break;
  57.                 case 1:
  58.                     t[i][j]->setPoint(0, sf::Vector2f(startX, startY));
  59.                     t[i][j]->setPoint(1, sf::Vector2f(startX - (0.866 * size), startY + (size / 2)));
  60.                     t[i][j]->setPoint(2, sf::Vector2f(startX, startY + size));
  61.                     break;
  62.                 case 2:
  63.                     t[i][j]->setPoint(0, sf::Vector2f(startX + (0.866 * size), startY + (size / 2)));
  64.                     t[i][j]->setPoint(1, sf::Vector2f(startX, startY));
  65.                     t[i][j]->setPoint(2, sf::Vector2f(startX + (0.866 * size), startY + (size / 2) - size));
  66.                     break;
  67.                 case 3:
  68.                     t[i][j]->setPoint(0, sf::Vector2f(startX + (0.866 * size), startY + (size / 2)));
  69.                     t[i][j]->setPoint(1, sf::Vector2f(startX + (2 * 0.866 * size), startY));
  70.                     t[i][j]->setPoint(2, sf::Vector2f(startX + (0.866 * size), startY + (size / 2) - size));
  71.                     break;
  72.             }
  73.         }
  74.     }
  75.  
  76.     // run the program as long as the window is open
  77.     while (window.isOpen())
  78.     {
  79.         // check all the window's events that were triggered since the last iteration of the loop
  80.         sf::Event event;
  81.         while (window.pollEvent(event))
  82.         {
  83.             // "close requested" event: we close the window
  84.             if (event.type == sf::Event::Closed)
  85.                 window.close();
  86.  
  87.             if (event.type == sf::Event::KeyReleased) {
  88.                 if (event.key.code == sf::Keyboard::Q) {
  89.                     activeColor = &palette[0];
  90.                     activeColorIndex = 0;
  91.                 } else if (event.key.code == sf::Keyboard::W) {
  92.                     activeColor = &palette[1];
  93.                     activeColorIndex = 1;
  94.                 } else if (event.key.code == sf::Keyboard::E) {
  95.                     activeColor = &palette[2];
  96.                     activeColorIndex = 2;
  97.                 } else if (event.key.code == sf::Keyboard::R) {
  98.                     activeColor = &palette[3];
  99.                     activeColorIndex = 3;
  100.                 } else if (event.key.code == sf::Keyboard::T) {
  101.                     activeColor = &palette[4];
  102.                     activeColorIndex = 4;
  103.                 } else if (event.key.code == sf::Keyboard::Z) {
  104.                     for (int i = 0; i < height; i++) {
  105.                         for (int j = 0; j < width; j++) {
  106.                             if (t[i][j]->getOutlineThickness())
  107.                                 t[i][j]->setOutlineThickness(0);
  108.                             else
  109.                                 t[i][j]->setOutlineThickness(thickness);
  110.                         }
  111.                     }
  112.                 }
  113.             }
  114.                
  115.  
  116.             for (int i = 0; i < height; i++) {
  117.                 for (int j = 0; j < width; j++) {
  118.                     if (event.type == sf::Event::MouseMoved) {
  119.                         int x = event.mouseMove.x,
  120.                             y = event.mouseMove.y;
  121.                         if (pointInsideTriangle(t[i][j]->getPoint(0),
  122.                                 t[i][j]->getPoint(1),
  123.                                 t[i][j]->getPoint(2),
  124.                                 x,
  125.                                 y
  126.                         ))
  127.                             t[i][j]->setOutlineColor(*activeColor);
  128.                         else
  129.                             t[i][j]->setOutlineColor(sf::Color(0, 0, 0));
  130.                     }
  131.  
  132.                     if (event.type == sf::Event::MouseButtonReleased
  133.                         && event.mouseButton.button == sf::Mouse::Left) {
  134.                         int x = event.mouseButton.x,
  135.                             y = event.mouseButton.y;
  136.  
  137.                         if (pointInsideTriangle(t[i][j]->getPoint(0),
  138.                             t[i][j]->getPoint(1),
  139.                             t[i][j]->getPoint(2),
  140.                             x,
  141.                             y
  142.                         )) {
  143.                             t[i][j]->setFillColor(*activeColor);
  144.                             colorMap[i][j] = activeColorIndex;
  145.                         }
  146.                     }
  147.                 }
  148.             }
  149.         }
  150.  
  151.         // clear the window with black color
  152.         window.clear(sf::Color::White);
  153.  
  154.         for (int i = 0; i < height; i++) {
  155.             for (int j = 0; j < width; j++) {
  156.                 window.draw(*t[i][j]);
  157.             }
  158.         }
  159.  
  160.         // end the current frame
  161.         window.display();
  162.     }
  163.  
  164.     return 0;
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement