用C语言代码实现一只可爱的冰墩墩!

Posted C语言编程学习基地

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用C语言代码实现一只可爱的冰墩墩!相关的知识,希望对你有一定的参考价值。

画一只冰墩墩

将冰墩墩简化成多个椭圆,并且按照不同部位,分步用函数分别绘制,冰墩墩的具体细节没有去刻画。适合初学者借鉴学习与加以细化,例如可以细化一下冰墩墩的腿部和手部细节,将它肚子上的文字改成冬奥 LOGO,改变一下眼睛的颜色等等,还可以画一只雪容融。

本程序改变了原有的坐标原点,通过观察,我们发现冰墩墩大部分身体都是对称的,因此,我们可以通过改变它的坐标原点,只需获取一半的坐标点,通过对称绘制另一半,减少我们获取坐标需要的时间。

 

截图


 

源码

#include<graphics.h>
#include<conio.h>
#include<math.h>

#define PI acos(-1.0)
#define WIDTH 800
#define HEIGHT 800
double th = PI / 180;

void DrawBack();                            // 绘制背景
void DrawEar();                                // 绘制耳朵
void DrawLeg();                                // 绘制腿
void DrawArm();                                // 绘制胳膊
void DrawBody();                            // 绘制身体
void DrawEye();                                // 绘制眼睛
void DrawNose();                            // 绘制鼻子
void DrawMouth();                            // 绘制嘴
void DrawColour();                            // 绘制彩带
void DrawLogo();                            // 绘制标志    
void heart(int x0, int y0, int size, COLORREF C);                        // 绘制心
void DrawEllipse(int x0, int y0, int a, int b, int k, COLORREF color);    // 绘制倾斜椭圆

int main()

    initgraph(WIDTH, HEIGHT);
    DrawBack();
    setorigin(WIDTH / 2, HEIGHT / 2);        // 设置坐标系
    DrawEar();                                // 绘制耳朵
    DrawLeg();                                // 绘制腿
    DrawArm();                                // 绘制胳膊
    DrawBody();                                // 绘制身体
    DrawEye();                                // 绘制眼睛
    DrawNose();                                // 绘制鼻子
    DrawMouth();                            // 绘制嘴
    DrawColour();                            // 绘制彩带
    DrawLogo();                                // 绘制标志    
    heart(330, -120, 10, RED);
    setfillcolor(RED);
    floodfill(330, -100, RED);
    _getch();
    return 0;


void DrawBack()

    float H = 190;        // 色相
    float S = 1;        // 饱和度
    float L = 0.7f;        // 亮度
    for (int y = 0; y < HEIGHT; y++)
    
        L += 0.0002f;
        setlinecolor(HSLtoRGB(H, S, L));
        line(0, y, WIDTH - 1, y);
    


void DrawEar()

    setfillcolor(BLACK);
    solidcircle(172, -300, 62);
    solidcircle(-172, -300, 62);


void DrawLeg()

    setfillcolor(BLACK);
    solidellipse(44, 155, 168, 348);
    solidellipse(-44, 155, -168, 348);


void DrawArm()

    DrawEllipse(-267, 50, 100, 60, 50, BLACK);
    DrawEllipse(297, -60, 100, 60, 50, BLACK);
    setfillcolor(BLACK);
    floodfill(-267, 50, BLACK);
    floodfill(297, -60, BLACK);


void DrawBody()

    setlinecolor(BLACK);
    setlinestyle(PS_SOLID, 8);
    setfillcolor(WHITE);
    fillellipse(-270, -354, 270, 260);


void DrawEye()

    DrawEllipse(109, -131, 84, 60, 314, BLACK);
    DrawEllipse(-109, -131, 84, 60, -314, BLACK);
    setfillcolor(BLACK);
    floodfill(109, -131, BLACK);
    floodfill(-109, -131, BLACK);
    setfillcolor(WHITE);
    setlinestyle(PS_SOLID, 1);
    solidcircle(92, -137, 30);
    solidcircle(-92, -137, 30);
    setfillcolor(BLACK);
    solidcircle(90, -137, 26);
    solidcircle(-90, -137, 26);
    setfillcolor(WHITE);
    solidcircle(81, -146, 9);
    solidcircle(-81, -146, 9);


void DrawNose()

    setlinestyle(PS_SOLID, 1);
    setfillcolor(BLACK);
    solidellipse(-26, -106, 26, -63);


void DrawMouth()

    setlinecolor(BLACK);
    setlinestyle(PS_SOLID, 8);
    arc(-43, -75, 43, -7, PI, 0);


void DrawColour()

    setlinecolor(BLACK);
    setlinestyle(PS_SOLID, 8);
    setlinecolor(RGB(91, 198, 250));
    ellipse(-205, -265, 205, 74);
    setlinecolor(RGB(119, 216, 113));
    ellipse(-215, -275, 215, 84);

    setlinecolor(RGB(254, 122, 185));
    ellipse(-225, -285, 225, 94);


void DrawLogo()

    RECT r =  -116, 100, 116, 175 ;
    settextcolor(BLACK);
    setbkmode(TRANSPARENT);
    settextstyle(60, 0, _T("黑体"));
    drawtext(_T("BeiJing"), &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
    r =  -116, 175, 116, 250 ;
    drawtext(_T("2022"), &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);


void DrawEllipse(int x0, int y0, int a, int b, int k, COLORREF color)

    double i;
    double x, y, tx, ty;
    for (i = -180; i <= 180; i = i + 0.5)
    
        x = a * cos(i * th);
        y = b * sin(i * th);
        tx = x;
        ty = y;
        x = tx * cos(k * th) - ty * sin(k * th) + x0;
        y = y0 - (ty * cos(k * th) + tx * sin(k * th));
        setfillcolor(color);
        solidcircle((int)x, (int)y, 2);
    


void heart(int x0, int y0, int size, COLORREF C)

    double m, n, x, y;
    double i;
    for (i = 0; i <= 2 * size; i = i + 0.01)
    
        // 产生极坐标点
        m = i;
        n = -size * (((sin(i) * sqrt(fabs(cos(i)))) / (sin(i) + 1.4142)) - 2 * sin(i) + 2);
        // 转换为笛卡尔坐标
        x = n * cos(m) + x0;
        y = n * sin(m) + y0;
        setfillcolor(C);
        solidcircle((int)x, (int)y, 1);
    

- End -

————————————————

不管你是转行也好,初学也罢,进阶也可,如果你想学编程~

【值得关注】我的 编程学习交流俱乐部【点击进入】


 

#include<graphics.h>#include<conio.h>#include<math.h>
#define PI acos(-1.0)#define WIDTH 800#define HEIGHT 800double th = PI / 180;
void DrawBack();// 绘制背景void DrawEar();// 绘制耳朵void DrawLeg();// 绘制腿void DrawArm();// 绘制胳膊void DrawBody();// 绘制身体void DrawEye();// 绘制眼睛void DrawNose();// 绘制鼻子void DrawMouth();// 绘制嘴void DrawColour();// 绘制彩带void DrawLogo();// 绘制标志void heart(int x0, int y0, int size, COLORREF C);// 绘制心void DrawEllipse(int x0, int y0, int a, int b, int k, COLORREF color);// 绘制倾斜椭圆
int main()initgraph(WIDTH, HEIGHT);DrawBack();setorigin(WIDTH / 2, HEIGHT / 2);// 设置坐标系DrawEar();// 绘制耳朵DrawLeg();// 绘制腿DrawArm();// 绘制胳膊DrawBody();// 绘制身体DrawEye();// 绘制眼睛DrawNose();// 绘制鼻子DrawMouth();// 绘制嘴DrawColour();// 绘制彩带DrawLogo();// 绘制标志heart(330, -120, 10, RED);setfillcolor(RED);floodfill(330, -100, RED);_getch();return 0;
void DrawBack()float H = 190;// 色相float S = 1;// 饱和度float L = 0.7f;// 亮度for (int y = 0; y < HEIGHT; y++)L += 0.0002f;setlinecolor(HSLtoRGB(H, S, L));line(0, y, WIDTH - 1, y);
void DrawEar()setfillcolor(BLACK);solidcircle(172, -300, 62);solidcircle(-172, -300, 62);
void DrawLeg()setfillcolor(BLACK);solidellipse(44, 155, 168, 348);solidellipse(-44, 155, -168, 348);
void DrawArm()DrawEllipse(-267, 50, 100, 60, 50, BLACK);DrawEllipse(297, -60, 100, 60, 50, BLACK);setfillcolor(BLACK);floodfill(-267, 50, BLACK);floodfill(297, -60, BLACK);
void DrawBody()setlinecolor(BLACK);setlinestyle(PS_SOLID, 8);setfillcolor(WHITE);fillellipse(-270, -354, 270, 260);
void DrawEye()DrawEllipse(109, -131, 84, 60, 314, BLACK);DrawEllipse(-109, -131, 84, 60, -314, BLACK);setfillcolor(BLACK);floodfill(109, -131, BLACK);floodfill(-109, -131, BLACK);setfillcolor(WHITE);setlinestyle(PS_SOLID, 1);solidcircle(92, -137, 30);solidcircle(-92, -137, 30);setfillcolor(BLACK);solidcircle(90, -137, 26);solidcircle(-90, -137, 26);setfillcolor(WHITE);solidcircle(81, -146, 9);solidcircle(-81, -146, 9);
void DrawNose()setlinestyle(PS_SOLID, 1);setfillcolor(BLACK);solidellipse(-26, -106, 26, -63);
void DrawMouth()setlinecolor(BLACK);setlinestyle(PS_SOLID, 8);arc(-43, -75, 43, -7, PI, 0);
void DrawColour()setlinecolor(BLACK);setlinestyle(PS_SOLID, 8);setlinecolor(RGB(91, 198, 250));ellipse(-205, -265, 205, 74);setlinecolor(RGB(119, 216, 113));ellipse(-215, -275, 215, 84);
setlinecolor(RGB(254, 122, 185));ellipse(-225, -285, 225, 94);
void DrawLogo()RECT r = -116, 100, 116, 175 ;settextcolor(BLACK);setbkmode(TRANSPARENT);settextstyle(60, 0, _T("黑体"));drawtext(_T("BeiJing"), &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);r = -116, 175, 116, 250 ;drawtext(_T("2022"), &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
void DrawEllipse(int x0, int y0, int a, int b, int k, COLORREF color)double i;double x, y, tx, ty;for (i = -180; i <= 180; i = i + 0.5)x = a * cos(i * th);y = b * sin(i * th);tx = x;ty = y;x = tx * cos(k * th) - ty * sin(k * th) + x0;y = y0 - (ty * cos(k * th) + tx * sin(k * th));setfillcolor(color);solidcircle((int)x, (int)y, 2);
void heart(int x0, int y0, int size, COLORREF C)double m, n, x, y;double i;for (i = 0; i <= 2 * size; i = i + 0.01)// 产生极坐标点m = i;n = -size * (((sin(i) * sqrt(fabs(cos(i)))) / (sin(i) + 1.4142)) - 2 * sin(i) + 2);// 转换为笛卡尔坐标x = n * cos(m) + x0;y = n * sin(m) + y0;setfillcolor(C);solidcircle((int)x, (int)y, 1);

以上是关于用C语言代码实现一只可爱的冰墩墩!的主要内容,如果未能解决你的问题,请参考以下文章

#私藏项目实操分享#利用鸿蒙JavaUI 框架的 WebView 加载本地冰墩墩网页

被大家所喜爱的冰墩墩,是如何创新出来的?

自造冰墩墩!用Threejs自己实现一个冰墩墩!

怎么用易语言写一个冰墩墩

冰墩墩是怎么选出来的呢?

如何用python画冰墩墩?