图形类:
bool IsPointInPolygon(const FVector2D& TestPoint, const TArray<FVector2D>& PolygonPoints)
{
const int NumPoints = PolygonPoints.Num();
float AngleSum = 0.0f;
for (int PointIndex = 0; PointIndex < NumPoints; ++PointIndex)
{
const FVector2D& VecAB = PolygonPoints[PointIndex] - TestPoint;
const FVector2D& VecAC = PolygonPoints[(PointIndex + 1) % NumPoints] - TestPoint;
const float Angle = FMath::Sign(FVector2D::CrossProduct(VecAB, VecAC)) * FMath::Acos(FMath::Clamp(FVector2D::DotProduct(VecAB, VecAC) / (VecAB.Size() * VecAC.Size()), -1.0f, 1.0f));
AngleSum += Angle;
}
return (FMath::Abs(AngleSum) > 0.001f);
}
float MeasurePolyArea(const TArray<FVector>& Locs)
{
FPoly NewPoly;
NewPoly.Init();
for (int i = 0; i < Locs.Num(); i++) {
NewPoly.Vertices.Add(Locs[i]);
}
return NewPoly.Area();
}
//2D
bool GetCenterOfVector2D(const TArray<FVector2D>& Points, FVector2D& Center)
{
if (Points.Num() == 1)
{
Center = Points[0];
return true;
}
else if (Points.Num() >= 2)
{
float MinX = Points[0].X, MaxX = Points[0].X;
float MinY = Points[0].Y, MaxY = Points[0].Y;
for (const FVector2D& Point : Points)
{
MinX = Point.X < MinX ? Point.X : MinX;
MaxX = Point.X > MaxX ? Point.X : MaxX;
MinY = Point.Y < MinY ? Point.Y : MinY;
MaxY = Point.Y > MaxY ? Point.Y : MaxY;
}
Center = 0.5 * FVector2D(MaxX + MinX, MaxY + MinY);
return true;
}
return false;
}
//3D
bool GetCenterOfVector(const TArray<FVector>& Points, FVector& Center)
{
if (Points.Num() == 1)
{
Center = Points[0];
return true;
}
else if (Points.Num() >= 2)
{
float MinX = Points[0].X, MaxX = Points[0].X;
float MinY = Points[0].Y, MaxY = Points[0].Y;
float MinZ = Points[0].Z, MaxZ = Points[0].Z;
for (const FVector& Point : Points)
{
MinX = Point.X < MinX ? Point.X : MinX;
MaxX = Point.X > MaxX ? Point.X : MaxX;
MinY = Point.Y < MinY ? Point.Y : MinY;
MaxY = Point.Y > MaxY ? Point.Y : MaxY;
MinZ = Point.Z < MinZ ? Point.Z : MinZ;
MaxZ = Point.Z > MaxZ ? Point.Z : MaxZ;
}
Center = 0.5 * FVector(MaxX + MinX, MaxY + MinY, MaxZ + MinZ);
return true;
}
return false;
}
FVector2D GetPointOnCircle(FVector2D Center, float Radius, float Angle)
{
float Radian = FMath::DegreesToRadians(Angle);
return(Center + Radius * FVector2D(FMath::Sin(Radian), FMath::Cos(Radian)));
}
|