Skip to content

Commit 02d7a76

Browse files
committed
更新对应文章代码——轩辕御龙
1 parent a04d2d7 commit 02d7a76

File tree

7 files changed

+156
-0
lines changed

7 files changed

+156
-0
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import turtle
2+
import math
3+
import random
4+
5+
import sys
6+
7+
class Spiro:
8+
def __init__(self, R, r, l, num, color):
9+
"""
10+
:params R: int. 大圆半径
11+
:params r: int. 小圆半径
12+
:params l: float. 画笔到小圆圆心的距离与小圆半径之比,在[0,1)区间
13+
:params num: int. 要画的完整曲线个数
14+
:params color: string, (int, int, int), (float, float, float). 指定曲线颜色
15+
"""
16+
self.R = R
17+
self.r = r
18+
self.l = l
19+
self.num = num
20+
self.pen = turtle.Turtle()
21+
self.pen.pencolor(color)
22+
turtle.colormode(1.0)
23+
24+
25+
def drawSingleSpiro(self):
26+
# 周期数 p
27+
p = self.periods()
28+
29+
for i in range(0, 360*p + 2, 2):
30+
if i != 0:
31+
self.pen.setpos(*self.cor_x_y_Spiro(i))
32+
else:
33+
self.pen.up()
34+
self.pen.setpos(*self.cor_x_y_Spiro(i))
35+
self.pen.down()
36+
37+
38+
def drawWhole(self):
39+
for s in range(self.num):
40+
if s != 0:
41+
self.randomSetting()
42+
self.drawSingleSpiro()
43+
else:
44+
self.drawSingleSpiro()
45+
46+
47+
def randomSetting(self):
48+
self.l = random.random()
49+
50+
# r = int(random.random() * 255)
51+
# g = int(random.random() * 255)
52+
# b = int(random.random() * 255)
53+
r = random.random()
54+
g = random.random()
55+
b = random.random()
56+
self.pen.pencolor((r, g, b))
57+
58+
59+
def hcf(self, x, y):
60+
if x == y:
61+
result = x
62+
elif x > y:
63+
result = self.hcf(x-y, y)
64+
else:
65+
result = self.hcf(x, y-x)
66+
return result
67+
68+
69+
def periods(self):
70+
div = self.hcf(self.R, self.r)
71+
return self.r//div
72+
73+
74+
def cor_x_y_Spiro(self, theta):
75+
k = self.r/self.R
76+
ef = 1 - k
77+
78+
rad = self.degreeToRadian(theta)
79+
x = self.R*(ef*math.cos(rad) + self.l*k*math.cos(ef/k*rad))
80+
y = self.R*(ef*math.sin(rad) - self.l*k*math.sin(ef/k*rad))
81+
82+
return (x, y)
83+
84+
85+
def degreeToRadian(self, degree):
86+
return degree * math.pi / 180
87+
88+
if __name__ == "__main__":
89+
# s = Spiro(100, 40, 0.6, 10, "pink")
90+
# s.drawWhole()
91+
# turtle.mainloop()
92+
if len(sys.argv) == 1:
93+
"""
94+
示例输入:python Spiro.py
95+
"""
96+
s = Spiro(100, 40, 0.6, 10, "pink")
97+
s.drawWhole()
98+
turtle.mainloop()
99+
elif len(sys.argv[5].split(',')) == 1:
100+
"""
101+
示例输入:python Spiro.py 100 40 0.6 10 pink
102+
"""
103+
s = Spiro(int(sys.argv[1]), int(sys.argv[2]), float(sys.argv[3]),
104+
int(sys.argv[4]), sys.argv[5])
105+
s.drawWhole()
106+
turtle.mainloop()
107+
else:
108+
"""
109+
示例输入:python Spiro.py 100 40 0.6 10 "251,165,59"
110+
"""
111+
rgb_list = sys.argv[5].split(',')
112+
rgb =[]
113+
for color in rgb_list:
114+
color = int(color)/255
115+
rgb.append(color)
116+
117+
rgb = tuple(rgb)
118+
s = Spiro(int(sys.argv[1]), int(sys.argv[2]), float(sys.argv[3]),
119+
int(sys.argv[4]), rgb)
120+
s.drawWhole()
121+
turtle.mainloop()
122+
# print(sys.argv)
123+
# # print(sys.argv[1].split(','))
124+
# int('25,36,55')

xuanyuanyulong/2020-02-29-python-spiro/__init__.py

Whitespace-only changes.

xuanyuanyulong/2020-03-27-PDF-watermark/__init__.py

Whitespace-only changes.
2.27 MB
Binary file not shown.
104 KB
Binary file not shown.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import PyPDF2
2+
3+
inputName = "input.pdf"
4+
watermarkName = "watermark.pdf"
5+
outputName = "output.pdf"
6+
7+
pdfInput = PyPDF2.PdfFileReader(inputName)
8+
watermark = PyPDF2.PdfFileReader(watermarkName).getPage(0)
9+
10+
pdfWriter = PyPDF2.PdfFileWriter()
11+
12+
# print(pdfInput.numPages)
13+
# print(pdfInput.getNumPages())
14+
15+
for i in range(pdfInput.numPages):
16+
page = pdfInput.getPage(i)
17+
page.mergePage(watermark)
18+
pdfWriter.addPage(page)
19+
20+
with open(outputName, "wb") as f:
21+
pdfWriter.write(f)

xuanyuanyulong/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Python 代码实例
2+
3+
Python技术 公众号文章代码库
4+
5+
- [2020-02-29-python-spiro](https://github.com/JustDoPython/python-examples/tree/master/xuanyuanyulong/2020-02-29-python-spiro)
6+
- [2020-03-27-PDF-watermark](https://github.com/JustDoPython/python-examples/tree/master/xuanyuanyulong/2020-03-27-PDF-watermark)
7+
8+
9+
关注公众号:python 技术,回复"python"一起学习交流
10+
11+
![](http://favorites.ren/assets/images/python.jpg)

0 commit comments

Comments
 (0)