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

Author Topic: Is it possible to create a line with circles?  (Read 1745 times)

0 Members and 1 Guest are viewing this topic.

Xrey274

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Is it possible to create a line with circles?
« on: March 23, 2019, 10:52:37 pm »
So i am trying to draw a consistent free line with circles along(sf::CircleShape). It works if I move the mouse slowly, but the circles get big gaps if i move it quickly. I was wondering if it possible to resolve this?

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

int main()
{
    sf::RenderWindow window(sf::VideoMode(1920, 1080), "Test");

    sf::Event event;

    std::vector<sf::CircleShape> circles;

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

    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)
            {
                sf::CircleShape newCircle;

                newCircle.setPosition(mouseCoords);
                newCircle.setRadius(3);
                newCircle.setFillColor(sf::Color::Black);

                circles.push_back(newCircle);
            }
        }
        window.clear(sf::Color::White);

        for(auto i : circles)
        {
            window.draw(i);
        }

        window.display();
    }
}
 

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Is it possible to create a line with circles?
« Reply #1 on: March 24, 2019, 11:16:38 am »
Instead of just adding one circle where the mouse is, add as many circles as needed (constant spacing) between the previous mouse position and the current one.
Laurent Gomila - SFML developer

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Is it possible to create a line with circles?
« Reply #2 on: March 24, 2019, 01:10:29 pm »
You'd have to interpolate the position in-between.
The simplest approach would be to connect each point with a line.
If you want rounded ends, you could use something like the Roun Ended Lines: https://github.com/SFML/SFML/wiki/Source:-Round-Ended-Lines
If you just want a line with variable thickness, you could get the Selba Ward Line: https://github.com/Hapaxia/SelbaWard/wiki/Line
Or if you want smoother interpolation, you could try to construct a spline path with Selba Ward's Spline: https://github.com/Hapaxia/SelbaWard/wiki/Spline
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: Is it possible to create a line with circles?
« Reply #3 on: March 25, 2019, 02:18:04 am »
Just exactly why do I need to interpolate? Why can't the program keep up with the mouse?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Is it possible to create a line with circles?
« Reply #4 on: March 25, 2019, 06:47:26 am »
Because you're not running your program at infinite small frame time. So for 60 fps, you can move the mouse quite far within 16ms.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/