Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Accurate globalBounds of circle.  (Read 2229 times)

0 Members and 1 Guest are viewing this topic.

Xrey274

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Accurate globalBounds of circle.
« on: March 18, 2019, 09:50:16 pm »
What I want is for the global bounds to be the same size and shape as the circle. Currently the globalBounds is basically a rectangle. Current code literally just simulates the code of getGlobalBounds function.

#include <iostream>
#include <SFML/Graphics.hpp>

bool calculate(sf::Vector2f pos, sf::Vector2f mouse, int r)
{
    std::cout<<pos.x - mouse.x<<" "<<pos.y - mouse.y <<std::endl;

    if(pos.x - mouse.x >= 0 && pos.x - mouse.x <= r)
    {
        if(pos.y - mouse.y >= 0 && pos.y - mouse.y <= r)
        {
            return true;
        }
        else if(pos.y - mouse.y <= 0 && pos.y - mouse.y >= -r)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    else if(pos.x - mouse.x <= 0 && pos.x - mouse.x >= -r)
    {
        if(pos.y - mouse.y <= 0 && pos.y - mouse.y >= -r)
        {
            return true;
        }
        else if(pos.y - mouse.y >= 0 && pos.y - mouse.y <= r)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    else
    {
        return 0;
    }
}

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "Test");

    sf::Event event;
    sf::Vector2f mouseCoords = window.mapPixelToCoords(sf::Vector2i(sf::Mouse::getPosition(window)));

    sf::CircleShape circle;
    circle.setRadius(150);
    circle.setPosition(sf::Vector2f(400, 300));
    circle.setFillColor(sf::Color::Blue);
    circle.setOrigin(circle.getRadius(), circle.getRadius());

    while(window.isOpen())
    {
        mouseCoords = window.mapPixelToCoords(sf::Vector2i(sf::Mouse::getPosition(window)));

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

            if(event.type == sf::Event::MouseMoved)
            {
                if(calculate(circle.getPosition(), mouseCoords, circle.getRadius()) == true)
                {
                    circle.setFillColor(sf::Color::Red);
                }
                else
                {
                    circle.setFillColor(sf::Color::Blue);
                }
            }
        }

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

        window.draw(circle);

        window.display();
    }
}
 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Accurate globalBounds of circle.
« Reply #1 on: March 19, 2019, 12:40:20 am »
You can calculate the distance between the center point of the circle and the mouse position. If the distance is larger than the radius, it's outside the circle. If it's smaller (or equal) it's inside.

For how to calculate the distance of two points, I suggest to just google that, as it's basic vector math and better explained elsewhere. :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Xrey274

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Re: Accurate globalBounds of circle.
« Reply #2 on: March 19, 2019, 05:19:33 pm »
I'm already doing that - calculating the distance between the center of the circle and the mouse.

Xrey274

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Re: Accurate globalBounds of circle.
« Reply #3 on: March 19, 2019, 05:44:34 pm »
So basically I had to just use a formula for calculating the distance between 2 points in euclidean space and just square it. Thanks for the reply.