Advertisement
VladimirKostovsky

SFML_hell

May 19th, 2024
525
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.83 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <iostream>
  3. #include <cmath>
  4.  
  5. #ifndef M_PI
  6. #define M_PI 3.14159265358979323846
  7. #endif
  8.  
  9. // Function to calculate gamma value
  10. float calculateGamma(float alpha, float beta) {
  11.     return std::sqrt((1 + alpha * alpha) / beta);
  12. }
  13.  
  14. // Function to get a point on the ellipse
  15. sf::Vector2f getEllipsePoint(float t, float alpha, float beta, float gamma, float epsilon) {
  16.     float cosT = std::cos(t);
  17.     float sinT = std::sin(t);
  18.     float denom = gamma * gamma + 2 * alpha * gamma * cosT + beta * cosT * cosT;
  19.     denom = (denom == 0) ? 0.0001f : denom; // Avoid division by zero
  20.     float x = std::sqrt(epsilon / denom) * sinT;
  21.     float y = std::sqrt(epsilon / denom) * cosT;
  22.     return sf::Vector2f(x, y);
  23. }
  24.  
  25. int main() {
  26.     sf::RenderWindow window(sf::VideoMode(1024, 1024), "Elliptical Beam Shape");
  27.     sf::Font font;
  28.     if (!font.loadFromFile("arial.ttf")) {
  29.         std::cerr << "Failed to load font" << std::endl;
  30.         return -1;
  31.     }
  32.  
  33.     // Scale factors for the ellipse
  34.     float scaleX = 100.0f;
  35.     float scaleY = 100.0f;
  36.  
  37.     // Form dimensions and position
  38.     const float formWidth = 200.0f;
  39.     const float formHeight = 150.0f;
  40.     sf::Vector2f formPosition((window.getSize().x - formWidth) / 2, (window.getSize().y - formHeight) / 2);
  41.     sf::Vector2f graphPosition((window.getSize().x - formWidth) / 2 + formWidth + 10, (window.getSize().y - formHeight) / 2);
  42.  
  43.     // Variable Labels
  44.     sf::Text variableLabels[3] = { sf::Text("Alpha:", font, 20), sf::Text("Beta:", font, 20), sf::Text("Epsilon:", font, 20) };
  45.     for (int i = 0; i < 3; ++i) {
  46.         variableLabels[i].setPosition(formPosition.x + 10, formPosition.y + 20 + i * 50);
  47.     }
  48.  
  49.     // Variable Values
  50.     sf::Text variableValues[3] = { sf::Text("1.0", font, 20), sf::Text("1.0", font, 20), sf::Text("1.0", font, 20) };
  51.     for (int i = 0; i < 3; ++i) {
  52.         variableValues[i].setPosition(formPosition.x + 110, formPosition.y + 20 + i * 50);
  53.     }
  54.  
  55.     // Input Box Outlines
  56.     sf::RectangleShape inputOutlines[3];
  57.     for (int i = 0; i < 3; ++i) {
  58.         inputOutlines[i].setSize(sf::Vector2f(100, 30));
  59.         inputOutlines[i].setPosition(formPosition.x + 100, formPosition.y + 20 + i * 50);
  60.         inputOutlines[i].setFillColor(sf::Color::Transparent);
  61.         inputOutlines[i].setOutlineColor(sf::Color::White);
  62.         inputOutlines[i].setOutlineThickness(2);
  63.     }
  64.  
  65.     // Input Field
  66.     sf::Text inputField("", font, 20);
  67.     inputField.setFillColor(sf::Color::White);
  68.     inputField.setPosition(formPosition.x + 100, formPosition.y + 20);
  69.  
  70.     bool activeInputs[3] = { false }; // Flags to track active input boxes
  71.  
  72.     // Grid Parameters
  73.     const float gridSpacingX = window.getSize().x / 10.0f;
  74.     const float gridSpacingY = window.getSize().y / 10.0f;
  75.  
  76.     // Main loop
  77.     while (window.isOpen()) {
  78.         sf::Event event;
  79.         while (window.pollEvent(event)) {
  80.             if (event.type == sf::Event::Closed) {
  81.                 window.close();
  82.             }
  83.             else if (event.type == sf::Event::MouseButtonPressed) {
  84.                 // Check if mouse is clicked inside any input box
  85.                 sf::Vector2i mousePos = sf::Mouse::getPosition(window);
  86.                 for (int i = 0; i < 3; ++i) {
  87.                     activeInputs[i] = inputOutlines[i].getGlobalBounds().contains(static_cast<float>(mousePos.x), static_cast<float>(mousePos.y));
  88.                 }
  89.             }
  90.             else if (event.type == sf::Event::TextEntered) {
  91.                 if (event.text.unicode == '\b' && !inputField.getString().isEmpty()) {
  92.                     inputField.setString(inputField.getString().substring(0, inputField.getString().getSize() - 1));
  93.                 }
  94.                 else if ((event.text.unicode >= 48 && event.text.unicode <= 57) || event.text.unicode == '.') {
  95.                     inputField.setString(inputField.getString() + static_cast<char>(event.text.unicode));
  96.                 }
  97.             }
  98.             else if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Enter) {
  99.                 for (int i = 0; i < 3; ++i) {
  100.                     if (activeInputs[i]) {
  101.                         variableValues[i].setString(inputField.getString());
  102.                         inputField.setString("");
  103.                     }
  104.                 }
  105.             }
  106.             else if (event.type == sf::Event::KeyPressed) {
  107.                 if (event.key.code == sf::Keyboard::Up) {
  108.                     scaleX *= 1.1f;
  109.                     scaleY *= 1.1f;
  110.                 }
  111.                 else if (event.key.code == sf::Keyboard::Down) {
  112.                     scaleX /= 1.1f;
  113.                     scaleY /= 1.1f;
  114.                 }
  115.             }
  116.         }
  117.  
  118.         window.clear();
  119.  
  120.         // Draw grid lines
  121.         sf::Color gridColor(100, 100, 100);
  122.         sf::VertexArray gridLinesX(sf::Lines, 2 * 10);
  123.         sf::VertexArray gridLinesY(sf::Lines, 2 * 10);
  124.  
  125.         // Vertical grid lines
  126.         for (int i = 0; i < 10; ++i) {
  127.             float xPos = i * gridSpacingX;
  128.             gridLinesX[2 * i].position = sf::Vector2f(xPos, 0);
  129.             gridLinesX[2 * i].color = gridColor;
  130.             gridLinesX[2 * i + 1].position = sf::Vector2f(xPos, window.getSize().y);
  131.             gridLinesX[2 * i + 1].color = gridColor;
  132.         }
  133.  
  134.         // Horizontal grid lines
  135.         for (int i = 0; i < 10; ++i) {
  136.             float yPos = i * gridSpacingY;
  137.             gridLinesY[2 * i].position = sf::Vector2f(0, yPos);
  138.             gridLinesY[2 * i].color = gridColor;
  139.             gridLinesY[2 * i + 1].position = sf::Vector2f(window.getSize().x, yPos);
  140.             gridLinesY[2 * i + 1].color = gridColor;
  141.         }
  142.  
  143.         window.draw(gridLinesX);
  144.         window.draw(gridLinesY);
  145.  
  146.         // Draw labels for grid lines
  147.         sf::Text gridLabelsX[10];
  148.         sf::Text gridLabelsY[10];
  149.         for (int i = 0; i < 10; ++i) {
  150.             float valueX = i * window.getSize().x / 10.0f / scaleX;
  151.             float valueY = i * window.getSize().y / 10.0f / scaleY;
  152.             gridLabelsX[i].setString(std::to_string(valueX));
  153.             gridLabelsX[i].setFont(font);
  154.             gridLabelsX[i].setCharacterSize(12);
  155.             gridLabelsX[i].setFillColor(sf::Color::White);
  156.             gridLabelsX[i].setPosition(i * window.getSize().x / 10.0f, window.getSize().y - 20);
  157.             window.draw(gridLabelsX[i]);
  158.  
  159.             gridLabelsY[i].setString(std::to_string(valueY));
  160.             gridLabelsY[i].setFont(font);
  161.             gridLabelsY[i].setCharacterSize(12);
  162.             gridLabelsY[i].setFillColor(sf::Color::White);
  163.             gridLabelsY[i].setPosition(5, i * window.getSize().y / 10.0f);
  164.             window.draw(gridLabelsY[i]);
  165.         }
  166.  
  167.         // Get parameter values
  168.         float alpha = std::stof(variableValues[0].getString().toAnsiString());
  169.         float beta = std::stof(variableValues[1].getString().toAnsiString());
  170.         float epsilon = std::stof(variableValues[2].getString().toAnsiString());
  171.  
  172.         // Calculate gamma
  173.         float gamma = calculateGamma(alpha, beta);
  174.  
  175.         // Draw the ellipse
  176.         sf::VertexArray ellipse(sf::LineStrip, 361);
  177.         for (int i = 0; i <= 360; ++i) {
  178.             float t = i * M_PI / 180.0f;
  179.             sf::Vector2f point = getEllipsePoint(t, alpha, beta, gamma, epsilon);
  180.             ellipse[i].position = sf::Vector2f(graphPosition.x + scaleX * point.x, graphPosition.y + scaleY * point.y);
  181.             ellipse[i].color = sf::Color::Green;
  182.         }
  183.         window.draw(ellipse);
  184.  
  185.         // Draw form elements
  186.         for (int i = 0; i < 3; ++i) {
  187.             window.draw(inputOutlines[i]);
  188.             window.draw(variableLabels[i]);
  189.             window.draw(variableValues[i]);
  190.         }
  191.         window.draw(inputField);
  192.  
  193.         window.display();
  194.     }
  195.  
  196.     return 0;
  197. }
  198.  
  199.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement