Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <SFML/Graphics.hpp>
- using namespace std;
- bool pointInsideTriangle (sf::Vector2f t1, sf::Vector2f t2, sf::Vector2f t3, int x, int y) {
- float w1 = (t1.x * (t3.y - t1.y) + (y - t1.y) * (t3.x - t1.x) - x * (t3.y - t1.y))
- / (((t2.y - t1.y) * (t3.x - t1.x)) - ((t2.x - t1.x) * (t3.y - t1.y)));
- float w2 = (y - t1.y - w1 * (t2.y - t1.y)) / (t3.y - t1.y);
- if (w1 >= 0 && w2 >= 0 && (w1 + w2) <= 1)
- return true;
- return false;
- }
- int main () {
- // create the window
- sf::ContextSettings settings;
- settings.antialiasingLevel = 8;
- sf::RenderWindow window(sf::VideoMode(4*300, 3*300), "New document", sf::Style::Default, settings);
- window.setFramerateLimit(60);
- int width = 52, height = 16, xOffset = 50, yOffset = 50, size = 50, thickness = 1;
- sf::ConvexShape* t[height][width];
- sf::Color palette[] = {
- sf::Color(255, 255, 255),
- sf::Color(100, 100, 100),
- sf::Color(50, 50, 100),
- sf::Color(255, 10, 10),
- sf::Color(50, 100, 50)
- };
- sf::Color* activeColor = &palette[1];
- sf::Color* backgroundColor = &palette[0];
- short activeColorIndex = 1;
- short colorMap[height][width];
- for (int i = 0; i < height; i++) {
- for (int j = 0; j < width; j++) {
- colorMap[i][j] = -1;
- int startX = xOffset + (((int) (j / 4)) * (2 * 0.866 * size)),
- startY = yOffset + (i * size);
- t[i][j] = new sf::ConvexShape;
- t[i][j]->setPointCount(3);
- t[i][j]->setFillColor(*backgroundColor);
- t[i][j]->setOutlineColor(sf::Color(50, 50, 100));
- t[i][j]->setOutlineThickness(thickness);
- switch (j % 4) {
- case 0:
- t[i][j]->setPoint(0, sf::Vector2f(startX, startY));
- t[i][j]->setPoint(1, sf::Vector2f(startX + (0.866 * size), startY + (size / 2)));
- t[i][j]->setPoint(2, sf::Vector2f(startX, startY + size));
- break;
- case 1:
- t[i][j]->setPoint(0, sf::Vector2f(startX, startY));
- t[i][j]->setPoint(1, sf::Vector2f(startX - (0.866 * size), startY + (size / 2)));
- t[i][j]->setPoint(2, sf::Vector2f(startX, startY + size));
- break;
- case 2:
- t[i][j]->setPoint(0, sf::Vector2f(startX + (0.866 * size), startY + (size / 2)));
- t[i][j]->setPoint(1, sf::Vector2f(startX, startY));
- t[i][j]->setPoint(2, sf::Vector2f(startX + (0.866 * size), startY + (size / 2) - size));
- break;
- case 3:
- t[i][j]->setPoint(0, sf::Vector2f(startX + (0.866 * size), startY + (size / 2)));
- t[i][j]->setPoint(1, sf::Vector2f(startX + (2 * 0.866 * size), startY));
- t[i][j]->setPoint(2, sf::Vector2f(startX + (0.866 * size), startY + (size / 2) - size));
- break;
- }
- }
- }
- // run the program as long as the window is open
- while (window.isOpen())
- {
- // check all the window's events that were triggered since the last iteration of the loop
- sf::Event event;
- while (window.pollEvent(event))
- {
- // "close requested" event: we close the window
- if (event.type == sf::Event::Closed)
- window.close();
- if (event.type == sf::Event::KeyReleased) {
- if (event.key.code == sf::Keyboard::Q) {
- activeColor = &palette[0];
- activeColorIndex = 0;
- } else if (event.key.code == sf::Keyboard::W) {
- activeColor = &palette[1];
- activeColorIndex = 1;
- } else if (event.key.code == sf::Keyboard::E) {
- activeColor = &palette[2];
- activeColorIndex = 2;
- } else if (event.key.code == sf::Keyboard::R) {
- activeColor = &palette[3];
- activeColorIndex = 3;
- } else if (event.key.code == sf::Keyboard::T) {
- activeColor = &palette[4];
- activeColorIndex = 4;
- } else if (event.key.code == sf::Keyboard::Z) {
- for (int i = 0; i < height; i++) {
- for (int j = 0; j < width; j++) {
- if (t[i][j]->getOutlineThickness())
- t[i][j]->setOutlineThickness(0);
- else
- t[i][j]->setOutlineThickness(thickness);
- }
- }
- }
- }
- for (int i = 0; i < height; i++) {
- for (int j = 0; j < width; j++) {
- if (event.type == sf::Event::MouseMoved) {
- int x = event.mouseMove.x,
- y = event.mouseMove.y;
- if (pointInsideTriangle(t[i][j]->getPoint(0),
- t[i][j]->getPoint(1),
- t[i][j]->getPoint(2),
- x,
- y
- ))
- t[i][j]->setOutlineColor(*activeColor);
- else
- t[i][j]->setOutlineColor(sf::Color(0, 0, 0));
- }
- if (event.type == sf::Event::MouseButtonReleased
- && event.mouseButton.button == sf::Mouse::Left) {
- int x = event.mouseButton.x,
- y = event.mouseButton.y;
- if (pointInsideTriangle(t[i][j]->getPoint(0),
- t[i][j]->getPoint(1),
- t[i][j]->getPoint(2),
- x,
- y
- )) {
- t[i][j]->setFillColor(*activeColor);
- colorMap[i][j] = activeColorIndex;
- }
- }
- }
- }
- }
- // clear the window with black color
- window.clear(sf::Color::White);
- for (int i = 0; i < height; i++) {
- for (int j = 0; j < width; j++) {
- window.draw(*t[i][j]);
- }
- }
- // end the current frame
- window.display();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement