鱼C论坛

 找回密码
 立即注册
查看: 1331|回复: 2

[已解决]如何画圆?

[复制链接]
发表于 2021-10-14 21:04:44 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
不用grace.h,如何画圆?
最佳答案
2021-10-14 22:04:28
在数学中怎么画圆,在编程中就怎么画圆

main.c
#include "screen.h"
#include <math.h>

void circle(screen_t *screen, size_t x, size_t y, size_t r) {
    for(double i = 0; i < 2 * M_PI; i += M_PI / 180) {
        screen_putchar(screen, x + (cos(i) * r) * 1.8, y + sin(i) * r, '0');
    }
}

int main(void) {
    screen_t *screen = screen_init(130, 41);
    screen_draw_border(screen, '#');
    circle(screen, screen->width / 2, screen->height / 2, 15);
    screen_refresh(screen, stdout);
    screen_deinit(screen);
    return 0;
}

screen.h
#ifndef _SCREEN_H_
#define _SCREEN_H_

#include <stdio.h>
#include <stddef.h>

typedef struct {
    size_t width, height;
    char *buff;
} screen_t;

screen_t *screen_init(size_t width, size_t height);
void screen_deinit(screen_t *screen);
void screen_putchar(screen_t *screen, size_t x, size_t y, char ch);
void screen_refresh(screen_t *screen, FILE *fp);
void screen_draw_border(screen_t *screen, char ch);

#endif

screen.c
#include "screen.h"
#include <stdlib.h>
#include <string.h>

screen_t *screen_init(size_t width, size_t height) {
    screen_t *screen = malloc(sizeof(*screen));
    *screen = (screen_t){width, height, malloc(width * height)};
    memset(screen->buff, ' ', screen->width * screen->height);
    return screen;
}

void screen_deinit(screen_t *screen) {
    free(screen->buff);
    free(screen);
}

void screen_putchar(screen_t *screen, size_t x, size_t y, char ch) {
    if(x < screen->width && y < screen->height)
        screen->buff[y * screen->width + x] = ch;
}

void screen_refresh(screen_t *screen, FILE *fp) {
    for(size_t y = 0; y < screen->height; ++y) {
        for(size_t x = 0; x < screen->width; ++x) {
            fputc(screen->buff[y * screen->width + x], fp);
        }
        fputc('\n', fp);
    }
}

void screen_draw_border(screen_t *screen, char ch) {
    for(size_t y = 0; y < screen->height; ++y) {
        for(size_t x = 0; x < screen->width; ++x) {
            if(y == 0 || x == 0 || y == screen->height - 1 || x == screen->width - 1)
                screen_putchar(screen, x, y, ch);
        }
    }
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-10-14 22:04:28 | 显示全部楼层    本楼为最佳答案   
在数学中怎么画圆,在编程中就怎么画圆

main.c
#include "screen.h"
#include <math.h>

void circle(screen_t *screen, size_t x, size_t y, size_t r) {
    for(double i = 0; i < 2 * M_PI; i += M_PI / 180) {
        screen_putchar(screen, x + (cos(i) * r) * 1.8, y + sin(i) * r, '0');
    }
}

int main(void) {
    screen_t *screen = screen_init(130, 41);
    screen_draw_border(screen, '#');
    circle(screen, screen->width / 2, screen->height / 2, 15);
    screen_refresh(screen, stdout);
    screen_deinit(screen);
    return 0;
}

screen.h
#ifndef _SCREEN_H_
#define _SCREEN_H_

#include <stdio.h>
#include <stddef.h>

typedef struct {
    size_t width, height;
    char *buff;
} screen_t;

screen_t *screen_init(size_t width, size_t height);
void screen_deinit(screen_t *screen);
void screen_putchar(screen_t *screen, size_t x, size_t y, char ch);
void screen_refresh(screen_t *screen, FILE *fp);
void screen_draw_border(screen_t *screen, char ch);

#endif

screen.c
#include "screen.h"
#include <stdlib.h>
#include <string.h>

screen_t *screen_init(size_t width, size_t height) {
    screen_t *screen = malloc(sizeof(*screen));
    *screen = (screen_t){width, height, malloc(width * height)};
    memset(screen->buff, ' ', screen->width * screen->height);
    return screen;
}

void screen_deinit(screen_t *screen) {
    free(screen->buff);
    free(screen);
}

void screen_putchar(screen_t *screen, size_t x, size_t y, char ch) {
    if(x < screen->width && y < screen->height)
        screen->buff[y * screen->width + x] = ch;
}

void screen_refresh(screen_t *screen, FILE *fp) {
    for(size_t y = 0; y < screen->height; ++y) {
        for(size_t x = 0; x < screen->width; ++x) {
            fputc(screen->buff[y * screen->width + x], fp);
        }
        fputc('\n', fp);
    }
}

void screen_draw_border(screen_t *screen, char ch) {
    for(size_t y = 0; y < screen->height; ++y) {
        for(size_t x = 0; x < screen->width; ++x) {
            if(y == 0 || x == 0 || y == screen->height - 1 || x == screen->width - 1)
                screen_putchar(screen, x, y, ch);
        }
    }
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-14 22:12:40 | 显示全部楼层
我就是问一下
不用grace.h,如何画圆?那使用grace.h,如何画圆?

完全没有听说过 grace.h 这个头文件,你是不是又因为不认真写错单词了?
你想写的是这个?
graphics.h
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-1-10 18:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表