如何从8点列表中确定最左点 [英] How to determine Top-Left point from list of 8 points
本文介绍了如何从8点列表中确定最左点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
从8个可用点中确定左上角点.
这里有8个点是矩形的句柄,矩形作为边界矩形.
每当调整矩形大小或旋转边界矩形时,都需要从可用的8个控点集中确定左上角.
给定一个二维坐标系和一组点,每个点的坐标为(x,y).
您必须遍历所有点,并记住具有最低x和最大y值的点.
寻找最小的hypot(x,y)吗?
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>
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.
解决方案
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.
look for the smallest hypot(x,y) ?
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>
这篇关于如何从8点列表中确定最左点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文