int Polygon::LocateSegment(int tolerance, int x, int y) { int index = -1; if (force_acttype == ROI_MOVE || npnts < 3){ return index; } for (int i = 0 ; i < npnts; i++) { if (IsNearLine(tolerance, x, y, pnt[i].x, pnt[i].y, pnt[(i+1)%npnts].x, pnt[(i+1)%npnts].y)) { index = i; break; } } if (closed && index >= npnts || (!closed) && index >= (npnts-1)) { index = -1; //cout << "No vertex found" << endl; } else { //cout << "That point is near vertex " << index << endl; } return index; }
/************************************************************************ * * * Check whether a line is selected or not. * * There are two possiblities: * * 1. Positioning a cursor at the end points will RESIZE a line. * * 2. Positioning a cursor close to the line (but not at end points) * * will MOVE a line. * * The algorithm used to select a line is to find the * * perpendicular distance from a current position to the line. * * If it is less than "aperture", it means that a line is * * selected. * * If the line is selected, if will set the 'acttype' variable. * * Return TRUE or FALSE. * * */ Flag Line::is_selected(short x, short y) { #define Corner_Check(a,b,p,q,v) com_point_in_rect(a, b, p-v, q-v, p+v, q+v) // Check whether or not a line is on the screen /* if (!roi_state(ROI_STATE_EXIST)) return(FALSE); */ // ---- Check to RESIZE a line ----- // Check the end points of a line. If the cursor is close enough to // the end points, we will assign the opposite position of the point // to variables 'basex' and 'basey' because this is the way we // interactively create a line. if (force_acttype != ROI_MOVE){ if (Corner_Check(x, y, pnt[0].x, pnt[0].y, aperture)) { basex = pnt[1].x; basey = pnt[1].y; acttype = ROI_RESIZE; return(TRUE); } else if (Corner_Check(x, y, pnt[1].x, pnt[1].y, aperture)) { basex = pnt[0].x; basey = pnt[0].y; acttype = ROI_RESIZE; return(TRUE); } } // ----- Check to MOVE a line ----- // Check if x and y point is within the range of a line if (IsNearLine(aperture, x, y, pnt[0].x, pnt[0].y, pnt[1].x, pnt[1].y)) { acttype = ROI_MOVE; return(TRUE); } // ----- A line is not selected ------ return(FALSE); #undef Corner_Check }
bool CChartLineSerie::IsPointOnSerie(const CPoint& screenPoint, unsigned& uIndex) const { uIndex = INVALID_POINT; if (!m_bIsVisible) return false; unsigned uFirst=0, uLast=0; if (!GetVisiblePoints(uFirst, uLast)) return false; if (uFirst>0) uFirst--; if (uLast<GetPointsCount()-1) uLast++; bool bResult = false; for (unsigned i=uFirst ; i < uLast ; i++) { SChartXYPoint PointOrig = GetPoint(i); SChartXYPoint PointDest = GetPoint(i+1); CPoint ScreenPointOrig, ScreenPointDest; ValueToScreen(PointOrig.X, PointOrig.Y, ScreenPointOrig); ValueToScreen(PointDest.X, PointDest.Y, ScreenPointDest); if (IsNearLine(ScreenPointOrig.x, ScreenPointOrig.y, ScreenPointDest.x, ScreenPointDest.y, screenPoint.x, screenPoint.y)) { // Check if the click is close to one of the two points. int xDist = abs(screenPoint.x - ScreenPointOrig.x); int yDist = abs(screenPoint.y - ScreenPointOrig.y); if (xDist<=5 && yDist<=5) uIndex = i; xDist = abs(screenPoint.x - ScreenPointDest.x); yDist = abs(screenPoint.y - ScreenPointDest.y); if (xDist<=5 && yDist<=5) uIndex = i+1; bResult = true; break; } } return bResult; }