如何在Cocos2d-x中画一段圆?

How to draw a segment of a circle in Cocos2d-x?

上下文
我尝试在我的游戏中绘制饼图以进行统计。我正在使用 Cocos2d-x ver.3.8.1。游戏的大小很重要,所以我不会使用第三方框架来制作饼图。
问题
我在 Cocos2d-x 中找不到任何合适的方法来绘制圆的一部分。
我试过了
我试图在网上找到解决这个问题的方法,但没有成功。
众所周知,圆的扇形=三角形+线段。因此,我也尝试使用 DrawNode 中的方法 drawSegment()。 虽然它有参数radius("The segment radius"写在API reference),但半径只影响线的粗细。 drawSegment()方法绘制一条简单的线,其粗细由方法调用设置。
问题
请提示我,如何在 Cocos2d-x 中绘制一段或一段圆? 任何建议将不胜感激,谢谢。

我认为在 Cocos2d-X 中绘制圆的一部分的方法之一是在 DrawNode 上使用 drawPolygon 的方法。我写了小样本。

void drawSector(cocos2d::DrawNode* node, cocos2d::Vec2 origin, float radius, float angle_degree,
                cocos2d::Color4F fillColor, float borderWidth, cocos2d::Color4F bordercolor,
                unsigned int num_of_points = 100)
{
  if (!node)
  {
    return;
  }

  const cocos2d::Vec2 start = origin + cocos2d::Vec2{radius, 0};
  const auto angle_step = 2 * M_PI * angle_degree / 360.f / num_of_points;
  std::vector<cocos2d::Point> circle;

  circle.emplace_back(origin);
  for (int i = 0; i <= num_of_points; i++)
  {
    auto rads = angle_step * i;
    auto x = origin.x + radius * cosf(rads);
    auto y = origin.y + radius * sinf(rads);
    circle.emplace_back(x, y);
  }

  node->drawPolygon(circle.data(), circle.size(), fillColor, borderWidth, bordercolor);
}

这是计算圆的边缘点位置和绘制多边形的函数。如果你想使用它,你需要像下面这样调用,

auto canvas = DrawNode::create();
drawSector(canvas, cocos2d::Vec2(400, 400), 100, 60, cocos2d::Color4F::GREEN, 2, cocos2d::Color4F::BLUE, 100);
this->addChild(triangle);

结果是这样的。我认为该代码将帮助您解决问题。