0% found this document useful (0 votes)
13 views4 pages

Computergraphics Project

This document contains a C++ program that simulates the umbra and penumbra effects created by the sun, earth, and moon using the SFML graphics library. It calculates the distances between these celestial bodies and draws their shadows on a window. The program runs in a loop, continuously rendering the sun, earth, moon, and their respective shadows until the window is closed.

Uploaded by

nasrin.akter2ne1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views4 pages

Computergraphics Project

This document contains a C++ program that simulates the umbra and penumbra effects created by the sun, earth, and moon using the SFML graphics library. It calculates the distances between these celestial bodies and draws their shadows on a window. The program runs in a loop, continuously rendering the sun, earth, moon, and their respective shadows until the window is closed.

Uploaded by

nasrin.akter2ne1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <iostream>

#include <cmath>

#include <SFML/Graphics.hpp>

using namespace std;

using namespace sf;

// Function to calculate the distance between two points (x1, y1) and (x2, y2)

double distance(double x1, double y1, double x2, double y2) {

return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));

// Function to draw the umbra and penumbra

void drawUmbraPenumbra(RenderWindow& window, CircleShape& sun, CircleShape& earth,


CircleShape& moon, double sunRadius, double earthRadius, double moonRadius) {

// Calculate the distance between the sun and the earth

double sunEarthDistance = distance(sun.getPosition().x, sun.getPosition().y, earth.getPosition().x,


earth.getPosition().y);

// Calculate the distance between the earth and the moon

double earthMoonDistance = distance(earth.getPosition().x, earth.getPosition().y,


moon.getPosition().x, moon.getPosition().y);

// Calculate the length of the umbra (full shadow)

double umbraLength = sunEarthDistance * (sunRadius - moonRadius) / (sunRadius + moonRadius);

// Calculate the length of the penumbra (partial shadow)

double penumbraLength = sunEarthDistance * (sunRadius + moonRadius) / (sunRadius - moonRadius);

// Draw the umbra (full shadow)


RectangleShape umbra(Vector2f(umbraLength, 10)); // Length of the umbra and height of the shadow

umbra.setFillColor(Color::Black); // Black color for the umbra

umbra.setPosition(earth.getPosition().x + earthRadius + earthMoonDistance - umbraLength,


earth.getPosition().y - 5);

window.draw(umbra);

// Draw the penumbra (partial shadow)

RectangleShape penumbra(Vector2f(penumbraLength, 10)); // Length of the penumbra and height of


the shadow

penumbra.setFillColor(Color(0, 0, 0, 128)); // Semi-transparent black for the penumbra

penumbra.setPosition(earth.getPosition().x + earthRadius + earthMoonDistance - penumbraLength,


earth.getPosition().y - 5);

window.draw(penumbra);

int main() {

// Create a window to display the simulation

RenderWindow window(VideoMode(800, 600), "Umbra and Penumbra Simulation");

// Create Circle shapes for the sun, earth, and moon

CircleShape sun(50); // Sun with a radius of 50

sun.setFillColor(Color::Yellow); // Sun's color is yellow

sun.setPosition(200, 300); // Position the sun at (200, 300)

CircleShape earth(30); // Earth with a radius of 30

earth.setFillColor(Color::Blue); // Earth is blue

earth.setPosition(400, 300); // Position the earth at (400, 300)

CircleShape moon(15); // Moon with a radius of 15

moon.setFillColor(Color::White); // Moon is white


moon.setPosition(450, 300); // Position the moon at (450, 300)

// Main loop to keep the window open and render objects

while (window.isOpen()) {

Event event;

while (window.pollEvent(event)) {

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

window.close(); // Close the window if the user clicks the close button

// Clear the window with a cyan color (sky background)

window.clear(Color::Cyan);

// Draw the sun, earth, and moon

window.draw(sun);

window.draw(earth);

window.draw(moon);

// Draw the umbra and penumbra

drawUmbraPenumbra(window, sun, earth, moon, sun.getRadius(), earth.getRadius(),


moon.getRadius());

// Display everything that has been drawn on the window

window.display();

return 0; // Exit the program

}
https://github.com/JoreKut/SNAKE/blob/master/MAAAZE/Source.cpp

You might also like