Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Snapping point to path

Snapping point to path

Scheduled Pinned Locked Moved Unsolved General and Desktop
qpainterpathsnapping
2 Posts 2 Posters 709 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Joel BodenmannJ Offline
    Joel BodenmannJ Offline
    Joel Bodenmann
    wrote on last edited by Joel Bodenmann
    #1

    I'm in need of a method that takes a point and a path and returns the point on the pathclosest to the specified point (snapping). The method prototype would look like this:

    QPointF snapPointToPath(const QPointF& point, const QPainterPath& path) const;
    

    I did implement this one for boxes/rectangles which is rather easy. However, now I would like to extend the functionality so it works with any shape/path/line.
    Could anyone tell me what's the fastest/easiest way of doing this? I assume that I should call QPainterPath::createStroke() to get just the outline. But from there I'm lost on how to get the point on the path closest to the specified one.

    Context: I am working with the graphics framework. A user should be able to drag a point around the outline of a QGraphicsItem. The method I want to implement now would take the current mouse position and the QGraphicsItem::shape() as parameters.

    Industrial process automation software: https://simulton.com
    Embedded Graphics & GUI library: https://ugfx.io

    O 1 Reply Last reply
    1
    • Joel BodenmannJ Joel Bodenmann

      I'm in need of a method that takes a point and a path and returns the point on the pathclosest to the specified point (snapping). The method prototype would look like this:

      QPointF snapPointToPath(const QPointF& point, const QPainterPath& path) const;
      

      I did implement this one for boxes/rectangles which is rather easy. However, now I would like to extend the functionality so it works with any shape/path/line.
      Could anyone tell me what's the fastest/easiest way of doing this? I assume that I should call QPainterPath::createStroke() to get just the outline. But from there I'm lost on how to get the point on the path closest to the specified one.

      Context: I am working with the graphics framework. A user should be able to drag a point around the outline of a QGraphicsItem. The method I want to implement now would take the current mouse position and the QGraphicsItem::shape() as parameters.

      O Offline
      O Offline
      ofmrew
      wrote on last edited by
      #2

      @Joel-Bodenmann Two points: 1. You can decompose the path to get each element and work on those independently. 2. You must contend with lines, polylines, ellipses and Bezier splines.

      Closes point on a line is simply a vector projection. Bezier splines are more complex. For an approximation you can get polyline approximation to the remainder using parametric equations, then use the same technique for lines. I am writing a book on this subject.

      By the way, I recall seeing an article on the method you propose.

      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved