0% found this document useful (0 votes)
3 views

code1

This document is a C++ program using the SFML library to create a graphical simulation of Earth and satellites. It initializes a window displaying a blue circle representing Earth and multiple white circles representing satellites orbiting around it at varying speeds. The program continuously updates and renders the positions of the satellites in a loop until the window is closed.

Uploaded by

kthamayanthi8
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

code1

This document is a C++ program using the SFML library to create a graphical simulation of Earth and satellites. It initializes a window displaying a blue circle representing Earth and multiple white circles representing satellites orbiting around it at varying speeds. The program continuously updates and renders the positions of the satellites in a loop until the window is closed.

Uploaded by

kthamayanthi8
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <SFML/Graphics.

hpp>

#include <cmath>

#include <vector>

const int WINDOW_WIDTH = 800;

const int WINDOW_HEIGHT = 600;

const float EARTH_RADIUS = 50.0f;

const float SATELLITE_RADIUS = 5.0f;

const float ORBIT_RADIUS = 150.0f;

const int NUM_SATELLITES = 10;

// Structure to represent a satellite

struct Satellite {

float angle; // Current angle in the orbit

float speed; // Speed of revolution

};

int main() {

// Create the SFML window

sf::RenderWindow window(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), "Earth and


Satellites");

// Create the Earth (a blue circle)

sf::CircleShape earth(EARTH_RADIUS);

earth.setFillColor(sf::Color::Blue);

earth.setOrigin(EARTH_RADIUS, EARTH_RADIUS); // Set origin to the center

earth.setPosition(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2);

// Create satellites

std::vector<Satellite> satellites(NUM_SATELLITES);

for (int i = 0; i < NUM_SATELLITES; ++i) {


satellites[i].angle = i * (360.0f / NUM_SATELLITES); // Evenly distribute satellites

satellites[i].speed = 0.5f + (i % 3) * 0.2f; // Different speeds for variety

// Main loop

while (window.isOpen()) {

// Handle events

sf::Event event;

while (window.pollEvent(event)) {

if (event.type == sf::Event::Closed) {

window.close();

// Clear the window

window.clear();

// Draw the Earth

window.draw(earth);

// Draw and update the position of each satellite

for (int i = 0; i < NUM_SATELLITES; ++i) {

// Calculate the satellite's position

float x = WINDOW_WIDTH / 2 + ORBIT_RADIUS * std::cos(satellites[i].angle * 3.14159f /


180.0f);

float y = WINDOW_HEIGHT / 2 + ORBIT_RADIUS * std::sin(satellites[i].angle * 3.14159f /


180.0f);

// Create a satellite (a small white circle)

sf::CircleShape satellite(SATELLITE_RADIUS);

satellite.setFillColor(sf::Color::White);

satellite.setOrigin(SATELLITE_RADIUS, SATELLITE_RADIUS); // Set origin to the center


satellite.setPosition(x, y);

// Draw the satellite

window.draw(satellite);

// Update the satellite's angle

satellites[i].angle += satellites[i].speed;

if (satellites[i].angle >= 360.0f) {

satellites[i].angle -= 360.0f;

// Display the window

window.display();

// Add a small delay to control the speed of the animation

sf::sleep(sf::milliseconds(10));

return 0;

You might also like