Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML/Graphics.hpp>
- #include <iostream>
- #include <cmath>
- #ifndef M_PI
- #define M_PI 3.14159265358979323846
- #endif
- // Function to calculate gamma value
- float calculateGamma(float alpha, float beta) {
- return std::sqrt((1 + alpha * alpha) / beta);
- }
- // Function to get a point on the ellipse
- sf::Vector2f getEllipsePoint(float t, float alpha, float beta, float gamma, float epsilon) {
- float cosT = std::cos(t);
- float sinT = std::sin(t);
- float denom = gamma * gamma + 2 * alpha * gamma * cosT + beta * cosT * cosT;
- denom = (denom == 0) ? 0.0001f : denom; // Avoid division by zero
- float x = std::sqrt(epsilon / denom) * sinT;
- float y = std::sqrt(epsilon / denom) * cosT;
- return sf::Vector2f(x, y);
- }
- int main() {
- sf::RenderWindow window(sf::VideoMode(1024, 1024), "Elliptical Beam Shape");
- sf::Font font;
- if (!font.loadFromFile("arial.ttf")) {
- std::cerr << "Failed to load font" << std::endl;
- return -1;
- }
- // Scale factors for the ellipse
- float scaleX = 100.0f;
- float scaleY = 100.0f;
- // Form dimensions and position
- const float formWidth = 200.0f;
- const float formHeight = 150.0f;
- sf::Vector2f formPosition((window.getSize().x - formWidth) / 2, (window.getSize().y - formHeight) / 2);
- sf::Vector2f graphPosition((window.getSize().x - formWidth) / 2 + formWidth + 10, (window.getSize().y - formHeight) / 2);
- // Variable Labels
- sf::Text variableLabels[3] = { sf::Text("Alpha:", font, 20), sf::Text("Beta:", font, 20), sf::Text("Epsilon:", font, 20) };
- for (int i = 0; i < 3; ++i) {
- variableLabels[i].setPosition(formPosition.x + 10, formPosition.y + 20 + i * 50);
- }
- // Variable Values
- sf::Text variableValues[3] = { sf::Text("1.0", font, 20), sf::Text("1.0", font, 20), sf::Text("1.0", font, 20) };
- for (int i = 0; i < 3; ++i) {
- variableValues[i].setPosition(formPosition.x + 110, formPosition.y + 20 + i * 50);
- }
- // Input Box Outlines
- sf::RectangleShape inputOutlines[3];
- for (int i = 0; i < 3; ++i) {
- inputOutlines[i].setSize(sf::Vector2f(100, 30));
- inputOutlines[i].setPosition(formPosition.x + 100, formPosition.y + 20 + i * 50);
- inputOutlines[i].setFillColor(sf::Color::Transparent);
- inputOutlines[i].setOutlineColor(sf::Color::White);
- inputOutlines[i].setOutlineThickness(2);
- }
- // Input Field
- sf::Text inputField("", font, 20);
- inputField.setFillColor(sf::Color::White);
- inputField.setPosition(formPosition.x + 100, formPosition.y + 20);
- bool activeInputs[3] = { false }; // Flags to track active input boxes
- // Grid Parameters
- const float gridSpacingX = window.getSize().x / 10.0f;
- const float gridSpacingY = window.getSize().y / 10.0f;
- // Main loop
- while (window.isOpen()) {
- sf::Event event;
- while (window.pollEvent(event)) {
- if (event.type == sf::Event::Closed) {
- window.close();
- }
- else if (event.type == sf::Event::MouseButtonPressed) {
- // Check if mouse is clicked inside any input box
- sf::Vector2i mousePos = sf::Mouse::getPosition(window);
- for (int i = 0; i < 3; ++i) {
- activeInputs[i] = inputOutlines[i].getGlobalBounds().contains(static_cast<float>(mousePos.x), static_cast<float>(mousePos.y));
- }
- }
- else if (event.type == sf::Event::TextEntered) {
- if (event.text.unicode == '\b' && !inputField.getString().isEmpty()) {
- inputField.setString(inputField.getString().substring(0, inputField.getString().getSize() - 1));
- }
- else if ((event.text.unicode >= 48 && event.text.unicode <= 57) || event.text.unicode == '.') {
- inputField.setString(inputField.getString() + static_cast<char>(event.text.unicode));
- }
- }
- else if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Enter) {
- for (int i = 0; i < 3; ++i) {
- if (activeInputs[i]) {
- variableValues[i].setString(inputField.getString());
- inputField.setString("");
- }
- }
- }
- else if (event.type == sf::Event::KeyPressed) {
- if (event.key.code == sf::Keyboard::Up) {
- scaleX *= 1.1f;
- scaleY *= 1.1f;
- }
- else if (event.key.code == sf::Keyboard::Down) {
- scaleX /= 1.1f;
- scaleY /= 1.1f;
- }
- }
- }
- window.clear();
- // Draw grid lines
- sf::Color gridColor(100, 100, 100);
- sf::VertexArray gridLinesX(sf::Lines, 2 * 10);
- sf::VertexArray gridLinesY(sf::Lines, 2 * 10);
- // Vertical grid lines
- for (int i = 0; i < 10; ++i) {
- float xPos = i * gridSpacingX;
- gridLinesX[2 * i].position = sf::Vector2f(xPos, 0);
- gridLinesX[2 * i].color = gridColor;
- gridLinesX[2 * i + 1].position = sf::Vector2f(xPos, window.getSize().y);
- gridLinesX[2 * i + 1].color = gridColor;
- }
- // Horizontal grid lines
- for (int i = 0; i < 10; ++i) {
- float yPos = i * gridSpacingY;
- gridLinesY[2 * i].position = sf::Vector2f(0, yPos);
- gridLinesY[2 * i].color = gridColor;
- gridLinesY[2 * i + 1].position = sf::Vector2f(window.getSize().x, yPos);
- gridLinesY[2 * i + 1].color = gridColor;
- }
- window.draw(gridLinesX);
- window.draw(gridLinesY);
- // Draw labels for grid lines
- sf::Text gridLabelsX[10];
- sf::Text gridLabelsY[10];
- for (int i = 0; i < 10; ++i) {
- float valueX = i * window.getSize().x / 10.0f / scaleX;
- float valueY = i * window.getSize().y / 10.0f / scaleY;
- gridLabelsX[i].setString(std::to_string(valueX));
- gridLabelsX[i].setFont(font);
- gridLabelsX[i].setCharacterSize(12);
- gridLabelsX[i].setFillColor(sf::Color::White);
- gridLabelsX[i].setPosition(i * window.getSize().x / 10.0f, window.getSize().y - 20);
- window.draw(gridLabelsX[i]);
- gridLabelsY[i].setString(std::to_string(valueY));
- gridLabelsY[i].setFont(font);
- gridLabelsY[i].setCharacterSize(12);
- gridLabelsY[i].setFillColor(sf::Color::White);
- gridLabelsY[i].setPosition(5, i * window.getSize().y / 10.0f);
- window.draw(gridLabelsY[i]);
- }
- // Get parameter values
- float alpha = std::stof(variableValues[0].getString().toAnsiString());
- float beta = std::stof(variableValues[1].getString().toAnsiString());
- float epsilon = std::stof(variableValues[2].getString().toAnsiString());
- // Calculate gamma
- float gamma = calculateGamma(alpha, beta);
- // Draw the ellipse
- sf::VertexArray ellipse(sf::LineStrip, 361);
- for (int i = 0; i <= 360; ++i) {
- float t = i * M_PI / 180.0f;
- sf::Vector2f point = getEllipsePoint(t, alpha, beta, gamma, epsilon);
- ellipse[i].position = sf::Vector2f(graphPosition.x + scaleX * point.x, graphPosition.y + scaleY * point.y);
- ellipse[i].color = sf::Color::Green;
- }
- window.draw(ellipse);
- // Draw form elements
- for (int i = 0; i < 3; ++i) {
- window.draw(inputOutlines[i]);
- window.draw(variableLabels[i]);
- window.draw(variableValues[i]);
- }
- window.draw(inputField);
- window.display();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement