<sub class="descriptionSection">08-01-2025 09:18:am // #Tag // [[Programmierung]]</sub>
____
main.cpp
```cpp
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <map>
#include "lib/utils/morsecode.h"
#include "lib/sfml/sfmlLibFuncs.h"
using namespace std;
void addCircle(int x, int y, int xDistance, int yDistance, int depth, int parentIndex, boerg::Window &window){
int completeXDistance = pow(depth, 2) * xDistance;
std::cout << window.getDimensions().first << std::endl;
//check ob die komplette länge aller Kreise die Breite des Fensters überschreitet
if(completeXDistance > window.getDimensions().first - 100){
std::cout << pow(depth - 1, 2) * xDistance << " " << depth << std::endl;
std::cout << pow(depth - 1, 2) << " " << xDistance << endl;
return;
}
//füge einen kreis links vom parent kreis hinzu
window.addCircle({x - xDistance, y + yDistance}, 25 / depth, sf::Color::Black);
//get alle kreise die bisher erstellt wurden
auto circles = window.getCircles();
//verbinde den parent kreis mit dem kreis der grade erstellt wurde
window.connect(parentIndex, circles.size() - 1, 10 / depth, sf::Color::Black);
//check ob die abstände zu klein sind
int newYDistance = yDistance / 2;
int newXDistance = xDistance / 2;
if(yDistance < 30){
newYDistance = 30;
}
if(xDistance < 30){
newXDistance = 30;
}
//calle die funktion rekursiv für den kreis der grade erstellt wurde
addCircle(x - xDistance, y + yDistance, newXDistance, newYDistance, depth + 1, circles.size() - 1, window);
//füge einen kreis rechts vom parent kreis hinzu und dann macht das selbe wieder wie oben
window.addCircle({x + xDistance, y + yDistance}, 25 / depth, sf::Color::Black);
circles = window.getCircles();
window.connect(parentIndex, circles.size() - 1, 10 / depth, sf::Color::Black);
addCircle(x + xDistance, y + yDistance, newXDistance, newYDistance, depth + 1, circles.size() - 1, window);
}
int main(){
//initialisiert das fenster mit meiner eigenen klasse weil sfml dumm ist
auto window = boerg::Window(1920, 800, "Hello World", sf::Color::White);
//initialisiert die position des ersten kreises
int x = window.getDimensions().first / 2;
int y = 100;
//setzt die x und y distanzen der kreise
int xDistance = 200;
int yDistance = 200;
window.addCircle({x, y}, 25, sf::Color::Black);
addCircle(x, y, xDistance, yDistance, 1, 0, window);
while(window.isOpen()){
sf::Event event;
while(window.pollEvent(event)){
if(event.type == sf::Event::Closed){
window.close();
}
}
//zeige das fenster an
window.render();
}
return 0;
}
```
lib/sfml/sfmlLibFuncs.h
```cpp
#include <SFML/Graphics.hpp>
#include <cmath>
#include <vector>
#include <math.h>
#pragma once
#ifndef SFMLLIBFUNCS_H
#define SFMLLIBFUNCS_H
#define _USE_MATH_DEFINES
namespace boerg {
int getRandomInt(int min = 0, int max = 1){
srand(time(nullptr));
return rand() % (max - min + 1) + min;
};
std::vector<int> getRandomIntVector(int size, int min = 0, int max = 1){
std::vector<int> randomNumbers;
for(int i = 0; i < size; i++){
randomNumbers.push_back(getRandomInt(min, max));
}
return randomNumbers;
};
double getRandomDouble(double min = 0, double max = 1){
srand(time(nullptr));
return min + static_cast <double> (rand()) /( static_cast <double> (RAND_MAX/(max-min)));
};
std::vector<double> getRandomDoubleVector(int size, double min = 0, double max = 1){
std::vector<double> randomNumbers;
for(int i = 0; i < size; i++){
randomNumbers.push_back(getRandomDouble(min, max));
}
return randomNumbers;
};
//draws a line from start to end with a given thickness and color
sf::RectangleShape drawLine(std::pair<int, int> start, std::pair<int, int> 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.first - start.first;
float y = end.second - start.second;
//calculate the angle
float alpha = atan(y/x)*180/M_PI;
if(x < 0){
alpha = alpha + 180;
}
//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.first), static_cast<int>(start.second)}));
return line;
}
//line klasse
class Line {
private:
sf::RectangleShape line;
std::pair<int, int> start;
std::pair<int, int> end;
sf::Color color = sf::Color::Black;
int thickness = 10;
public:
//initialisiert die linie
Line(std::pair<int, int> start, std::pair<int, int> end, const sf::RenderWindow &window, float thickness = 10, sf::Color color = sf::Color::Black){
this->start = start;
this->end = start;
this->color = color;
this->thickness = thickness;
this->line = drawLine({start.first, start.second}, {end.first, end.second}, window, thickness, color);
}
//gibt die linie zurück
sf::RectangleShape getLine(){
return this->line;
}
//gibt die start und end position der linie zurück
std::pair<int, int> getStart(){
return this->start;
};
std::pair<int, int> getEnd(){
return this->end;
};
};
//circle klasse
class Circle{
private:
sf::CircleShape circle;
std::pair<int, int> actualPosition;
std::pair<int, int> initPosition;
int radius;
sf::Color color = sf::Color::Black;
public:
//initialisiert den kreis
Circle(sf::Vector2f position, int radius, const sf::RenderWindow &window, sf::Color color = sf::Color::Black){
this->color = color;
this->actualPosition = std::make_pair(position.x - radius, position.y - radius);
this->initPosition = std::make_pair(position.x, position.y);
this->radius = radius;
this->circle = sf::CircleShape(radius);
this->circle.setFillColor(color);
this->circle.setPosition(window.mapPixelToCoords({this->actualPosition.first, this->actualPosition.second}));
}
//gibt den kreis zurück
sf::CircleShape getCircle(){
return this->circle;
}
//gibt die position des kreises zurück
std::pair<int, int> getPosition(){
return this->initPosition;
}
//gibt den radius des kreises zurück
int getRadius(){
return this->radius;
}
};
//fenster klasse die utility funktionen enthält
class Window {
private:
sf::RenderWindow window;
std::vector<Line> lines = {};
std::vector<Circle> circles = {};
sf::Color backgroundColor = sf::Color::White;
std::pair<int, int> dimensions = {800, 800};
public:
//initialisiert das fenster
Window(int width, int height, std::string title, sf::Color backgroundColor = sf::Color::White, int framerate = 60):backgroundColor(backgroundColor){
this->window.create(sf::VideoMode(width, height), title);
this->dimensions = {width, height};
this->window.setFramerateLimit(framerate);
}
//zeigt alle gespeicherten kreise und linien an
void render(){
this->window.clear(this->backgroundColor);
for(auto &line : this->lines){
this->window.draw(line.getLine());
}
for(auto &circle : this->circles){
this->window.draw(circle.getCircle());
}
this->window.display();
}
//TODO: implement this
void onResize(void callback()){
callback();
};
//verbinde zwei kreise mit einer linie
void connect(int index1, int index2, int thickness = 10, sf::Color color = sf::Color::Black){
this->lines.push_back(Line(this->circles[index1].getPosition(), this->circles[index2].getPosition(), this->window, thickness, color));
}
//gibt alle gespeicherten kreise zurück
std::vector<boerg::Circle> getCircles(){
return this->circles;
}
//gibt alle gespeicherten linien zurück
std::vector<boerg::Line> getLines(){
return this->lines;
}
//macht das fenster zu
void close(){
this->window.close();
}
//gibt die dimensionen des fensters zurück
std::pair<int, int> getDimensions(){
return this->dimensions;
}
//fügt eine linie hinzu
void addLine(std::pair<int, int> start, std::pair<int, int> end, float thickness = 10, sf::Color color = sf::Color::Black){
this->lines.push_back(Line(start, end, this->window, thickness, color));
}
//fügt einen kreis hinzu
void addCircle(sf::Vector2f position, float radius, sf::Color color = sf::Color::Black){
this->circles.push_back(Circle(position, radius, this->window, color));
}
//gibt zurück ob das fenster offen ist
bool isOpen(){
return this->window.isOpen();
}
//pollt events
bool pollEvent(sf::Event &event){
return this->window.pollEvent(event);
}
//funktion die events handelt wie das schließen des fensters
void events(){
sf::Event event;
while(this->window.pollEvent(event)){
if(event.type == sf::Event::Closed){
this->window.close();
}
if(event.type == sf::Event::Resized){
this->window.setSize(sf::Vector2u(event.size.width, event.size.height));
}
}
};
};
}
#endif //SFMLLIBFUNCS_H
```