|
| 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') |
0 commit comments