UE算法收集

图形类:

  • 是否点在多边形内部
bool IsPointInPolygon(const FVector2D& TestPoint, const TArray& 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& 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& 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& 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)));
}

你可能感兴趣的:(UE4,UE4,算法)