Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
2.7 kB
2
Indexable
Never
#include <SFML/Graphics.hpp>
#include <iostream>
#include <cmath>

using namespace sf;

const int width = 602;
const int height = 602;
const double g = 9.81;

int main() {
	setlocale(LC_ALL, "Russian");
	double velocity;
	double angle_of_launch;
	double initial_height;

	std::cout << "Задай скорость: ";
	std::cin >> velocity;
	std::cout << "Задай угол запуска: ";
	std::cin >> angle_of_launch;
	std::cout << "Задай начальную высоту: ";
	std::cin >> initial_height;

	RenderWindow window(VideoMode(width, height), "Trajectory of a projectile");

	int x0 = width / 2;
	int y0 = height / 2;

	CircleShape point(2.f);
	point.setFillColor(Color::Red);

	int o1 = -10;
	int o2 = 10;
	float c = 100;
	int quantity_of_points = ((o1) * (-1) + o2) * c + 1;
	int scale = 30;

	RectangleShape line[40];
	for (int i = 0; i < 40; i++) {
		line[i].setSize(Vector2f(1, 20));
		line[i].setFillColor(Color::Black);
		if (i < 20) {
			if (i < 10)
				line[i].setPosition(x0 - (i + 1) * scale, y0 - 10);
			else
				line[i].setPosition(x0 + (i - 9) * scale, y0 - 10);
		}
		else {
			line[i].setRotation(90);
			if (i < 30)
				line[i].setPosition(x0 + 10, y0 + (i - 30) * scale);
			else
				line[i].setPosition(x0 + 10, y0 + (i - 29) * scale);
		}
	}

	RectangleShape OsX(Vector2f(width, 1));
	OsX.setFillColor(Color::Black);
	OsX.setPosition(0, y0);

	RectangleShape OsY(Vector2f(1, height));
	OsY.setFillColor(Color::Black);
	OsY.setPosition(x0, 0);

	RectangleShape strel[4];
	for (int i = 0; i < 4; i++) {
		strel[i].setSize(Vector2f(1, 25));
		strel[i].setFillColor(Color::Black);

		if (i < 2)
			strel[i].setPosition(x0, 0);
		else
			strel[i].setPosition(width, y0);
	}
	strel[0].setRotation(25);
	strel[1].setRotation(-25);
	strel[2].setRotation(60);
	strel[3].setRotation(-250);

	int anim = 0;

	while (window.isOpen()) {
		Event event;
		while (window.pollEvent(event)) {
			if (event.type == Event::Closed) window.close();
		}
		if (anim < quantity_of_points) anim += 5;

		window.clear(Color::White);
		window.draw(OsX);
		window.draw(OsY);

		for (int i = 0; i < 4; i++)
			window.draw(strel[i]);
		for (int i = 0; i < 40; i++)
			if (i != 19 && i != 20)
				window.draw(line[i]);
		for (int i = 0; i < anim; i++) {
			double x = o1 + i / c;
			double y = initial_height + x * tan(angle_of_launch) - g * pow(x, 2) / (2 * pow(velocity, 2) * pow(cos(angle_of_launch), 2));

			double x1 = x0 + x * scale;
			double y1 = y0 - y * scale;

			point.setPosition(x1, y1);
			window.draw(point);
		}
		window.display();
	}
}
Leave a Comment