Following function will split a circle in equidistant NumPoints, and return a position on the circle according to the index of the point (defined by PointId).
// Header
/**
* @brief Returns a point on a circle with a given number of equidistant points.
*
* @param CenterLocation The center location of the circle.
* @param Radius The radius of the circle.
* @param NumPoints The number of equidistant points on the circle.
* @param PointId The index of the point to get on the circle.
* @return A vector representing the position of the point on the circle.
*/
UFUNCTION(BlueprintPure)
static FVector PointOnCircle(FVector const& CenterLocation, float Radius, int NumPoints, int PointId);
// Body
FVector UL0GameplayLibrary::PointOnCircle(FVector const& CenterLocation, float Radius, int NumPoints, int PointId)
{
const float AngleBetweenPoints = 360.0f / static_cast<float>(NumPoints);
const float AngleForPoint = AngleBetweenPoints * static_cast<float>(PointId);
const float X = FMath::Cos(FMath::DegreesToRadians(AngleForPoint)) * Radius;
const float Y = FMath::Sin(FMath::DegreesToRadians(AngleForPoint)) * Radius;
const FVector Point(X, Y, CenterLocation.Z);
return CenterLocation + Point;
}
Leave a Reply