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. [SOLVED] intersection point between a QGraphicsEllipceItem and a QLineF in a GraphicsScene
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] intersection point between a QGraphicsEllipceItem and a QLineF in a GraphicsScene

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 5.6k Views 1 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.
  • I Offline
    I Offline
    issam
    wrote on last edited by
    #1

    Hi all,
    How to get the intersection point between a QGraphicsEllipceItem and a QGraphicsLineItem in a GraphicsScene ?

    http://www.iissam.com/

    1 Reply Last reply
    0
    • I Offline
      I Offline
      issam
      wrote on last edited by
      #2

      Hi !
      Oo

      http://www.iissam.com/

      1 Reply Last reply
      0
      • U Offline
        U Offline
        utcenter
        wrote on last edited by
        #3

        There is no method in Qt for this, you have to implement it yourself.

        1 Reply Last reply
        0
        • I Offline
          I Offline
          issam
          wrote on last edited by
          #4

          I want to ask if there is an algorithm written for such as problem :)

          http://www.iissam.com/

          1 Reply Last reply
          0
          • U Offline
            U Offline
            utcenter
            wrote on last edited by
            #5

            http://www.analyzemath.com/EllipseProblems/ellipse_line_int.html

            1 Reply Last reply
            0
            • C Offline
              C Offline
              ChrisW67
              wrote on last edited by
              #6

              You also need to be aware that there will be one, two, or no intersections of a line (infinite) and ellipse... As a general rule having only one intersection will be rare because the line has to be perfectly tangential to the ellipse. Once you find the intersections of the infinite line then you can check if either falls on the segment of interest i.e. the part corresponding to your line item.

              1 Reply Last reply
              0
              • I Offline
                I Offline
                issam
                wrote on last edited by
                #7

                I will try, thanks all !

                http://www.iissam.com/

                1 Reply Last reply
                0
                • I Offline
                  I Offline
                  issam
                  wrote on last edited by
                  #8

                  Well this my code to determine the intersection point :

                  @
                  QPointF Line::intersectionPoint1(QGraphicsItem * circle, QLineF * line)
                  {

                  float R= 20;
                  
                  float x0 = (circle->mapToScene(circle->shape().controlPointRect().center())).x();
                  float y0 = (circle->mapToScene(circle->shape().controlPointRect().center())).y();
                  

                  if(line->x2() != line->x1())
                  {
                  QPointF yaxis_intersection;
                  line->intersect( QLineF(QPointF(0, 10000), QPointF(0, -10000)), &yaxis_intersection);

                  float a = (line->y2() - line->y1())/(line->x2() - line->x1());
                  float b = yaxis_intersection.y();
                  
                  float A = 1 + a*a;
                  float B = 2*(a*b - a*y0 - x0);
                  float C = x0 * x0 + y0*y0 + b*b - 2*b*y0 - R*R;
                  
                  float Q = B*B - 4*A*C;  
                  if(Q > 0)
                   {  
                     float s1 = (-1)*(B + sqrt(Q))/(2*A);
                     float s2 = (sqrt(Q) - B)/(2*A);
                     QPointF ps1(s1, a*s1 + b);
                     QPointF ps2(s2, a*s2 + b); 
                  

                  if(contains(ps1))
                  return ps1;
                  else
                  return ps2;
                  }
                  else
                  {
                  float s0 = (-1)B/(2A);
                  return QPointF(s0, as0 + b);
                  }
                  }
                  else
                  {
                  float x = line->x1();
                  // y
                  y - 2y0y + (x - x0)(x - x0) + y0y0 - R*R = 0

                   float C = (x - x0)*(x - x0) + y0*y0 - R*R;
                   float Q = 4*y0*y0 - 4*C;  
                  
                  if(Q > 0)
                   {  
                     float s1 = y0 - sqrt(Q)/2;
                     float s2 = y0 + sqrt(Q)/2 ;
                     QPointF ps1(x, s1);
                     QPointF ps2(x, s2); 
                  

                  if(contains(ps1))
                  return ps1;
                  else
                  return ps2;
                  }
                  else
                  {
                  return QPointF(x, y0);
                  }
                  }

                  }

                  @

                  http://www.iissam.com/

                  1 Reply Last reply
                  0

                  • Login

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