#my numbers became something else?

1 messages · Page 1 of 1 (latest)

young carbonBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

#

@ashen gyro

Screenshots!

Your message appears to contain screenshots but no code. Please send code and error messages in text instead of screenshots if applicable!

ashen gyro
#
#include <iostream>
#include <iterator>
#include <list>
#include <SFML/Graphics.hpp>
#include <random>
using namespace std;

class Point {
public:
    float x;
    float y;

    Point(float x, float y) {
        this->x = x;
        this->y = y;
    }

    friend std::ostream& operator<<(std::ostream& strm, const Point& p) {
        return strm << "(" << p.x << "," << p.y << ")";
    }
};

class Rectangle {
public:
    float x;
    float y;
    float w;
    float h;
    float half_w;
    float half_h;

    Rectangle(float x, float y, float w, float h) {
        this->x = x;
        this->y = y;
        this->w = w;
        this->h = h;
        this->half_w = w / 2;
        this->half_h = h / 2;
    }
    Rectangle(Point p, float w, float h) {
        this->x = p.x;
        this->y = p.y;
        this->w = w;
        this->h = h;
        this->half_w = w / 2;
        this->half_h = h / 2;
    }

    bool contains(Point p) {
        return (x <= p.x && p.x <= x + w) && (y <= p.y && p.y <= y + h);
    }

    friend std::ostream& operator<<(std::ostream& strm, const Rectangle& r) {
        return strm << "(" << r.x << "," << r.y << "," << r.w << "," << r.h << ")";
    }
};
#
int main() {
    Rectangle r(0, 0, 800, 800);
    Quadtree qt(r, 4);

    int N = 5;
    int M = r.w;

    random_device rd;
    mt19937 gen(rd());
    uniform_real_distribution<float> dis(0.0, M);

    for (int i = 0; i < N; i++) {
        int x = dis(gen);
        int y = dis(gen);
        qt.insert(Point(x, y));
    }

    sf::RenderWindow window(sf::VideoMode(r.w, r.h), "My Window", sf::Style::Close);

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear(sf::Color::Black);

        qt.windowify(&window);

        window.display();
        return 0;
    }
    return 0;
}
#
class Quadtree {
public:
    Quadtree(Rectangle b, int c) : boundary(b), capacity(c), ne(nullptr), nw(nullptr), se(nullptr), sw(nullptr) {}

    void insert(Point p) {
        if (this->points.size() < this->capacity)
            this->points.push_back(p);
        else {
            if (!divided)
                this->subdivide();

            if (ne->boundary.contains(p))
                ne->insert(p);
            else if (nw->boundary.contains(p))
                nw->insert(p);
            else if (se->boundary.contains(p))
                se->insert(p);
            else
                sw->insert(p);
        }
    }
#

    void windowify(sf::RenderWindow* window) {
        sf::RectangleShape rectangle(sf::Vector2f(this->boundary.w-4, this->boundary.h-4));
        rectangle.setOutlineColor(sf::Color::Cyan);
        rectangle.setFillColor(sf::Color::Transparent);
        rectangle.setOutlineThickness(2);
        rectangle.setPosition(boundary.x+2, boundary.y+2);
        for (auto element : points) {
            sf::CircleShape point(2);
            point.setFillColor(sf::Color::White);
            point.setPosition(element.x, element.y);
            window->draw(point);
        }
        if (divided) {
            ne->windowify(window);
            nw->windowify(window);
            se->windowify(window);
            sw->windowify(window);
        }
        window->draw(rectangle);
    }

    void subdivide() {
        Rectangle ner(boundary.x + boundary.half_w, boundary.y, boundary.half_w, boundary.half_h);
        this->ne = new Quadtree(ner, capacity);
        Rectangle nwr(boundary.x, boundary.y, boundary.half_w, boundary.half_h);
        this->nw = new Quadtree(nwr, capacity);
        Rectangle ser(boundary.x + boundary.half_w, boundary.y + boundary.half_h, boundary.half_w, boundary.half_h);
        this->se = new Quadtree(ser, capacity);
        Rectangle swr(boundary.x, boundary.y + boundary.half_h, boundary.half_w, boundary.half_h);
        this->sw = new Quadtree(swr, capacity);
        divided = true;
    }

    friend std::ostream& operator<<(std::ostream& strm, const Quadtree& qt) {
        return strm << qt.boundary;
    }
#
private:
    Rectangle& boundary;
    int capacity;
    list<Point> points;
    Quadtree* ne;
    Quadtree* nw;
    Quadtree* se;
    Quadtree* sw;
    bool divided = false;
};
#

it didnt allow me to send the full quadtree code in the same message for some reason