美文网首页
【2024-01-05】用python解决小学几何题

【2024-01-05】用python解决小学几何题

作者: 猫叽先森 | 来源:发表于2024-01-04 10:28 被阅读0次
image.png

老婆发了道题过来,想解释的话,画图是最明显的吧,就用python的turtle模块试试咯。

import turtle
t = turtle.Pen()

i = 0
t.forward(50)

while i < 7 :
    t.left(45)
    t.forward(100)
    i += 1

t.left(45)
t.forward(50)

t.circle(120.71)
t.left(90)
t.forward(241.42)
turtle.mainloop()

效果还不错,就是用三角函数算边长半径花了点时间。



把问题丢给了chatGPT



跟着chatGPT学python

import numpy as np
import matplotlib.pyplot as plt

# Function to draw a regular octagon circumscribed around a circle
def draw_circumscribed_octagon(radius):
    angle = np.pi / 4  # 45 degrees in radians
    points = []
    for i in range(8):
        x = radius * np.cos(i * angle)
        y = radius * np.sin(i * angle)
        points.append((x, y))
    points.append(points[0])  # Closing the octagon
    return points

# Circle parameters
radius = 1  # radius of the circle
theta = np.linspace(0, 2 * np.pi, 100)

# Circle coordinates
x_circle = radius * np.cos(theta)
y_circle = radius * np.sin(theta)

# Getting the points for the circumscribed octagon
circumscribed_octagon_points = draw_circumscribed_octagon(radius)
x_circumscribed_octagon, y_circumscribed_octagon = zip(*circumscribed_octagon_points)

# Plotting the circle, the circumscribed octagon, and the perpendicular lines
plt.figure(figsize=[6, 6])
plt.plot(x_circle, y_circle, label="Circle", color='green')  # Circle in green
plt.plot(x_circumscribed_octagon, y_circumscribed_octagon, label="Circumscribed Regular Octagon", color='orange')

# Plotting perpendicular lines from the center to each vertex of the octagon
for i in range(len(x_circumscribed_octagon) - 1):
    plt.plot([0, x_circumscribed_octagon[i]], [0, y_circumscribed_octagon[i]], color='blue', linestyle='--')

# Setting equal aspect ratio
plt.axis('equal')
plt.title("Circle with Circumscribed Regular Octagon and Perpendicular Lines to Vertices")
plt.legend()
plt.grid(True)
plt.show()

相关文章

网友评论

      本文标题:【2024-01-05】用python解决小学几何题

      本文链接:https://www.haomeiwen.com/subject/nnavndtx.html