How to check if a point is inside a curve

How to check if a point is inside a curve

nam_h_tran
Advocate Advocate
391 Views
3 Replies
Message 1 of 4

How to check if a point is inside a curve

nam_h_tran
Advocate
Advocate

How can i check if the point i pick is in inside the curve. Such as line, polyline, closed polyline,...

0 Likes
Accepted solutions (1)
392 Views
3 Replies
Replies (3)
Message 2 of 4

autoid374ceb4990
Collaborator
Collaborator

Are you trying to find if a point is inside a closed polygon or other closed figure composed of lines or polylines? 

Some pictures might help explain what you are trying to find.

 If the figure is closed the general method is to cast a line from the point and see if the line crosses all the line segments which compose the figure an odd or even number of times, if odd the point is inside the figure.

0 Likes
Message 3 of 4

Kyudos
Advisor
Advisor
Accepted solution

It may also depend upon whether you want 'on the line' to be inside or outside, and you'll need to decide what the tolerance for that is. We do this:

 

 

 

//------------------------------------------------------------------------------
BOOL Polyline::IsPointInside(const Point3D& Pt)
//------------------------------------------------------------------------------
{
  BOOL bInside = IsPointOnLine(Pt, 0.05);
  if (!bInside)
  {
    bInside = Inside(Pt);
  }
  return bInside;
}

//------------------------------------------------------------------------------
BOOL Polyline::IsPointOnLine(const Point3D& Pt, double dTolerance)
//------------------------------------------------------------------------------
{
  // Determines whether Point Pt is on this polyline within dtolerance.
  dTolerance = dTolerance * dTolerance;  // we use squared distances.
  BOOL bOn = FALSE;
  int nSize = GetSize();
  if (nSize > 0)
  {
    LineSegment Line;
    Point3D P1 = operator[](0);
    Point3D P2;
    for (int i = 1; i < nSize && !bOn; i++)
    {
      P2 = operator[](i);
      Line.Construct(P1, P2);
      if (Line.GetSquaredDistanceTo(Pt) <= dTolerance)
      {
        bOn = TRUE;
      }
      else
      {
        P1 = P2;
      }
    }

    // If the polyline is marked as closed and the first and last points are not equal,
    // check the line between the first and last points
    if (m_bIsClosed)
    {
      P1 = operator[](0);
      P2 = operator[](nSize - 1);
      if (P1 != P2)
      {
        Line.Construct(P1, P2);
        if (Line.GetSquaredDistanceTo(Pt) <= dTolerance)
        {
          bOn = TRUE;
        }
      }
    }
  }
  return bOn;
}

//------------------------------------------------------------------------------
BOOL Polyline::Inside(const Point3D& Pt) const
//------------------------------------------------------------------------------
{
  // See if the point lies inside the polygon in the xy plane

  // Check the polyline is closed
  if (!IsClosed())
  {
    return FALSE;
  }

  int nSize = GetSize();
  BOOL bInside = FALSE;

  Point3D iPt, jPt;

  // If the n'th point is the same as the 1st point only loop to n-1
  int loopend = 0;
  iPt = operator[](0);
  jPt = operator[](nSize - 1);
  if (iPt == jPt)
  {
    loopend = nSize - 1;
    jPt = operator[](nSize - 2);
  }
  else
  {
    loopend = nSize;
  }
  for (int i = 0; i < loopend; i++)
  {
    iPt = operator[](i);
    if ((((iPt.y <= Pt.y) && (Pt.y < jPt.y)) || ((jPt.y <= Pt.y) && (Pt.y < iPt.y))) &&
      (Pt.x < (jPt.x - iPt.x) * (Pt.y - iPt.y) / (jPt.y - iPt.y) + iPt.x))
    {
      bInside = !bInside;
    }
    jPt = iPt;

    if (i == nSize)
    {
      // Here we have a closed polygon without matching ends, so explicitly check the last segment
      // jPt has become the last point, so set iPt to the first one again
      iPt = operator[](0);
      if ((((iPt.y <= Pt.y) && (Pt.y < jPt.y)) || ((jPt.y <= Pt.y) && (Pt.y < iPt.y))) &&
        (Pt.x < (jPt.x - iPt.x) * (Pt.y - iPt.y) / (jPt.y - iPt.y) + iPt.x))
      {
        bInside = !bInside;
      }
    }
  }
  return bInside;
}

 

 

0 Likes
Message 4 of 4

nam_h_tran
Advocate
Advocate

TKS guys

0 Likes