MATLAB画小猪佩琪

本文介绍了如何使用MATLAB结合自定义椭圆函数和阿基米德螺旋线绘制小猪佩奇的图像。通过精心设计的参数和曲线方程,实现了小猪的各个部位,如‘猪尾巴’、‘手脚’、‘身体’等特征。代码逐步展示每个部分的绘制过程,为读者提供了一个有趣的MATLAB图形艺术实践。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

程序分析 

绘制小猪佩琪图像主要就是用许多椭圆、线条拼接而成的;为了方便绘制椭圆我自定义一个椭圆函数具体使用方法可以参考之前推文链接EllipseAndCircle函数

椭圆函数

function XY=EllipseAndCircle(varargin)
% author:2377389590@qq.com
% EllipseAndCircle(a,b,x,y,Angle,Rotate,LineColor,LineWidth,PlotType,FillColor,Alpha)
%                a、b :为长短半径
%                x,y :圆心坐标
%                Angle :开始/结束角度
%                Rotate:旋转角度
%                LineColor:线条颜色
%                LineWidth:线宽
%                FillColor :填充的颜色
%                Alpha:透明度
a=varargin{1};b=varargin{2};
x=varargin{3};y=varargin{4};
Angle=varargin{5};Rotate=varargin{6};
LineColor=varargin{7};
LineWidth=varargin{8};
theta=Angle(1):pi/100:Angle(2);
X=x+a*cos(theta); Y=y+b*sin(theta);
XY=[X;Y]'*[cos(Rotate),-sin(Rotate);sin(Rotate),cos(Rotate)];
if length(varargin)==8||strcmp(varargin{9},'plot' )
    plot(XY(:,1),XY(:,2),'-','color',LineColor,'linewidth',LineWidth);
    hold on; axis equal
elseif length(varargin)==11||strcmp(varargin{9},'fill' )
    try
        FillColor=varargin{10};
        Alpha=varargin{11};
        fill(XY(:,1),XY(:,2),FillColor,'LineWidth',LineWidth,'FaceAlpha',Alpha,'edgecolor',LineColor)
%         hold on; axis equal
    catch
        disp('参数输入错误1')
    end
else
    disp('参数输入错误2')
end
end

“猪尾巴”曲线

为了找到合适的符合“猪尾巴”卷曲的曲线方程,小编把落灰的高数书翻了一遍,最后发现使用阿基米德螺旋线变形方式可以达到类似的效果

r=a\theta

clc;clear;close all
theta = -5*pi:0.01*pi:5*pi;
r =0.2*theta;
x = r.*cos(theta);
y = -r.*sin(theta);
plot(x,y,'color','b','LineWidth',1.5)
ax=gca;
ax.TickDir='out';
ax.XMinorTick='on';
ax.YMinorTick='on';
ax.FontSize=14;
ax.LineWidth=1.5;
ax.Box='off';

完整代码 

【环境:matlab21b  收藏=学会】

clc;clear;close all;
%======================================
%  Author:好玩的MATLAB
%  Email : 2377389590@qq.com
%  Date  :2022-5-31
%--------------------------------------
ax=gca;
ax.XLim=[-5 5];
ax.YLim=[0 10];
hold on;
% ================猪尾巴==========================
% 使用阿基米德螺旋线变形方式绘制
theta = -pi:0.01*pi:pi;
r =0.2*theta;
x = r.*cos(theta);
y = -r.*sin(theta);
plot(x-2.6871,y+2.7277,'color',[0.98 0.68 0.87],'LineWidth',3)
pause(0.5)
%=================== 左手============================
line([-3.2 -1.7],[3.5 4.3],'Color',[0.85 0.55 0.73],'LineStyle','-','LineWidth',5,'LineJoin','round')
line([-3.3 -2.7],[3.9 3.8],'Color',[0.85 0.55 0.73],'LineStyle','-','LineWidth',5,'LineJoin','round')
line([-3 -2.7],[3.2 3.8],'Color',[0.85 0.55 0.73],'LineStyle','-','LineWidth',5,'LineJoin','round')
pause(0.5)
% ===================右手=============================
line([2.4 1.1],[3.3 4.3],'Color',[0.85 0.55 0.73],'LineStyle','-','LineWidth',5,'LineJoin','round')
line([2 2],[3.53 3.1],'Color',[0.85 0.55 0.73],'LineStyle','-','LineWidth',5,'LineJoin','round')
line([2 2.4],[3.5 3.7],'Color',[0.85 0.55 0.73],'LineStyle','-','LineWidth',5,'LineJoin','round')
pause(0.5)
% ===================左腿===============================
line([-1.4448 -1.4686],[1.8487 0.9921],'Color',[0.85 0.55 0.73],'LineStyle','-','LineWidth',8,'LineJoin','round')
pause(0.5)
% ==================右腿==============================
line([0.4349 0.4349],[1.85 1.0159],'Color',[0.85 0.55 0.73],'LineStyle','-','LineWidth',8,'LineJoin','round')
pause(0.5)
% ==================左脚===============================
EllipseAndCircle(0.6,0.2,-1.1,1,[0,2*pi],0,[0 0 0],2,'fill',[0 0 0],1);
pause(0.5)
% ==================右脚==============================
EllipseAndCircle(0.6,0.2,0.8,1,[0,2*pi],0,[0 0 0],2,'fill',[0 0 0],1);
pause(0.5)
%===================身体==============================
EllipseAndCircle(2.35,5,-0.27,0.4,[0.3,2.861],0,[ 0.71 0.2 0.3],4,'fill',[1 0.35 0.37],1);
pause(0.5)
% ==================耳朵===============================
EllipseAndCircle(0.4,0.63,2.5,8,[0,2*pi],-0.4,[0.85 0.55 0.73],3,'fill',[0.98 0.68 0.87],1);
EllipseAndCircle(0.4,0.63,3.5,7,[0,2*pi],-0.7,[0.85 0.55 0.73],3,'fill',[0.98 0.68 0.87],1);
pause(0.5)
% =================头和嘴巴=========================
EllipseAndCircle(1.9,1.8,-0.27,6,[0,2*pi],0,[0.98 0.68 0.87],2,'fill',[0.98 0.68 0.87],1);
EllipseAndCircle(0.8,0.6,1.8,5.5,[pi,2*pi],-0.3,[ 0.7 0.4 0.58],4,'fill',[0.98 0.68 0.87],1);%嘴巴
EllipseAndCircle(1.3,2.4,-6.5,2.7,[0,2*pi],1.2,[0.98 0.68 0.87],2,'fill',[0.98 0.68 0.87],1);
pause(0.5)
% ==================鼻子=============================
EllipseAndCircle(0.52,0.8,4.58,6.5,[0,2*pi],-0.37,[0.85 0.55 0.73],3,'fill',[0.98 0.68 0.87],1);
EllipseAndCircle(0.1,0.1,1.67,7.67,[0,2*pi],0,[0.85 0.55 0.73],2,'fill',[0.85 0.55 0.73],1);
EllipseAndCircle(0.1,0.1,2.1,7.8,[0,2*pi],0,[0.85 0.55 0.73],2,'fill',[0.85 0.55 0.73],1);
% ==================眼睛=============================
EllipseAndCircle(0.33,0.33,0.22,7.8,[0,2*pi],0,[0.85 0.55 0.73],2,'fill',[1 1 1],1);
EllipseAndCircle(0.33,0.33,-0.6,7.4,[0,2*pi],0,[0.85 0.55 0.73],2,'fill',[1 1 1],1);
EllipseAndCircle(0.1,0.1,-0.5,7.4,[0,2*pi],0,[0 0 0],2,'fill',[0 0 0],1);
EllipseAndCircle(0.1,0.1,0.32,7.8,[0,2*pi],0,[0 0 0],2,'fill',[0 0 0],1);
pause(0.5)
% ==================腮红==============================
EllipseAndCircle(0.6,0.6,-1.4,6,[0,2*pi],0,[ 1 0.55 0.8],4,'fill',[1 0.55 0.8],1);

function XY=EllipseAndCircle(varargin)
% author:2377389590@qq.com
% EllipseAndCircle(a,b,x,y,Angle,Rotate,LineColor,LineWidth,PlotType,FillColor,Alpha)
%                a、b :为长短半径
%                x,y :圆心坐标
%                Angle :开始/结束角度
%                Rotate:旋转角度
%                LineColor:线条颜色
%                LineWidth:线宽
%                FillColor :填充的颜色
%                Alpha:透明度
a=varargin{1};b=varargin{2};
x=varargin{3};y=varargin{4};
Angle=varargin{5};Rotate=varargin{6};
LineColor=varargin{7};
LineWidth=varargin{8};
theta=Angle(1):pi/100:Angle(2);
X=x+a*cos(theta); Y=y+b*sin(theta);
XY=[X;Y]'*[cos(Rotate),-sin(Rotate);sin(Rotate),cos(Rotate)];
if length(varargin)==8||strcmp(varargin{9},'plot' )
    plot(XY(:,1),XY(:,2),'-','color',LineColor,'linewidth',LineWidth);
elseif length(varargin)==11||strcmp(varargin{9},'fill' )
    try
        FillColor=varargin{10};
        Alpha=varargin{11};
        fill(XY(:,1),XY(:,2),FillColor,'LineWidth',LineWidth,'FaceAlpha',Alpha,'edgecolor',LineColor)
    catch
        disp('参数输入错误1')
    end
else
    disp('参数输入错误2')
end
end

源码下载:

gitee下载:https://gitee.com/iDMatlab/piggy-peggy/tree/master

温馨提示:如果出现404,可能原因是代码还没有审核通过。

参考资料:

【1】【好玩的源码】MATLAB画雪容融

好的,下面是使用Python的turtle库绘制的代码: ```python import turtle # 设置布大小和背景颜色 turtle.setup(800, 600) turtle.bgcolor('#FFC0CB') # 绘制的头 turtle.penup() turtle.goto(0, 100) turtle.pendown() turtle.fillcolor('#FFFFFF') turtle.begin_fill() turtle.circle(120) turtle.end_fill() # 绘制的眼睛 turtle.penup() turtle.goto(-40, 160) turtle.pendown() turtle.fillcolor('#000000') turtle.begin_fill() turtle.circle(20) turtle.end_fill() turtle.penup() turtle.goto(40, 160) turtle.pendown() turtle.fillcolor('#000000') turtle.begin_fill() turtle.circle(20) turtle.end_fill() # 绘制的鼻子 turtle.penup() turtle.goto(0, 100) turtle.pendown() turtle.fillcolor('#FFC0CB') turtle.begin_fill() turtle.circle(40) turtle.end_fill() # 绘制的嘴巴 turtle.penup() turtle.goto(-60, 20) turtle.pendown() turtle.pensize(8) turtle.right(60) turtle.circle(80, 120) # 绘制的身体 turtle.penup() turtle.goto(-150, -80) turtle.pendown() turtle.fillcolor('#FFFFFF') turtle.begin_fill() turtle.forward(300) turtle.left(90) turtle.circle(150, 180) turtle.left(90) turtle.forward(300) turtle.end_fill() # 绘制的手臂 turtle.penup() turtle.goto(-150, -80) turtle.pendown() turtle.fillcolor('#FFC0CB') turtle.begin_fill() turtle.right(45) turtle.forward(100) turtle.right(45) turtle.forward(80) turtle.right(90) turtle.forward(80) turtle.right(45) turtle.forward(100) turtle.right(135) turtle.forward(100) turtle.right(45) turtle.forward(80) turtle.right(90) turtle.forward(80) turtle.right(45) turtle.forward(100) turtle.end_fill() # 绘制的脚 turtle.penup() turtle.goto(-70, -300) turtle.pendown() turtle.fillcolor('#000000') turtle.begin_fill() turtle.right(90) turtle.forward(80) turtle.right(90) turtle.forward(150) turtle.right(90) turtle.forward(80) turtle.end_fill() turtle.penup() turtle.goto(70, -300) turtle.pendown() turtle.fillcolor('#000000') turtle.begin_fill() turtle.right(90) turtle.forward(80) turtle.right(90) turtle.forward(150) turtle.right(90) turtle.forward(80) turtle.end_fill() # 隐藏笔 turtle.hideturtle() # 显示绘制结果 turtle.done() ``` 这是绘制出的的图片: ![](https://i.imgur.com/zzSjKBA.png)
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

好玩的Matlab(NCEPU)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值