我是靠谱客的博主 开心棒球,这篇文章主要介绍MATLAB画小猪佩琪,现在分享给大家,希望可以做个参考。

程序分析 

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

椭圆函数

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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

“猪尾巴”曲线

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

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
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  收藏=学会】

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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画雪容融

最后

以上就是开心棒球最近收集整理的关于MATLAB画小猪佩琪的全部内容,更多相关MATLAB画小猪佩琪内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(77)

评论列表共有 0 条评论

立即
投稿
返回
顶部