2021年中秋即将到来,作为程序员,前有「产品迭代」,后有「运营需求」。
作为程序员的你:
快来分享你的心得吧!
● 围绕如何在中秋节前高效完成需求,发表见解 50 字以上(需原创,禁止转载)
回答赞同数 TOP10 的用户将有机会获得精美定制小礼品一份
腾讯云开发者社区小助手 9 月 24 日在获奖评论下通知答主,奖品将于30日内发放
更多精彩问答与定制好礼,尽请关注 【云+有奖问答专题】 \( ̄▽ ̄)/
public static void main(String[] args) {
int r = 20;
for (int y = 0; y <= 2 * r; y += 4) {
int x = (int) Math.round(r - Math.sqrt(2 * r * y - y * y));
int len = 2 * (r - x);
for (int i = 0; i <= x; i++) {
System.out.print(' ');
}
System.out.print('*');
if(y==r){
for (int j = 0; j <= len/4; j++) {
System.out.print("月饼");
}
}else {
for (int j = 0; j <= len; j++) {
System.out.print(' ');
}
}
System.out.println('*');
}
}
效果
* *
* *
* *
* *
* *
*月饼月饼月饼月饼月饼月饼月饼月饼月饼月饼月饼*
* *
* *
* *
* *
* *
首先来张效果图把:
1、首先我们需要导入画图和数据计算的相关库。
#导入画图和数据计算的相关库
import numpy as np
from numpy import sin, cos, pi
import matplotlib.pyplot as plt
import matplotlib.patches as mpatch
from matplotlib.patches import Arc, Circle, Wedge
from matplotlib.collections import PatchCollection
from matplotlib.font_manager import FontProperties
2、为了在图片上显示中文,需要先加载Alibaba-PuHuiTi-Medium字体下载地址:
#为了在图片上显示中文,加载字体
!wget http://tianchi-media.oss-cn-beijing.aliyuncs.com/DSW/Python/miniproject/01_draw_moon_cake/Alibaba-PuHuiTi-Medium.ttf
3、然后设置3个变量,这3个变量的用途到后面会有解释:
#设置变量
length = 20R = 3**0.5*length/(3**0.5*cos(pi/12)-sin(pi/12))r = 2*sin(pi/12)*R/3**0.5
4、接下来我们要画弧线,弧线是月饼边缘的花纹:
arc1 = Arc([0, length], width=2.9*r, height=2.9*r, angle=0, theta1=60, theta2=420, ec='orange', linewidth=4)
5、其中 [0, length] 是这个Arc弧线的圆心,因为这个Arc类是一个椭圆形的一部分,所以其包含圆心、横轴、纵轴等参数,length变量就是arc1圆心的纵坐标,width=2r 和 height=2r 就说明这个椭圆的纵轴和横轴长度相等,其是一个圆,而不是椭圆,变量 r 就是这个圆的半径。angle=0 就是我们不需要对这个圆进行旋转,theta1=30 和 theta2=150 是指明这个弧线的起始角度和终止角度,这里的角度分别是30度和150度。而 ec=‘orange’ 和linewidth=4 分别指边线的颜色和线条宽度,颜色我们设为橙色,宽度为4。而这些弧线的效果图如图2所示,在图中笔者标出了arc1、arc5和arc9这3条弧线的位置,所有弧线按照逆时针方向排列。这些位置大同小异,大家只要计算好其各自的位置参数就行,这里就不再赘述了。
接下来我们要画剩下的11条弧线
#绘制弧线
arc1 = Arc([0, length], width=2.9*r, height=2.9*r, angle=0, theta1=60, theta2=420, ec='orange', linewidth=4)
arc2 = Arc([-length/2, length/2*3**0.5], width=2.9*r, height=2.9*r,
angle=0, theta1=120, theta2=480, ec='orange', linewidth=4)
arc3 = Arc([-length/2*3**0.5, length/2], width=2.9*r, height=2.9*r,
angle=0, theta1=180, theta2=540, ec='orange', linewidth=4)
arc4 = Arc([-length, 0], width=2.9*r, height=2.9*r, angle=0, theta1=240, theta2=600, ec='orange', linewidth=4)
arc5 = Arc([-length/2*3**0.5, -length/2], width=2.9*r, height=2.9*r,
angle=0, theta1=300, theta2=660,ec='orange', linewidth=4)
arc6 = Arc([-length/2, -length/2*3**0.5], width=2.9*r, height=2.9*r,
angle=0, theta1=360, theta2=720, ec='orange', linewidth=4)
arc7 = Arc([0, -length], width=2.9*r, height=2.9*r, angle=0, theta1=420, theta2=780, ec='orange', linewidth=4)
arc8 = Arc([length/2, -length/2*3**0.5], width=2.9*r, height=2.9*r,
angle=0, theta1=480, theta2=840, ec='orange', linewidth=4)
arc9 = Arc([length/2*3**0.5, -length/2], width=2.9*r, height=2.9*r,
angle=0, theta1=540, theta2=900, ec='orange', linewidth=4)
arc10 = Arc([length, 0], width=2.9*r, height=2.9*r, angle=0, theta1=600, theta2=960, ec='orange', linewidth=4)
arc11 = Arc([length/2*3**0.5, length/2], width=2.9*r, height=2.9*r,
angle=0, theta1=660, theta2=1020, ec='orange', linewidth=4)
arc12 = Arc([length/2, length/2*3**0.5], width=2.9*r, height=2.9*r,
angle=0, theta1=720, theta2=1080, ec='orange', linewidth=4)
6、然后我们再画一个圆,这个圆是月饼的主体部分,其圆心和整个月饼的中心是重合的,圆心位置我们选在了坐标原点,代码如下:
circle = Circle((0,0), R, ec='orange', fc='white', linewidth=4) ##ec为线条颜色,fc为填充颜色,可以自由替换
circle2 = Circle((0,0), (1/4+1)*R, ec='orange', fc='brown', linewidth=4)
7、这里变量R就是这个大圆的半径,边线颜色(ec)设置为橙色,表面颜色设为白色。 接下来我们再画上一些花纹,这些花纹主要由8个扇形组成,其中有4个大的扇形和4个小的扇形,我们先画一个,代码如下:
wedge1 = Wedge([-2, 2], R-5, 90, 180,
ec='orange', fc=r'white', linewidth=4) ##ec为线条颜色,fc为填充颜色,可以自由替换
8、其中[-2, 2]是这个Wedge所代表的圆的圆心,R-5 是其半径,90 和180分别代表起始和终止角度,ec、fc和linewidth这些都和前面Arc类的用法相似,这8个扇形的效果图如图4所示。在图中笔者标出了wedge1、wedge2、wedge5和wedge6的位置,所有扇形按照逆时针方向放置。 了解以后我们把剩下的部分给画完
wedge1 = Wedge([-2, 2], R-5, 90, 180,
ec='orange', fc=r'white', linewidth=4) ##ec为线条颜色,fc为填充颜色,可以自由替换
wedge2 = Wedge([-5, 5], R-12, 90, 180, ec='orange',
fc=r'brown', linewidth=4)
wedge3 = Wedge([-2, -2], R-5, 180, 270, ec='orange',
fc=r'white', linewidth=4)
wedge4 = Wedge([-5, -5], R-12, 180, 270, ec='orange',
fc=r'brown', linewidth=4)
wedge5 = Wedge([2, -2], R-5, 270, 360, ec='orange',
fc=r'white', linewidth=4)
wedge6 = Wedge([5, -5], R-12, 270, 360, ec='orange',
fc=r'brown', linewidth=4)
wedge7 = Wedge([2, 2], R-5, 0, 90, ec='orange',
fc=r'white', linewidth=4)
wedge8 = Wedge([5, 5], R-12, 0, 90, ec='orange',
fc=r'brown', linewidth=4)
9、最后我们再把自己取好的名称加上,因为代码不多,所以把剩余所有代码都写在这里:
art_list = [circle2,arc1, arc2, arc3, arc4, arc5, arc6, arc7, arc8, arc9, arc10, arc11, arc12]
art_list.extend([circle, wedge1, wedge2, wedge3, wedge4, wedge5, wedge6, wedge7, wedge8])
fig, ax = plt.subplots(figsize=(8,8))
ax.set_aspect('equal')
for a in art_list:
ax.add_patch(a)
plt.axis('off')
font_set = FontProperties(fname=r"Alibaba-PuHuiTi-Medium.ttf", size=12) ##可以自由下载字体使用
plt.text(-15, 7, '中',fontproperties=font_set,color='orange',fontsize=49) ##ec为线条颜色,color为字体颜色,可以自由替换
plt.text(-15, -13, '快',fontproperties=font_set,color='orange',fontsize=49) ##ec为线条颜色,color为字体颜色,可以自由替换
plt.text(7, 7, '秋',fontproperties=font_set,color='orange',fontsize=49) ##ec为线条颜色,color为字体颜色,可以自由替换
plt.text(7, -13, '乐',fontproperties=font_set,color='orange',fontsize=49) ##ec为线条颜色,color为字体颜色,可以自由替换
plt.text(-25,-40,'但愿人长久,千里共婵娟',fontproperties=font_set, fontsize=30, color='#aa4a30')
plt.ylim([-35, 35])
plt.xlim([-35, 35])
plt.show()
铛铛,一个月饼就画好啦!
#导入画图和数据计算的相关库
import numpy as np
from numpy import sin, cos, pi
import matplotlib.pyplot as plt
import matplotlib.patches as mpatch
from matplotlib.patches import Arc, Circle, Wedge
from matplotlib.collections import PatchCollection
from matplotlib.font_manager import FontProperties
#为了在图片上显示中文,加载字体
!wget http://tianchi-media.oss-cn-beijing.aliyuncs.com/DSW/Python/miniproject/01_draw_moon_cake/Alibaba-PuHuiTi-Medium.ttf
#设置变量
length = 20
R = 3**0.5*length/(3**0.5*cos(pi/12)-sin(pi/12))
r = 2*sin(pi/12)*R/3**0.5
#绘制弧线
arc1 = Arc([0, length], width=2.9*r, height=2.9*r, angle=0, theta1=60, theta2=420, ec='orange', linewidth=4)
arc2 = Arc([-length/2, length/2*3**0.5], width=2.9*r, height=2.9*r,
angle=0, theta1=120, theta2=480, ec='orange', linewidth=4)
arc3 = Arc([-length/2*3**0.5, length/2], width=2.9*r, height=2.9*r,
angle=0, theta1=180, theta2=540, ec='orange', linewidth=4)
arc4 = Arc([-length, 0], width=2.9*r, height=2.9*r, angle=0, theta1=240, theta2=600, ec='orange', linewidth=4)
arc5 = Arc([-length/2*3**0.5, -length/2], width=2.9*r, height=2.9*r,
angle=0, theta1=300, theta2=660,ec='orange', linewidth=4)
arc6 = Arc([-length/2, -length/2*3**0.5], width=2.9*r, height=2.9*r,
angle=0, theta1=360, theta2=720, ec='orange', linewidth=4)
arc7 = Arc([0, -length], width=2.9*r, height=2.9*r, angle=0, theta1=420, theta2=780, ec='orange', linewidth=4)
arc8 = Arc([length/2, -length/2*3**0.5], width=2.9*r, height=2.9*r,
angle=0, theta1=480, theta2=840, ec='orange', linewidth=4)
arc9 = Arc([length/2*3**0.5, -length/2], width=2.9*r, height=2.9*r,
angle=0, theta1=540, theta2=900, ec='orange', linewidth=4)
arc10 = Arc([length, 0], width=2.9*r, height=2.9*r, angle=0, theta1=600, theta2=960, ec='orange', linewidth=4)
arc11 = Arc([length/2*3**0.5, length/2], width=2.9*r, height=2.9*r,
angle=0, theta1=660, theta2=1020, ec='orange', linewidth=4)
arc12 = Arc([length/2, length/2*3**0.5], width=2.9*r, height=2.9*r,
angle=0, theta1=720, theta2=1080, ec='orange', linewidth=4)
#画圆
circle = Circle((0,0), R, ec='orange', fc='white', linewidth=4) ##ec为线条颜色,fc为填充颜色,可以自由替换
circle2 = Circle((0,0), (1/4+1)*R, ec='orange', fc='brown', linewidth=4)
wedge1 = Wedge([-2, 2], R-5, 90, 180,
ec='orange', fc=r'white', linewidth=4) ##ec为线条颜色,fc为填充颜色,可以自由替换
wedge2 = Wedge([-5, 5], R-12, 90, 180, ec='orange',
fc=r'brown', linewidth=4)
wedge3 = Wedge([-2, -2], R-5, 180, 270, ec='orange',
fc=r'white', linewidth=4)
wedge4 = Wedge([-5, -5], R-12, 180, 270, ec='orange',
fc=r'brown', linewidth=4)
wedge5 = Wedge([2, -2], R-5, 270, 360, ec='orange',
fc=r'white', linewidth=4)
wedge6 = Wedge([5, -5], R-12, 270, 360, ec='orange',
fc=r'brown', linewidth=4)
wedge7 = Wedge([2, 2], R-5, 0, 90, ec='orange',
fc=r'white', linewidth=4)
wedge8 = Wedge([5, 5], R-12, 0, 90, ec='orange',
fc=r'brown', linewidth=4)
art_list = [circle2,arc1, arc2, arc3, arc4, arc5, arc6, arc7, arc8, arc9, arc10, arc11, arc12]
art_list.extend([circle, wedge1, wedge2, wedge3, wedge4, wedge5, wedge6, wedge7, wedge8])
fig, ax = plt.subplots(figsize=(8,8))
ax.set_aspect('equal')
for a in art_list:
ax.add_patch(a)
plt.axis('off')
font_set = FontProperties(fname=r"Alibaba-PuHuiTi-Medium.ttf", size=12) ##可以自由下载字体使用
plt.text(-15, 7, '中',fontproperties=font_set,color='orange',fontsize=49) ##ec为线条颜色,color为字体颜色,可以自由替换
plt.text(-15, -13, '快',fontproperties=font_set,color='orange',fontsize=49) ##ec为线条颜色,color为字体颜色,可以自由替换
plt.text(7, 7, '秋',fontproperties=font_set,color='orange',fontsize=49) ##ec为线条颜色,color为字体颜色,可以自由替换
plt.text(7, -13, '乐',fontproperties=font_set,color='orange',fontsize=49) ##ec为线条颜色,color为字体颜色,可以自由替换
plt.text(-25,-40,'但愿人长久,千里共婵娟',fontproperties=font_set, fontsize=30, color='#aa4a30')
plt.ylim([-35, 35])
plt.xlim([-35, 35])
plt.show()
效果预览:
<!doctype html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>中秋快乐</title>
<style>
.blessing {
font-size: 100px;
letter-spacing: 40px;
width: 400px;
text-align: center;
line-height: 200px;
color: #fede65;
}
span{position:absolute; color: #f00;}
</style>
</head>
<body>
<div class="blessing">阖家平安</div>
<script type="text/javascript">
function moonCake(r) {
var PI = Math.PI;
for(var i = 0; i < 360; i += 6) {
var O = document.createElement('span');
O.appendChild(document.createTextNode('.'));
O.style.left = (Math.cos(PI / 180 * i) * r + r) + 'px';
O.style.top = (Math.sin(PI / 180 * i) * r + r) + 'px';
document.body.appendChild(O);
}
}
moonCake(200);
</script>
</body>
</html>
<!doctype html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>中秋快乐</title>
<style>
.blessing {
font-size: 100px;
letter-spacing: 40px;
width: 400px;
text-align: center;
line-height: 200px;
border-radius: 200px;
background: #c96904;
color: #fede65;
border: 5px solid #fede65;
}
</style>
</head>
<body>
<div class="blessing">阖家平安</div>
</body>
</html>