<sub class="descriptionSection">04-12-2024 01:21:pm // #Tag // [[Programmierung]]</sub>
____
## Aufgabe 1
```cpp
#include <SFML/Graphics.hpp>
using namespace sf;
int main()
{
RenderWindow window(VideoMode(200, 200), "Clock");
CircleShape shape(100.f);
shape.setFillColor(Color::Green);
RectangleShape rectangle(sf::Vector2f(70.f, 10.f));
RectangleShape rectangle2(sf::Vector2f(6.f, -120.f));
rectangle.setPosition(100.f, 95.f);
rectangle2.setPosition(100.f, 97.f);
rectangle.setFillColor(Color::Red);
rectangle2.setFillColor(Color::Blue);
while (window.isOpen())
{
Event event;
while (window.pollEvent(event))
{
if (event.type == Event::Closed)
window.close();
}
window.clear();
window.draw(shape);
window.draw(rectangle);
window.draw(rectangle2);
window.display();
}
return 0;
}
```
## Aufgabe 2
(main.cpp)
```cpp
#include <SFML/Graphics.hpp>
#include "./lib/sfml/sfmlLibFuncs.h"
#include <vector>
#include <iostream>
#include <cmath>
using namespace sf;
int main(){
RenderWindow window(VideoMode(400, 400), "Entsperrmuster");
std::vector<std::pair<std::pair<int,int>, CircleShape>> circles;
//numeration of circles
//0 1 2 //3 4 5 //6 7 8 std::vector<int> pattern = {0, 5, 5, 8, 8, 6};
for(int i = 0; i < 9; i++){
CircleShape circle(50.f);
circle.setOutlineColor(Color::Black);
circle.setOutlineThickness(5.f);
//push back the circle
//pair is initialized with 0,0 as we do not yet know the position of the circle circles.push_back(std::make_pair(std::make_pair(0,0), circle));
}
View view = window.getDefaultView();
while(window.isOpen()){
Event event;
//determine reference side (shorter one)
float reference = window.getSize().x;
//std::cout << "X: " << window.getSize().x << " Y: " << window.getSize().y << std::endl;
if(view.getSize().y < window.getSize().x){
reference = window.getSize().y;
//std::cout << "Referencing Y" << std::endl;
}
int i = 0;
int distance = reference / 4;
if(distance < 1){
distance = 1;
}
float size = distance/5;
//View view = window.getView();
while(window.pollEvent(event)){
if(event.type == Event::Closed){
window.close();
}
else if (event.type == sf::Event::Resized){
view.setSize({
static_cast<float>(event.size.width),
static_cast<float>(event.size.height)
});
window.setView(view);
//align and resize the circles
//std::cout << "Resized: " << event.size.width << " " << event.size.height << std::endl;/* for(auto &circle : circles){
int x = distance * (i % 3); int y = distance * (i / 3); std::cout << "X: " << x << " Y: " << y << std::endl; std::cout << window.mapPixelToCoords(sf::Vector2i{x, y}).x << " " << window.mapPixelToCoords(sf::Vector2i{x, y}).y << std::endl; circle.setPosition(window.mapPixelToCoords(sf::Vector2i{x, y})); circle.setRadius(size); i++; }*/ }
}
window.clear(Color::White);
std::vector<std::pair<float,float>> coords = {};
for(auto &circle : circles){
float x = distance * (i % 3) + window.getSize().x / 5;
float y = distance * (i / 3) + window.getSize().y / 5;
circle = circles[i];
circle.second.setPosition(window.mapPixelToCoords({static_cast<int>(x), static_cast<int>(y)}));
circle.second.setRadius(size);
window.draw(circle.second);
circles[i] = std::make_pair(std::make_pair(x+size,y+size), circle.second);
i++;
}
bool isSecond = false;
for(int y = 0; y < pattern.size(); y++){
auto taggedCircle = pattern[y];
if(isSecond){
isSecond = false;
continue;
}
RectangleShape line = drawLine({circles[taggedCircle].first.first, circles[taggedCircle].first.second}, {circles[pattern[y+1]].first.first, circles[pattern[y+1]].first.second}, window, 5, Color::Red);
window.draw(line);
isSecond = true;
}
RectangleShape testLine = drawLine({circles[0].first.first, circles[0].first.second}, {circles[5].first.first, circles[5].first.second}, window, 5, Color::Red);
//window.draw(testLine);
window.display();
}
return 0;
}
```
(sfmllibFuncs.h)
```cpp
//
// Created by berg on 05.12.24.
//
#include <SFML/Graphics.hpp>
#include <cmath>
#pragma once
#ifndef SFMLLIBFUNCS_H
#define SFMLLIBFUNCS_H
//draws a line from start to end with a given thickness and color
sf::RectangleShape drawLine(sf::Vector2f start, sf::Vector2f end, const sf::RenderWindow &window, float thickness = 10, sf::Color color = sf::Color::Black){
sf::RectangleShape line(sf::Vector2f(10.f, thickness));
//calculate length of x and y
float x = end.x - start.x;
float y = end.y - start.y;
//if x or y is below 0 we need to invert
if(x < 0){
x = x * -1;
}
if(y < 0){
y = y * -1;
}
//calculate the angle
float alpha = atan(y/x)*180/M_PI;
//calculate the length
float length = sqrt(pow(x, 2) + pow(y, 2));
//if the angle is 0 or 90 we need to adjust
if(alpha == -90 || alpha == 0){
alpha = alpha + 180;
}
//set the rotation
line.setRotation(alpha);
//set the length
line.setSize({length, thickness});
line.setFillColor(color);
line.setPosition(window.mapPixelToCoords({static_cast<int>(start.x), static_cast<int>(start.y)}));
return line;
}
#endif //SFMLLIBFUNCS_H
```
## Aufgabe 3
```cpp
#include <SFML/Graphics.hpp>
//#include "./lib/sfml/sfmlLibFuncs.h"
#include <vector>
#include <iostream>
#include <cmath>
#include <stack>
#include <random>
using namespace sf;
int main(){
RenderWindow window(VideoMode(800, 600), "SFML");
window.setFramerateLimit(60);
std::stack<CircleShape> circles;
int x = 0,y = 0, i = 0;
srand(time(NULL));
while(window.isOpen() && i < 500){
window.clear();
CircleShape circle(10);
circle.setPosition(x, y);
circle.setFillColor(Color::Red);
circles.push(circle);
window.draw(circles.top());
window.display();
int randomNum = rand() % 10;
if(randomNum < 7){
x++;
}
else{
y++;
}
i++;
}
while(circles.size() > 0){
window.clear();
window.draw(circles.top());
window.display();
circles.pop();
}
return 0;
}
```