mirror of
https://github.com/VCMP-SqMod/SqMod.git
synced 2025-06-15 22:57:12 +02:00
Allow circles to be transformed to area points.
This commit is contained in:
@ -467,6 +467,33 @@ Circle Circle::Abs() const
|
||||
return {pos.Abs(), std::fabs(rad)};
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Array Circle::ToPointsArray(SQInteger num_segments) const
|
||||
{
|
||||
// Allocate an array with the same amount of elements as the number of segments
|
||||
Array arr(SqVM(), num_segments);
|
||||
// Iterate the specified segments array
|
||||
arr.AppendFromCounted([this](HSQUIRRELVM vm, SQInteger i, SQInteger num_segments) -> bool {
|
||||
if (i >= num_segments) return false;
|
||||
// Get the current angle
|
||||
#ifdef SQUSEDOUBLE
|
||||
SQFloat theta = 2.0d * SQMOD_PI64 * static_cast< SQFloat >(i) / static_cast< SQFloat >(num_segments);
|
||||
#else
|
||||
SQFloat theta = 2.0f * SQMOD_PI * static_cast< SQFloat >(i) / static_cast< SQFloat >(num_segments);
|
||||
#endif // SQUSEDOUBLE
|
||||
// Calculate the x component
|
||||
SQFloat x = (rad * std::cos(theta)) + pos.x;
|
||||
// Calculate the y component
|
||||
SQFloat y = (rad * std::sin(theta)) + pos.y;
|
||||
// Push the Vector2 instance on the stack
|
||||
Var< Vector2 >::push(vm, Vector2{x, y});
|
||||
// Insert the element on the stack into the array
|
||||
return true;
|
||||
}, num_segments);
|
||||
// Return the resulted array
|
||||
return arr;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
const Circle & Circle::Get(StackStrF & str)
|
||||
{
|
||||
@ -539,6 +566,7 @@ void Register_Circle(HSQUIRRELVM vm)
|
||||
.Func(_SC("SetPositionEx"), &Circle::SetPositionEx)
|
||||
.FmtFunc(_SC("SetStr"), &Circle::SetStr)
|
||||
.Func(_SC("Clear"), &Circle::Clear)
|
||||
.Func(_SC("ToPointsArray"), &Circle::ToPointsArray)
|
||||
// Member Overloads
|
||||
.Overload< void (Circle::*)(void) >(_SC("Generate"), &Circle::Generate)
|
||||
.Overload< void (Circle::*)(Val, Val, bool) >(_SC("Generate"), &Circle::Generate)
|
||||
|
@ -411,6 +411,11 @@ struct Circle
|
||||
*/
|
||||
Circle Abs() const;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Transform this into an array of Vector2 points that form a circle.
|
||||
*/
|
||||
Array ToPointsArray(SQInteger num_segments) const;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Extract the values for components of the Circle type from a string.
|
||||
*/
|
||||
|
Reference in New Issue
Block a user