要用C語言繪制曲線,可以使用圖形庫如OpenGL或者SDL來實現。下面是一個簡單的使用SDL繪制曲線的示例代碼:
#include <SDL.h>
int main(int argc, char *argv[]) {
SDL_Init(SDL_INIT_VIDEO);
SDL_Window *window = SDL_CreateWindow("Curve Drawing",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
800, 600, 0);
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
int x1 = 100, y1 = 100;
int x2 = 700, y2 = 500;
int steps = 100;
for (int i = 0; i < steps; i++) {
float t = (float)i / steps;
int x = x1 + t * (x2 - x1);
int y = y1 + t * (y2 - y1);
SDL_RenderDrawPoint(renderer, x, y);
}
SDL_RenderPresent(renderer);
bool quit = false;
SDL_Event event;
while (!quit) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
quit = true;
}
}
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
這個示例代碼使用SDL庫來創建窗口和渲染器,然后繪制一條直線。你可以修改x1
、y1
、x2
、y2
來繪制不同的曲線,也可以修改steps
來控制曲線的平滑度。整個繪制過程是通過在起點和終點之間插值的方式來實現的。最后,通過處理SDL的事件循環來保持窗口顯示,直到用戶關閉窗口。