全套代码-之前做的5.47题
一个随机将点投射到一定范围的代码5.47题作为参考
import turtle
turtle.penup()
turtle.goto(-60, 50)
turtle.pendown()
turtle.goto(60, 50)
turtle.goto(60, -50)
turtle.goto(-60, -50)
turtle.goto(-60, 50)
turtle.penup()
for i in range(1, 10):
a = random.randint(-60, 60)
b = random.randint(-50, 50)
turtle.goto(a, b)
turtle.dot(6, "red")
turtle.done()
全套代码
顺序毕竟混乱
def drawRectangle(x, y, width, height, color="black"):
turtle.penup()
turtle.goto(x, y)
turtle.dot(6, "blue")
turtle.goto(x - width / 2, y - height / 2)
turtle.pendown()
turtle.forward(width)
turtle.left(90)
turtle.forward(height)
turtle.left(90)
turtle.forward(width)
turtle.left(90)
turtle.forward(height)
# 如果你把这行代码删除,你再运行,一定很好玩
turtle.right(270)
def judgmentCon(judgment):
if judgment == "R".lower():
xIn, yIn = eval(input("Enter x,y coordinate: "))
widthIn, heightIn = eval(input("Enter width and height: "))
pointInNum = eval(input("Enter point number: "))
pointInRec(xIn, yIn, widthIn, heightIn, pointInNum)
elif judgment == "C".lower():
xInC, yInC = eval(input("Enter x,y coordinate: "))
radiusC = eval(input("Enter radius: "))
pointInNumC = eval(input("Enter point number: "))
pointInCircle(xInC, yInC, radiusC, pointInNumC)
# 我们做个改变增加一下识别是矩形还是圆形
def pointInRec(xC, yC, width, height, pointNum):
# 一定要带int要不这个公式出来都是浮点数
aPointNegative = int(xC - width / 2)
aPointPositive = int(xC + width / 2)
bPointNegative = int(yC - height / 2)
bPointPositive = int(yC + height / 2)
turtle.penup()
turtle.goto(xC, yC)
turtle.dot(6, "blue")
turtle.goto(xC - width / 2, yC - height / 2)
turtle.pendown()
turtle.pendown()
turtle.forward(width)
turtle.left(90)
turtle.forward(height)
turtle.left(90)
turtle.forward(width)
turtle.left(90)
turtle.forward(height)
# 如果你把这行代码删除,你再运行,一定很好玩
turtle.right(270)
turtle.penup()
for i in range(1, pointNum):
a = random.randint(aPointNegative, aPointPositive)
b = random.randint(bPointNegative, bPointPositive)
turtle.goto(a, b)
turtle.dot(6, "red")
def pointInCircle(xC, yC, rC, pointNumC):
turtle.penup()
turtle.goto(xC, yC)
turtle.dot(6, "blue")
turtle.goto(xC, yC - rC)
turtle.pendown()
turtle.circle(rC)
turtle.penup()
for i in range(1, pointNumC):
a = random.randint(0, 50)
b = random.randint(-30, 30)
turtle.goto(a, b)
turtle.dot(6, "red")
def main():
a = input("Enter Rectangle is R and Circle is C: ")
judgmentCon(a)
main()
turtle.hideturtle()
turtle.done()
对代码拆分-绘制矩形不带随机点
def drawRectangle(x, y, width, height, color="black"):
turtle.penup()
turtle.goto(x, y)
turtle.dot(6, "blue")
turtle.goto(x - width / 2, y - height / 2)
turtle.pendown()
turtle.forward(width)
turtle.left(90)
turtle.forward(height)
turtle.left(90)
turtle.forward(width)
turtle.left(90)
turtle.forward(height)
# 如果你把这行代码删除,你再运行,一定很好玩
turtle.right(270)
对代码拆分-绘制圆形带随机点
def pointInCircle(xC, yC, rC, pointNumC):
turtle.penup()
turtle.goto(xC, yC)
turtle.dot(6, "blue")
turtle.goto(xC, yC - rC)
turtle.pendown()
turtle.circle(rC)
turtle.penup()
for i in range(1, pointNumC):
a = random.randint(0, 50)
b = random.randint(-30, 30)
turtle.goto(a, b)
turtle.dot(6, "red")
对不起我才发现我的矩形def是没有带随机投射原点的。而画圆的时候自己带了随机画原点
还有要说明。我这个在园中放随机点,因为面积坐标的问题。没有很好的解决
对代码拆分-绘制矩形带随机点
def pointInRec(xC, yC, width, height, pointNum):
# 一定要带int要不这个公式出来都是浮点数
aPointNegative = int(xC - width / 2)
aPointPositive = int(xC + width / 2)
bPointNegative = int(yC - height / 2)
bPointPositive = int(yC + height / 2)
turtle.penup()
turtle.goto(xC, yC)
turtle.dot(6, "blue")
turtle.goto(xC - width / 2, yC - height / 2)
turtle.pendown()
turtle.pendown()
turtle.forward(width)
turtle.left(90)
turtle.forward(height)
turtle.left(90)
turtle.forward(width)
turtle.left(90)
turtle.forward(height)
# 如果你把这行代码删除,你再运行,一定很好玩
turtle.right(270)
turtle.penup()
for i in range(1, pointNum):
a = random.randint(aPointNegative, aPointPositive)
b = random.randint(bPointNegative, bPointPositive)
turtle.goto(a, b)
turtle.dot(6, "red")
对代码拆分-判读需要的画矩形还是画圆形
def judgmentCon(judgment):
if judgment == "R".lower():
xIn, yIn = eval(input("Enter x,y coordinate: "))
widthIn, heightIn = eval(input("Enter width and height: "))
pointInNum = eval(input("Enter point number: "))
pointInRec(xIn, yIn, widthIn, heightIn, pointInNum)
elif judgment == "C".lower():
xInC, yInC = eval(input("Enter x,y coordinate: "))
radiusC = eval(input("Enter radius: "))
pointInNumC = eval(input("Enter point number: "))
pointInCircle(xInC, yInC, radiusC, pointInNumC)