All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines
Public Member Functions | Protected Attributes
QGpCoreTools::Segment2D Class Reference

A 2D segment made of two points. More...

#include <Segment2D.h>

List of all members.

Public Member Functions

double distanceTo (const Point2D &p) const
bool includes (const Point2D &p, double precision=1e-10) const
bool intersects (const Segment2D &seg, Point2D &p) const
double length () const
Point2D p1 () const
Point2D p2 () const
Point2D pointAtFrom1 (double distance)
Point2D pointAtFrom2 (double distance)
Segment2D rotated (const Point2D &center, const Angle &a) const
 Segment2D ()
 Segment2D (const Point2D &p1, const Point2D &p2)
 Segment2D (double x1, double y1, double x2, double y2)
 Segment2D (const Segment2D &o)
void set (const Point2D &p1, const Point2D &p2)
void set (double x1, double y1, double x2, double y2)

Protected Attributes

Rect _limits
Point2D _p1
Point2D _p2

Detailed Description

A 2D segment made of two points.

Full description of class still missing


Constructor & Destructor Documentation

Referenced by rotated().

{}
QGpCoreTools::Segment2D::Segment2D ( const Point2D p1,
const Point2D p2 
) [inline]
{set(p1,p2);}
QGpCoreTools::Segment2D::Segment2D ( double  x1,
double  y1,
double  x2,
double  y2 
) [inline]
{set(x1, y1, x2, y2);}

References _limits, _p1, _p2, and TRACE.

{
  TRACE;
  _p1=o._p1;
  _p2=o._p2;
  _limits=o._limits;
}

Member Function Documentation

double QGpCoreTools::Segment2D::distanceTo ( const Point2D p) const

References _limits, _p1, _p2, QGpCoreTools::Point2D::distanceTo(), QGpCoreTools::Rect::includes(), QGpCoreTools::Point2D::setX(), QGpCoreTools::Point2D::setY(), TRACE, QGpCoreTools::Point2D::x(), and QGpCoreTools::Point2D::y().

{
  TRACE;
  double dx1=_p2.x()-_p1.x();
  double dy1=_p2.y()-_p1.y();
  double dx2=dy1;
  double dy2=-dx1;
  double fac=dx2*dy1-dx1*dy2;
  Point2D pi;
  pi.setX(dx1*dx2*(p.y()-_p1.y())+dx2*dy1*_p1.x()-dx1*dy2*p.x());
  pi.setX(pi.x()/fac);
  if(fabs(dx1)>fabs(dx2))
    pi.setY(_p1.y()+dy1/dx1*(pi.x()-_p1.x()));
  else
    pi.setY(p.y()+dy2/dx2*(pi.x()-p.x()));
  if(_limits.includes(pi)) {
    return p.distanceTo(pi);
  }
  else {
    double d1=p.distanceTo(_p1);
    double d2=p.distanceTo(_p2);
    if(d1<d2) return d1; else return d2;
  }
}
bool QGpCoreTools::Segment2D::includes ( const Point2D p,
double  precision = 1e-10 
) const

Returns true if p is located on the segment (1e-10 precision)

References _limits, _p1, _p2, QGpCoreTools::Rect::includes(), TRACE, QGpCoreTools::Point2D::x(), and QGpCoreTools::Point2D::y().

{
  TRACE;
  if(!_limits.includes(p)) return false;
  double dx=_p2.x()-_p1.x();
  double dy=_p2.y()-_p1.y();
  if(fabs(dx)>fabs(dy)) {
    if(fabs(p.y()-(_p1.y()+dy/dx*(p.x()-_p1.x())))<precision) {
      return true;
    } else {
      return false;
    }
  } else {
    if(fabs(p.x()-(_p1.x()+dx/dy*(p.y()-_p1.y())))<precision) {
      return true;
    } else {
      return false;
    }
  }
}
bool QGpCoreTools::Segment2D::intersects ( const Segment2D seg,
Point2D p 
) const

Returns true if this segment intersects segment seg. The intersection point is returned in p.

References _limits, _p1, _p2, QGpCoreTools::Rect::includes(), QGpCoreTools::Point2D::setX(), QGpCoreTools::Point2D::setY(), TRACE, QGpCoreTools::Point2D::x(), and QGpCoreTools::Point2D::y().

Referenced by QGpCoreTools::IrregularGrid2DData::crossSection().

{
  TRACE;
  double dx1=_p2.x()-_p1.x();
  double dy1=_p2.y()-_p1.y();
  double dx2=seg._p2.x()-seg._p1.x();
  double dy2=seg._p2.y()-seg._p1.y();
  double fac=dx2*dy1-dx1*dy2;
  if(fabs(fac)<1e-20) return false; // parallel segments
  // Horizontal or vertical lines are handled separately to avoid precision errors
  if(dx1==0.0) {
    p.setX(_p1.x());
    p.setY(seg._p1.y()+dy2/dx2*(p.x()-seg._p1.x()));
  } else if(dx2==0.0) {
    p.setX(seg._p1.x());
    p.setY(_p1.y()+dy1/dx1*(p.x()-_p1.x()));
  } else if(dy1==0.0) {
    p.setY(_p1.y());
    p.setX(seg._p1.x()+dx2/dy2*(p.y()-seg._p1.y()));
  } else if(dy2==0.0) {
    p.setY(seg._p1.y());
    p.setX(_p1.x()+dx1/dy1*(p.y()-_p1.y()));
  } else {
    double tmp=dx1*dx2*(seg._p1.y()-_p1.y())+dx2*dy1*_p1.x()-dx1*dy2*seg._p1.x();
    p.setX(tmp/fac);
    if(fabs(dx1)>fabs(dx2)) {
      p.setY(_p1.y()+dy1/dx1*(p.x()-_p1.x()));
    } else {
      p.setY(seg._p1.y()+dy2/dx2*(p.x()-seg._p1.x()));
    }
  }
  if(_limits.includes(p) && seg._limits.includes(p)) return true;
  else return false;
}
double QGpCoreTools::Segment2D::length ( ) const [inline]

Referenced by pointAtFrom1(), and pointAtFrom2().

{return _p1.distanceTo(_p2);}

References _p1, _p2, length(), and TRACE.

{
  TRACE;
  double ratio=distance/length();
  return _p1*(1.0-ratio)+_p2*ratio;
}

References _p1, _p2, length(), and TRACE.

{
  TRACE;
  double ratio=distance/length();
  return _p1*ratio+_p2*(1.0-ratio);
}
Segment2D QGpCoreTools::Segment2D::rotated ( const Point2D center,
const Angle a 
) const

References _p1, _p2, p1(), p2(), QGpCoreTools::Point2D::rotate(), Segment2D(), and TRACE.

{
  TRACE;
  Point2D p1(_p1), p2(_p2);
  p1-=center;
  p2-=center;
  p1.rotate(a);
  p2.rotate(a);
  p1+=center;
  p2+=center;
  return Segment2D(p1, p2);
}
void QGpCoreTools::Segment2D::set ( const Point2D p1,
const Point2D p2 
)
void QGpCoreTools::Segment2D::set ( double  x1,
double  y1,
double  x2,
double  y2 
)

Member Data Documentation


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines