📄 polygon.cpp
字号:
#include "Polygon.h"
#include "mathutils.h"
TBool MPolygon::InsidePolygon(const TPoint p) const
{
TInt counter = 0;
TInt i;
TReal xinters;
TPoint p1,p2;
p1 = PolyPoint(0);
for (i=1;i<=PolyCount();i++)
{
p2 = PolyPoint(i % PolyCount());
if (p.iY > Min(p1.iY,p2.iY))
{
if (p.iY <= Max(p1.iY,p2.iY))
{
if (p.iX <= Max(p1.iX,p2.iX))
{
if (p1.iY != p2.iY)
{
xinters = (p.iY-p1.iY)*(p2.iX-p1.iX)/(p2.iY-p1.iY)+p1.iX;
if (p1.iX == p2.iX || p.iX <= xinters)
counter++;
}
}
}
}
p1 = p2;
}
if (counter % 2 == 0)
return(EFalse);
else
return(ETrue);
}
TBool MPolygon::Intersects(TPoint aPoint1, TPoint aPoint2) const
{
for(TInt i=0; i<PolyLineCount(); i++)
{
TPoint poly1, poly2;
GetPolyLine(i, poly1, poly2);
if(MathUtils::Intersect(poly1,poly2, aPoint1, aPoint2))
return ETrue;
}
if(InsidePolygon(aPoint1) || InsidePolygon(aPoint2))
return ETrue;
return EFalse;
}
TBool MPolygon::Intersects(const MPolygon& aOther) const
{
for(TInt i=0; i<aOther.PolyLineCount(); i++)
{
TPoint a, b;
aOther.GetPolyLine(i, a, b);
if(Intersects(a, b))
return ETrue;
}
return EFalse;
}
TInt RPolygon::PolyCount() const
{
return iPointArray.Count();
}
void RPolygon::RemovePoint(TInt aIndex)
{
iPointArray.Remove(aIndex);
}
TPoint RPolygon::PolyPoint(TInt aPointNumber) const
{
return iPointArray[aPointNumber];
}
void RPolygon::SetPolyPoint(TInt aPointNumber, TPoint aPoint)
{
iPointArray[aPointNumber] = aPoint;
}
inline const TPoint& RPolygon::operator[](TInt aNum) const
{
return iPointArray[aNum];
}
inline TPoint& RPolygon::operator[](TInt aNum)
{
return iPointArray[aNum];
}
void RPolygon::Close()
{
iPointArray.Close();
}
TBool MPolygon::PointInterSects(const TPoint& aPoint) const
{
TInt x=aPoint.iX;
TInt y=aPoint.iY;
TInt i, j, c = 0;
for (i = 0, j = PolyCount()-1; i < PolyCount(); j = i++)
{
if
(
(
( (PolyPoint(i).iY <= y) && (y < PolyPoint(j).iY) ) ||
( (PolyPoint(j).iY <= y) && (y < PolyPoint(i).iY) )
) &&
(x < (PolyPoint(j).iX - PolyPoint(i).iX) *
(y - PolyPoint(i).iY) / (PolyPoint(j).iY - PolyPoint(i).iY) + PolyPoint(i).iX))
{
c = !c;
}
}
return c;
}
TBool MPolygon::RectInterSects(const TRect& /*aRect*/) const
{
return EFalse;
}
void MPolygon::GetPolyLine(TInt aLineNumber, TPoint& aStartLine, TPoint& aEndLine) const
{
aStartLine = PolyPoint(aLineNumber);
if(aLineNumber == PolyLineCount()-1)
aEndLine = PolyPoint(0);
else
aEndLine = PolyPoint(aLineNumber+1);
}
void MPolygon::Rotate(TInt aAngle, MPolygon &aDestination) const
{
TPoint origin(0,0);
for(TInt i=0; i<PolyCount(); i++)
{
TPoint point(PolyPoint(i));
TReal a = MathUtils::Sin(aAngle);
TReal b = MathUtils::Cos(aAngle);
TReal newX = (point.iX * a + point.iY * b);
TReal newY = (point.iY * a - point.iX * b);
Math::Round(newX, newX,0);
Math::Round(newY, newY,0);
aDestination.SetPolyPoint(i,TPoint(MathUtils::Round(newX), MathUtils::Round(newY)));
}
}
void MPolygon::Scale(TReal iDx, TReal iDy)
{
TPoint origin(0,0);
for(TInt i=0; i<PolyCount(); i++)
{
TPoint point(PolyPoint(i));
TReal newX = (TReal)point.iX * iDx;
TReal newY = (TReal)point.iY * iDy;
SetPolyPoint(i,TPoint(MathUtils::Round(newX), MathUtils::Round(newY)));
}
}
void MPolygon::GetBoundingRect(TRect& aRect) const
{
aRect= TRect(KMaxTInt,KMaxTInt, KMinTInt,KMinTInt);
for(TInt i=0; i<PolyCount(); i++)
{
TPoint point(PolyPoint(i));
if(point.iX < aRect.iTl.iX)
aRect.iTl.iX = point.iX;
if(point.iX > aRect.iBr.iX)
aRect.iBr.iX = point.iX+1;
if(point.iY > aRect.iBr.iY)
aRect.iBr.iY = point.iY+1;
if(point.iY < aRect.iTl.iY)
aRect.iTl.iY = point.iY;
}
}
void MPolygon::RotateAndOffset(TInt aAngle,TPoint aOffset, MPolygon& aDestination) const
{
aAngle = aAngle%360;
TPoint origin(0,0);
for(TInt i=0; i<PolyCount(); i++)
{
TPoint point = PolyPoint(i);
TReal a = MathUtils::Sin(aAngle);
TReal b = MathUtils::Cos(aAngle);
TReal newX = (point.iX * a + point.iY * b);
TReal newY = (point.iY * a - point.iX * b);
aDestination.SetPolyPoint(i,TPoint(MathUtils::Round(newX)+aOffset.iX, MathUtils::Round(newY)+aOffset.iY));
}
}
void MPolygon::Offset(TPoint aOffset, MPolygon& aDestination) const
{
for(TInt i=0; i<PolyCount(); i++)
{
aDestination.SetPolyPoint(i, PolyPoint(i) + aOffset);
}
}
void MPolygon::RotateOffsetAndScale(TInt aAngle,TPoint aOffset,TReal iXScale, TReal iYScale, MPolygon& aDestination) const
{
aAngle = aAngle%360;
TPoint origin(0,0);
for(TInt i=0; i<PolyCount(); i++)
{
TPoint point = PolyPoint(i);
const TReal a = MathUtils::Sin(aAngle);
const TReal b = MathUtils::Cos(aAngle) ;
TReal newX = (point.iX * a + point.iY * b) * iXScale;
TReal newY = (point.iY * a - point.iX * b) * iYScale;
aDestination.SetPolyPoint(i,TPoint(MathUtils::Round(newX)+aOffset.iX, MathUtils::Round(newY)+aOffset.iY));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -