c++与sdl2快速绘制填充圆形,要函数形式
时间: 2024-03-23 13:43:22 浏览: 135
c++图形填充,非常快的一种方法
5星 · 资源好评率100%
在SDL2中,可以使用以下函数绘制填充圆形:
```cpp
void filledCircle(SDL_Renderer* renderer, int cx, int cy, int radius, SDL_Color color);
```
其中,参数含义如下:
- renderer:渲染器
- cx, cy:圆心坐标
- radius:半径
- color:颜色
函数代码如下:
```cpp
void filledCircle(SDL_Renderer* renderer, int cx, int cy, int radius, SDL_Color color) {
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a);
for (int w = 0; w < radius * 2; w++) {
for (int h = 0; h < radius * 2; h++) {
int dx = radius - w; // 水平方向距离中心点的距离
int dy = radius - h; // 垂直方向距离中心点的距离
if ((dx * dx + dy * dy) <= (radius * radius)) {
SDL_RenderDrawPoint(renderer, cx + dx, cy + dy);
}
}
}
}
```
调用示例:
```cpp
int main(int argc, char* argv[]) {
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window* window = SDL_CreateWindow("Filled Circle", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, 0);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0);
SDL_Color color = { 255, 255, 255, 255 };
filledCircle(renderer, 320, 240, 100, color);
SDL_RenderPresent(renderer);
SDL_Delay(2000);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
```
阅读全文