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
Forum Updated to NodeBB v4.3 + New Features

Snapping point to path

Scheduled Pinned Locked Moved Unsolved General and Desktop
qpainterpathsnapping
2 Posts 2 Posters 766 Views 3 Watching
  • 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.
  • J Offline
    J Offline
    Joel Bodenmann
    wrote on 12 Nov 2018, 20:11 last edited by Joel Bodenmann 11 Dec 2018, 20:12
    #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 12 Nov 2018, 20:41
    1
    • J Joel Bodenmann
      12 Nov 2018, 20:11

      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 12 Nov 2018, 20:41 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

      1/2

      12 Nov 2018, 20:11

      • Login

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