Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 2

Qt-interest Archive, December 2006
[QT4.2.2] QLineF intersect() request


Message 1 in thread

Is it possible to add a method to QLineF to pass intersect() a
QPointF as well?

Jeff

--
 [ signature omitted ] 

Message 2 in thread

Hi,

If you mean "add a method in a future release of the library itself", then
you should post your request on the Task Tracker (see the website).

If you mean "add a method in a subclass of my own", I'd say it is not
relevant since QLineF is most often handled statically, not through a
pointer.

Anyhow, to me, the method should perhaps be called  "contains" instead of
"intersect" : a line does not "intersect" a point, to me.

You may implement your own method, eventually static. Mine looks like as far
as i remember :
bool contains(QLineF line, QpointF point) {
        QRectF rectLine(line.p1, line.p2);
        if( ! rectLine.contains(point) )
                return false 

        QLineF line2(point, line.p1)
        return (angle(line, line2) == 0);
}

You may have to check whether or not the angle between a null line and a
line is 0 or not, since point may be equal to line.p1 or line.p2

Best-
Nicolas

Jeff Lacki wrote:

> 
> Is it possible to add a method to QLineF to pass intersect() a
> QPointF as well?
> 
> Jeff
> 
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
> "unsubscribe" in the subject or the body. List archive and information:
> http://lists.trolltech.com/qt-interest/

--
 [ signature omitted ] 

Message 3 in thread

Nicolas Castagne wrote:
> You may implement your own method, eventually static. Mine 
> looks like as far
> as i remember :
> bool contains(QLineF line, QpointF point) {
>         QRectF rectLine(line.p1, line.p2);
>         if( ! rectLine.contains(point) )
>                 return false 
> 
>         QLineF line2(point, line.p1)
>         return (angle(line, line2) == 0);
> }

I am not sure I can beat that in terms of inefficiency.
I am tempted to try nevertheless [completley untested]:

bool contains(const QLineF & l, const QPointF & p)
{
  const double x0 = p.x();
  const double y0 = p.y();
  const double x1 = l.p1().x();
  const double y1 = l.p1().y();
  const double x2 = l.p2().x();
  const double y2 = l.p2().y();
  const double d  = x0 * y1 - y0 * x1  
                 +  x1 * y2 - y1 * x2
                 -  x0 * y2 + y0 * x2;
  if (d != 0)  // FIXME
    return false;
  if (x1 < x2)
    return x1 <= x0 && x0 <= x2;
  if (x2 < x1)
    return x2 <= x0 && x0 <= x1;
  if (y1 < y2)
    return y1 <= y0 && y0 <= y2;
  if (y2 < y1)
    return y2 <= y0 && y0 <= y1;
  return x0 == x1 && y0 == y1; // FIXME
}

Of course, comparing floating point numbers with '=='
is bound to fail for any non-trivial example.

Regards,
Andre'

--
 [ signature omitted ] 

Message 4 in thread

Thank you Nicolas and Andre.  Ill try this out.  I agree contains()
is a much better method name per my request.  When I get a chance
Ill try this out and submit a request on trolltech.

Jeff

On Wed, 2006-12-06 at 12:33 +0100, Andrà PÃnitz wrote:
> Nicolas Castagne wrote:
> > You may implement your own method, eventually static. Mine 
> > looks like as far
> > as i remember :
> > bool contains(QLineF line, QpointF point) {
> >         QRectF rectLine(line.p1, line.p2);
> >         if( ! rectLine.contains(point) )
> >                 return false 
> > 
> >         QLineF line2(point, line.p1)
> >         return (angle(line, line2) == 0);
> > }
> 
> I am not sure I can beat that in terms of inefficiency.
> I am tempted to try nevertheless [completley untested]:
> 
> bool contains(const QLineF & l, const QPointF & p)
> {
>   const double x0 = p.x();
>   const double y0 = p.y();
>   const double x1 = l.p1().x();
>   const double y1 = l.p1().y();
>   const double x2 = l.p2().x();
>   const double y2 = l.p2().y();
>   const double d  = x0 * y1 - y0 * x1  
>                  +  x1 * y2 - y1 * x2
>                  -  x0 * y2 + y0 * x2;
>   if (d != 0)  // FIXME
>     return false;
>   if (x1 < x2)
>     return x1 <= x0 && x0 <= x2;
>   if (x2 < x1)
>     return x2 <= x0 && x0 <= x1;
>   if (y1 < y2)
>     return y1 <= y0 && y0 <= y2;
>   if (y2 < y1)
>     return y2 <= y0 && y0 <= y1;
>   return x0 == x1 && y0 == y1; // FIXME
> }
> 
> Of course, comparing floating point numbers with '=='
> is bound to fail for any non-trivial example.
> 
> Regards,
> Andre'
> 
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/

--
 [ signature omitted ]