Click here to Skip to main content
15,894,180 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Determine Top-Left point from 8 available points.
Here 8 points are handles of rectangle, rectangle which acts as bounding rectangle.
Top Left needs to be determined from available set of 8 handles, whenever resizing or rotation of bounding rectangle.
Posted

In my opinion the simplest mathematical solution would be the following.

Given a 2 dimensional coordinate system and a set of points, each point with coordinates (x, y).
You'd have to iterate over all points and remember the point with the lowest x and highest y value.
 
Share this answer
 
Comments
sooraj subramanya 17-May-11 7:19am    
Please consider coordinate system Top-Left (x,y)=(0,0).
And also consider the bounding rectangle can be rotated, which affects values of Handles.
Legor 17-May-11 8:18am    
Well if it's a Top-Left system you'd have to look for the lowest x,y value pair.
Im not sure what you mean with the handles. If the rectangle gets rotated of course the points change their position, so you'll have to do the computation again after that.
Olivier Levrey 17-May-11 8:45am    
I agree. 5.
For the top-left point: lowest x and lowest y.
Rotating the rectangle won't change anything, unless you want the chosen handle to move as well, but in that case it will not be the "top-left" anymore.
sooraj subramanya 18-May-11 0:29am    
ya. you are absolutely right. It wont be top-left anymore. After rotating( including handles) you will get 8 new points(for ex: consider you rotate a rectangle clockwise by 30 degree),then which point will you consider as top-left and why??
Olivier Levrey 18-May-11 3:35am    
This is up to you:
1- If you want to get the "real" top-left point (geometrically speaking), then Legor's solution is the right one and you should accept it.
2- If you want to keep always the same handle as your reference point (I wouldn't call it top-left because it might change after a rotation), then you just need to store its index (actually I suppose it would just be the first one, so index=0)
look for the smallest hypot(x,y) ?
 
Share this answer
 
if( IsSelected())
			{
				UINT iTopLeft_BottomRight_Cursor = 0;
				UINT iMidTop_MidBottom_Cursor = 0;
				UINT iTopRight_BottomLeft_Cursor = 0;
				UINT iMidLeft_MidRight_Cursor = 0;
				float fRotationAngle = 0.0f;
				GetRotationAngleForFrame(GetCurrentFrame(), fRotationAngle);
				int nRotAngle = Round(fRotationAngle * (180.0f/PI));
}           

 if (
                ( (nRotAngle > 0) && (nRotAngle < 23 ) )
                || ( (nRotAngle > 180) && (nRotAngle < 203) )
                || ( (nRotAngle > 270) && (nRotAngle < 293) )
                || ( (nRotAngle > 90 ) && (nRotAngle < 113) )
                )
            {
                iTopLeft_BottomRight_Cursor =   CURSOR_NESW;
                iMidTop_MidBottom_Cursor    =   CURSOR_EW;
                iTopRight_BottomLeft_Cursor =   CURSOR_NWSE;
                iMidLeft_MidRight_Cursor    =   CURSOR_NS;
            }
            else if (
                (nRotAngle > 337)
                || (nRotAngle > 157 && nRotAngle <= 180)
                || (nRotAngle == 0 )
                || (nRotAngle > 67 && nRotAngle < 91)
                || (nRotAngle > 247 && nRotAngle <= 270)
                )
            {
                iTopLeft_BottomRight_Cursor =   CURSOR_NWSE;
                iMidTop_MidBottom_Cursor    =   CURSOR_NS;
                iTopRight_BottomLeft_Cursor =   CURSOR_NESW;
                iMidLeft_MidRight_Cursor    =   CURSOR_EW;
            }
            else if (
                (nRotAngle > 22 && nRotAngle < 68)
                || (nRotAngle > 202 && nRotAngle < 248)
                || (nRotAngle > 112 && nRotAngle < 158)
                || (nRotAngle > 292 && nRotAngle < 338)
                )
            {
                iTopLeft_BottomRight_Cursor =   CURSOR_EW;
                iMidTop_MidBottom_Cursor    =   CURSOR_NWSE;
                iTopRight_BottomLeft_Cursor =   CURSOR_NS;
                iMidLeft_MidRight_Cursor    =   CURSOR_NESW;
            }
            UpdateHandlesWhenHover();
           
            if (m_TopLeftRect.PtInRect(pt) || m_BottomRightRect.PtInRect(pt) )
            {
                if(hCursor)
                {
                    DestroyIcon(hCursor);
                    hCursor = NULL;
                }
                hCursor = GetCustomCursor(AfxGetInstanceHandle(), iTopLeft_BottomRight_Cursor, _T("PNG"));
            }
            else if (m_MidTopRect.PtInRect(pt) || m_MidBottomRect.PtInRect(pt))
            {
                if(hCursor)
                {
                    DestroyIcon(hCursor);
                    hCursor = NULL;
                }
                hCursor = GetCustomCursor(AfxGetInstanceHandle(), iMidTop_MidBottom_Cursor, _T("PNG"));
            }
            else if (m_TopRightRect.PtInRect(pt) || m_BottomLeftRect.PtInRect(pt))
            {
                if(hCursor)
                {
                    DestroyIcon(hCursor);
                    hCursor = NULL;
                }
                hCursor = GetCustomCursor(AfxGetInstanceHandle(), iTopRight_BottomLeft_Cursor, _T("PNG"));
            }
            else if (m_MidLeftRect.PtInRect(pt) || m_MidRightRect.PtInRect(pt) )
            {
                if(hCursor)
                {
                    DestroyIcon(hCursor);
                    hCursor = NULL;
                }
                hCursor = GetCustomCursor(AfxGetInstanceHandle(), iMidLeft_MidRight_Cursor, _T("PNG"));
            }
                       else if(IsPtWithinROI(pt))
            {
                if(hCursor)
                {
                    DestroyIcon(hCursor);
                    hCursor = NULL;
                }
                hCursor = GetCustomCursor(AfxGetInstanceHandle(), IDR_MOVE_CURSOR, _T("PNG"));
                break;
            }
            else
            {
                  if(hCursor)
                {
                    DestroyIcon(hCursor);
                    hCursor = NULL;
                }
                hCursor = GetCustomCursor(AfxGetInstanceHandle(), IDR_ARROW_CURSOR, _T("PNG"));
                   }
               }
        break;
    }
case CURSOR_ON_REGION:
    {
        //Temporarily SIZEALL cursor is loaded. It is to be changed to SIZEALL + ARROW.
        if(hCursor)
        {
            DestroyIcon(hCursor);
            hCursor = NULL;
        }
        hCursor = GetCustomCursor(AfxGetInstanceHandle(), IDR_MOVE_CURSOR, _T("PNG"));
        break;
    }
case CURSOR_ON_ROTATION_HANDLE:
    {
        if(hCursor)
        {
            DestroyIcon(hCursor);
            hCursor = NULL;
        }
        hCursor = GetCustomCursor(AfxGetInstanceHandle(), IDR_ROTATION_MARK_CURSOR, _T("PNG"));
        break;
    }
case CURSOR_ROTATING_REGION:
    {
        if(hCursor)
        {
            DestroyIcon(hCursor);
            hCursor = NULL;
        }
        hCursor = GetCustomCursor(AfxGetInstanceHandle(), IDR_ROTATING_MARK_CURSOR, _T("PNG"));
        break;
    }
default:
    {
        if(hCursor)
        {
            DestroyIcon(hCursor);
            hCursor = NULL;
        }
        hCursor = GetCustomCursor(AfxGetInstanceHandle(), IDR_ARROW_CURSOR, _T("PNG"));

        break;
    }
}

pmainDlg->m_hCursor = hCursor;
SetCursor(pmainDlg->m_hCursor);



UpdateHandlesWhenHover()
{
	CShapeCal cShapeCal;
	CRect rcMeasRect(0,0,100,100);
	CBaseMeasCal cBaseMeasCal;
	CPrecisionPoint  ptCenter;
	CList<CPrecisionPoint,CPrecisionPoint&> ptInputList;
	CPrecisionPoint  ptPoint;
	ptPoint = m_TopLeftRect.CenterPoint();
	ptInputList.AddTail(ptPoint);
	ptPoint = m_MidLeftRect.CenterPoint();
	ptInputList.AddTail(ptPoint);
	ptPoint = m_BottomLeftRect.CenterPoint();
	ptInputList.AddTail(ptPoint);
	ptPoint = m_MidTopRect.CenterPoint();
	ptInputList.AddTail(ptPoint);
	ptPoint = m_MidBottomRect.CenterPoint();
	ptInputList.AddTail(ptPoint);
	ptPoint = m_TopRightRect.CenterPoint();
	ptInputList.AddTail(ptPoint);
	ptPoint = m_MidRightRect.CenterPoint();
	ptInputList.AddTail(ptPoint);
	ptPoint = m_BottomRightRect.CenterPoint();
	ptInputList.AddTail(ptPoint);
	cBaseMeasCal.CalculateCenterPoint(ptInputList,ptCenter);
	CList<CPrecisionPoint,CPrecisionPoint&> ptOutputList;
	double dReturn = 0.0;
	dReturn = cShapeCal.SortEditPoint(rcMeasRect,ptInputList,ptCenter,ptOutputList);
	CPrecisionPoint ptTopLeft(-1,-1);
	//
	vector<CPoint> vectHandles;
	vectHandles.push_back(CPoint(m_TopLeftRect.CenterPoint()));
	vectHandles.push_back(CPoint(m_MidLeftRect.CenterPoint()));
	vectHandles.push_back(CPoint(m_BottomLeftRect.CenterPoint()));
	vectHandles.push_back(CPoint(m_MidTopRect.CenterPoint()));
	vectHandles.push_back(CPoint(m_MidBottomRect.CenterPoint()));
	vectHandles.push_back(CPoint(m_TopRightRect.CenterPoint()));
	vectHandles.push_back(CPoint(m_MidRightRect.CenterPoint()));
	vectHandles.push_back(CPoint(m_BottomRightRect.CenterPoint()));
	::sort(vectHandles.begin(), vectHandles.end(), SortingFunction());
	ptTopLeft = vectHandles[0];
	POSITION position = ptOutputList.GetHeadPosition();
	while( position != NULL )
	{
		ptPoint  = ptOutputList.GetNext(position);
		if(ptTopLeft == ptPoint)
		{
			m_TopLeftRect.SetRect(ptPoint.x,ptPoint.y,ptPoint.x,ptPoint.y);
			m_TopLeftRect.InflateRect(ROI_HANDLE_MARGIN,ROI_HANDLE_MARGIN);
			if( position == NULL )
			{
				position = ptOutputList.GetHeadPosition();
			}
			ptPoint  = ptOutputList.GetNext(position);
			m_MidTopRect.SetRect(ptPoint.x,ptPoint.y,ptPoint.x,ptPoint.y);
			m_MidTopRect.InflateRect(ROI_HANDLE_MARGIN,ROI_HANDLE_MARGIN);
			if( position == NULL )
			{
				position = ptOutputList.GetHeadPosition();
			}
			ptPoint  = ptOutputList.GetNext(position);
			m_TopRightRect.SetRect(ptPoint.x,ptPoint.y,ptPoint.x,ptPoint.y);
			m_TopRightRect.InflateRect(ROI_HANDLE_MARGIN,ROI_HANDLE_MARGIN);
			if( position == NULL )
			{
				position = ptOutputList.GetHeadPosition();
			}
			ptPoint  = ptOutputList.GetNext(position);
			m_MidRightRect.SetRect(ptPoint.x,ptPoint.y,ptPoint.x,ptPoint.y);
			m_MidRightRect.InflateRect(ROI_HANDLE_MARGIN,ROI_HANDLE_MARGIN);
			if( position == NULL )
			{
				position = ptOutputList.GetHeadPosition();
			}
			ptPoint  = ptOutputList.GetNext(position);
			m_BottomRightRect.SetRect(ptPoint.x,ptPoint.y,ptPoint.x,ptPoint.y);
			m_BottomRightRect.InflateRect(ROI_HANDLE_MARGIN,ROI_HANDLE_MARGIN);
			if( position == NULL )
			{
				position = ptOutputList.GetHeadPosition();
			}
			ptPoint  = ptOutputList.GetNext(position);
			m_MidBottomRect.SetRect(ptPoint.x,ptPoint.y,ptPoint.x,ptPoint.y);
			m_MidBottomRect.InflateRect(ROI_HANDLE_MARGIN,ROI_HANDLE_MARGIN);
			if( position == NULL )
			{
				position = ptOutputList.GetHeadPosition();
			}
			ptPoint  = ptOutputList.GetNext(position);
			m_BottomLeftRect.SetRect(ptPoint.x,ptPoint.y,ptPoint.x,ptPoint.y);
			m_BottomLeftRect.InflateRect(ROI_HANDLE_MARGIN,ROI_HANDLE_MARGIN);
			if( position == NULL )
			{
				position = ptOutputList.GetHeadPosition();
			}
			ptPoint  = ptOutputList.GetNext(position);
			m_MidLeftRect.SetRect(ptPoint.x,ptPoint.y,ptPoint.x,ptPoint.y);
			m_MidLeftRect.InflateRect(ROI_HANDLE_MARGIN,ROI_HANDLE_MARGIN);
			break;
		}
	}
}


struct SortingFunction
{  
	bool operator()(const CRect& a, const CRect& b)
    {
        if (a.left == b.left)
            return a.top <= b.top;
        else
            return a.left < b.left;
	}
    bool operator()(const CPoint& a, const CPoint& b)
    {
        if (a.x == b.x)
            return a.y <= b.y;
        else
            return a.x < b.x;
    }
};</pre>
 
Share this answer
 
v2
Comments
Richard MacCutchan 18-May-11 3:30am    
Use the <pre> tags please.
Olivier Levrey 18-May-11 3:32am    
What is that? Is it your solution, or do you have a question regarding all this code? If so, please use the "Improve question" widget instead of posting an answer.
And please use pre tag to format your code to make it easier to read.
sooraj subramanya 18-May-11 4:11am    
Have added <pre> tag. and this is the solution i have implemented.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900