用MATLAB画一只可爱的小熊

本文介绍了一个使用MATLAB绘制可爱小熊的代码实现。

通过定义身体各部分的参数(如头部半径、耳朵位置、四肢尺寸等),利用rectangle函数绘制圆形和圆角矩形,组合成完整的小熊形象。代码设置了不同颜色区分身体(深灰)、脸部(浅灰)、眼睛(黑)和鼻子(棕),并添加微笑曲线作为嘴巴。程序包含缩放比例调整功能,可整体改变小熊大小。最终生成一个600×600像素的白色背景图形,隐藏坐标轴,呈现出一个比例协调的卡通小熊形象。

可爱的小熊

Matlab 复制代码
%% MATLAB绘制可爱小熊

% 清理工作区
clear; clc; close all;

% --- 1. 定义小熊身体各部分的参数 ---
% 这些参数可以调整,以改变小熊的大小和比例
body_scale = 1; % 整体缩放比例

% 头部
head_center = [0, 5]; % 头部中心坐标
head_radius = 2.5 * body_scale; % 头部半径

% 耳朵
ear_radius = 0.8 * body_scale; % 耳朵半径
ear_offset_x = 1.8 * body_scale; % 耳朵在x方向上的偏移
ear_offset_y = 1.8 * body_scale; % 耳朵在y方向上的偏移

% 眼睛
eye_radius = 0.3 * body_scale; % 眼睛半径
eye_offset_x = 1.0 * body_scale; % 眼睛在x方向上的偏移
eye_offset_y = 0.5 * body_scale; % 眼睛在y方向上的偏移

% 鼻子
nose_width = 0.6 * body_scale; % 鼻子宽度
nose_height = 0.4 * body_scale; % 鼻子高度

% 嘴巴
mouth_width = 1.2 * body_scale; % 嘴巴宽度
mouth_height = 0.5 * body_scale; % 嘴巴高度

% 身体
body_width = 3.5 * body_scale; % 身体宽度
body_height = 3.0 * body_scale; % 身体高度

% 四肢
limb_width = 0.6 * body_scale; % 四肢宽度
limb_height = 2.0 * body_scale; % 四肢长度

% --- 2. 绘制小熊 ---
figure('Color', 'w', 'Position', [100, 100, 600, 600]);
hold on;
axis equal;
axis off; % 隐藏坐标轴

% 定义颜色
bear_color = [0.3, 0.3, 0.3]; % 小熊身体颜色 (深灰色)
face_color = [0.9, 0.9, 0.9]; % 小熊脸部颜色 (浅灰色)
eye_color = [0, 0, 0];        % 眼睛颜色 (黑色)
nose_color = [0.7, 0.2, 0.2]; % 鼻子颜色 (棕色)

% --- 开始绘制 ---

% 绘制身体 (使用rectangle函数绘制圆角矩形)
rectangle('Position', [-body_width/2, 0, body_width, body_height], ...
          'Curvature', [0.5, 0.5], 'FaceColor', bear_color, 'EdgeColor', 'none');

% 绘制头部 (使用rectangle函数绘制一个圆形)
rectangle('Position', [head_center(1)-head_radius, head_center(2)-head_radius, ...
                       2*head_radius, 2*head_radius], ...
          'Curvature', [1, 1], 'FaceColor', bear_color, 'EdgeColor', 'none');

% 绘制脸部 (一个稍小的圆形)
rectangle('Position', [head_center(1)-head_radius+0.5, head_center(2)-head_radius+0.5, ...
                       2*head_radius-1, 2*head_radius-1], ...
          'Curvature', [1, 1], 'FaceColor', face_color, 'EdgeColor', 'none');

% 绘制耳朵
% 左耳
rectangle('Position', [head_center(1)-ear_offset_x-ear_radius, head_center(2)+ear_offset_y-ear_radius, ...
                       2*ear_radius, 2*ear_radius], ...
          'Curvature', [1, 1], 'FaceColor', bear_color, 'EdgeColor', 'none');
% 右耳
rectangle('Position', [head_center(1)+ear_offset_x-ear_radius, head_center(2)+ear_offset_y-ear_radius, ...
                       2*ear_radius, 2*ear_radius], ...
          'Curvature', [1, 1], 'FaceColor', bear_color, 'EdgeColor', 'none');

% 绘制眼睛
% 左眼
rectangle('Position', [head_center(1)-eye_offset_x-eye_radius, head_center(2)+eye_offset_y-eye_radius, ...
                       2*eye_radius, 2*eye_radius], ...
          'Curvature', [1, 1], 'FaceColor', eye_color, 'EdgeColor', 'none');
% 右眼
rectangle('Position', [head_center(1)+eye_offset_x-eye_radius, head_center(2)+eye_offset_y-eye_radius, ...
                       2*eye_radius, 2*eye_radius], ...
          'Curvature', [1, 1], 'FaceColor', eye_color, 'EdgeColor', 'none');

% 绘制鼻子 (一个椭圆形)
rectangle('Position', [-nose_width/2, head_center(2)-nose_height/2, nose_width, nose_height], ...
          'Curvature', [1, 1], 'FaceColor', nose_color, 'EdgeColor', 'none');

% 绘制嘴巴 (一个微笑的弧线)
theta = linspace(pi, 2*pi, 100); % 角度从180度到360度
mouth_x = (mouth_width/2) * cos(theta);
mouth_y = -0.2 + (mouth_height/2) * sin(theta); % -0.2是为了让嘴巴位置下移一点
plot(mouth_x, mouth_y, 'k', 'LineWidth', 2);

% 绘制四肢
% 左手臂
rectangle('Position', [-body_width/2-limb_width, body_height/2, limb_width, limb_height], ...
          'Curvature', [0.3, 0.3], 'FaceColor', bear_color, 'EdgeColor', 'none');
% 右手臂
rectangle('Position', [body_width/2, body_height/2, limb_width, limb_height], ...
          'Curvature', [0.3, 0.3], 'FaceColor', bear_color, 'EdgeColor', 'none');
% 左腿部
rectangle('Position', [-body_width/4-limb_width/2, -limb_height, limb_width, limb_height], ...
          'Curvature', [0.3, 0.3], 'FaceColor', bear_color, 'EdgeColor', 'none');
% 右腿部
rectangle('Position', [body_width/4-limb_width/2, -limb_height, limb_width, limb_height], ...
          'Curvature', [0.3, 0.3], 'FaceColor', bear_color, 'EdgeColor', 'none');

% 添加标题
title('MATLAB 小熊', 'FontSize', 16, 'FontWeight', 'bold');

hold off;
相关推荐
三小河6 小时前
前端视角详解 Agent Skill
前端·javascript·后端
Aniugel6 小时前
单点登录(SSO)系统
前端
鹏多多6 小时前
移动端H5项目,还需要react-fastclick解决300ms点击延迟吗?
前端·javascript·react.js
serioyaoyao6 小时前
上万级文件一起可视化,怎么办?答案是基于 ParaView 的远程可视化
前端
万少6 小时前
端云一体 一天开发的元服务-奇趣故事匣经验分享
前端·ai编程·harmonyos
WindrunnerMax6 小时前
从零实现富文本编辑器#11-Immutable状态维护与增量渲染
前端·架构·前端框架
不想秃头的程序员6 小时前
Vue3 封装 Axios 实战:从基础到生产级,新手也能秒上手
前端·javascript·面试
数研小生6 小时前
亚马逊商品列表API详解
前端·数据库·python·pandas
你听得到116 小时前
我彻底搞懂了 SSE,原来流式响应效果还能这么玩的?(附 JS/Dart 双端实战)
前端·面试·github
不倒翁玩偶6 小时前
npm : 无法将“npm”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。
前端·npm·node.js