这些图形,大家不会陌生吧,小时候都画过。
有一天猫哥看见猴孩子们在玩自己从前玩的玩具,忽然灵光一闪。这不是圆内摆线嘛?
于是任务就来了,怎样用代码模拟的方式,生成所有的圆内摆线图形呢?
猫哥造了3个圆,外圆,内圆和笔迹圆,让内圆沿着外圆内部转动,笔迹圆记下的点就是我要的图形。
效果简直完美。
本来是想做给学编程的学生玩玩的,没想到他们缠着猫哥要讲原理。好吧,也许一年后,你们可以自己看懂圆内摆线生成的算法。
import math
import turtle
__Pen = turtle.Turtle()
def jsqjs():
global r, g, b, r1, g1, b1
c = (wyzgjd % (255 * 6))
if (c >= 255 * 5):
b += 1
elif (c >= 255 * 4):
g -= 1
elif (c >= 255 * 3):
r += 1
elif (c >= 255 * 2):
b -= 1
elif (c >= 255 * 1):
g += 1
else:
r -= 1
# 电脑版放出这几行
r1 = (r / 255)
g1 = (g / 255)
b1 = (b / 255)
# 网页版放出这几行
#r1 = r
#g1 = g
#b1 = b
def jsbjd():
global bjdx, bjdy
nyyxx = ((wybj - nybj) * math.cos(math.radians(wyzgjd)))
nyyxy = ((wybj - nybj) * math.sin(math.radians(wyzgjd)))
nyzgjd = ((wyzgjd * wybj) / nybj)
bjdjd = (wyzgjd - nyzgjd)
bjdx = (nyyxx + bjdbj * math.cos(math.radians(bjdjd)))
bjdy = (nyyxy + bjdbj * math.sin(math.radians(bjdjd)))
# return bjdx, bjdy
#开始进入Python的世界
def drawFlower():
global wybj, nybj, bjdbj, bjdx, bjdy, wyzgjd, r, g, b, r1, g1, b1
# 三个变量分别是:外圆半径,内圆半径,笔迹点半径。外圆半径>内圆半径>笔迹点半径
# set these number to make wybj > nybj > bjdbj, draw flowers.
wybj, nybj, bjdbj = 180, 127, 90
# 150, 93, 70 # 150, 61, 52
wyzgjd = 0
bjdx = ((wybj - nybj) + bjdbj)
bjdy, wyzgjd = 0, 0
r, g, b = 255, 0, 255
go = 'N'
__Pen.clear()
__Pen.penup()
__Pen.goto(bjdx, bjdy)
__Pen.pendown()
turtle.speed(100)
while (go == 'n' or go == 'N'):
for __count in range(7200):
jsqjs()
__Pen.color(r1, g1, b1)
wyzgjd += 1
# qjs = wyzgjd * 0x01040D % 0x1000000
# __Pen.pencolor('#%06X' % qjs)
# print(qjs)
jsbjd()
__Pen.goto(bjdx, bjdy)
go = '0'
while (go != 'Y' and go != 'N' and go != 'y' and go != 'n'):
go = input('gameOver? Y/N ')
drawFlower()
文末祭上两朵玫瑰,情人节快乐!
import turtle
import random
def drawStar():
turtle.bgcolor("#000000")
t = turtle.Pen()
t.hideturtle()
t.speed(200)
for n in range(128):
c = random.randint(25, 127)
s = random.random() * 5 + 1
t.penup()
t.goto(random.randint(-350, +350), random.randint(-300, +300))
t.pendown()
t.pencolor('#%02X%02X%02X' % (c, c, c))
t.dot(s)
c *= 3
t.pencolor('#%02X%02X%02X' % (c, c, c))
t.dot(s/2)
def drawRose():
len = 0
t = turtle.Pen()
t.showturtle()
t.speed(50)
t.penup()
t.pensize(2)
t.goto(0,0)
t.setheading(-90)
t.pendown()
for i in range(50):
for j in range(3):
t.forward(len)
t.right(360/8)
t.left(160)
t.pencolor("#11FF0B");
len+=0.5
for k in range(2):
t.pencolor("#EF5064")
t.forward(len)
t.right(360/6)
len+=0.4
drawStar()
drawRose()
input()
import turtle
import time
__Pen = turtle.Pen()
__Pen.pencolor("#ff0000")
__Pen.pensize(2)
__Pen.goto(0, 0)
__Pen.pendown()
i = 20
for __count in range(20):
__Pen.forward(i)
i += 5
__Pen.right(110)
__Pen.penup()
__Pen.pencolor("#cc66cc")
i = 0
for __count in range(5):
i += 1
__Pen.penup()
__Pen.goto(0, 0)
__Pen.setheading((i * 72))
__Pen.forward(80)
__Pen.pendown()
x = __Pen.xcor()
y = __Pen.ycor()
__Pen.right(120)
__Pen.forward(120)
__Pen.left(160)
__Pen.forward(80)
__Pen.goto(x, y)
time.sleep(20)
input("按下任意键继续")