news 2026/6/23 22:30:31

DeepSeek辅助Python编写直角多边形拟合圆轮廓并画图

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
DeepSeek辅助Python编写直角多边形拟合圆轮廓并画图

为了测试多边形之间的包含关系,实现了用户设置圆半径和单位长度,程序自动确定圆心位置。

importmathimportturtledefgenerate_polygon_circle(radius,unit_length):""" 生成近似圆的多边形轮廓顶点坐标 参数: radius: 半径 unit_length: 单位长度(多边形的步长) 返回: list: 多边形顶点坐标列表,按顺时针顺序 """ifradius<=0orunit_length<=0:return[]vertices=[]# 圆心设为(r, r)以便所有坐标为正center_x=radius center_y=radius# 计算总步数(每90度圆弧的步数)steps_per_quadrant=max(1,int(radius/unit_length))# 存储第一象限的点(不含起点)first_quadrant_points=[]# 计算第一象限的点(从(0, r)开始,顺时针到(r, 0))foriinrange(steps_per_quadrant+1):x=i*unit_lengthifx>radius:# 防止x大于半径x=radius y_sq=max(0,radius**2-x**2)y=math.sqrt(y_sq)first_quadrant_points.append((x,y))# 起点:正上方 (r, 2r)start_x=center_x start_y=center_y+radius vertices.append((start_x,start_y))# 第一象限:右上1/4圆弧(从顶部到右侧)# 从第一个点(接近顶部)开始foriinrange(len(first_quadrant_points)-1,-1,-1):x_offset,y_offset=first_quadrant_points[i]x=center_x+x_offset y=center_y+y_offsetifvertices:last_x,last_y=vertices[-1]# 插入直角顶点iflast_x!=xandlast_y!=y:vertices.append((last_x,y))vertices.append((x,y))# 第二象限:右下1/4圆弧(从右侧到底部)foriinrange(len(first_quadrant_points)):x_offset,y_offset=first_quadrant_points[i]x=center_x+y_offset# 注意:这里x和y交换,因为是对称的y=center_y-x_offset last_x,last_y=vertices[-1]iflast_x!=xandlast_y!=y:vertices.append((last_x,y))vertices.append((x,y))# 第三象限:左下1/4圆弧(从底部到左侧)foriinrange(len(first_quadrant_points)-1,-1,-1):x_offset,y_offset=first_quadrant_points[i]x=center_x-x_offset y=center_y-y_offset last_x,last_y=vertices[-1]iflast_x!=xandlast_y!=y:vertices.append((last_x,y))vertices.append((x,y))# 第四象限:左上1/4圆弧(从左侧到顶部)foriinrange(len(first_quadrant_points)):x_offset,y_offset=first_quadrant_points[i]x=center_x-y_offset# 注意:这里x和y交换,因为是对称的y=center_y+x_offset last_x,last_y=vertices[-1]iflast_x!=xandlast_y!=y:vertices.append((last_x,y))vertices.append((x,y))# 闭合多边形(回到起点)vertices.append(vertices[0])# 对坐标进行单位长度的整数倍调整adjusted_vertices=[]forx,yinvertices:ifunit_length>=2:adj_x=round(x/unit_length)*unit_length adj_y=round(y/unit_length)*unit_lengthelse:adj_x=round(x)adj_y=round(y)adjusted_vertices.append((adj_x,adj_y))returnadjusted_verticesdefdraw_polygon_with_turtle(vertices,radius):"""使用turtle绘制多边形"""# 设置画布screen=turtle.Screen()screen.title("近似圆的多边形")screen.setup(width=800,height=800)# 计算顶点坐标的范围all_x=[xforx,_invertices]all_y=[yfor_,yinvertices]min_x,max_x=min(all_x),max(all_x)min_y,max_y=min(all_y),max(all_y)# 计算缩放比例,使图形占据画布的70%width=max_x-min_x height=max_y-min_y screen_width=800screen_height=800# 计算缩放比例scale_x=(screen_width*0.7)/max(width,1)scale_y=(screen_height*0.7)/max(height,1)scale=min(scale_x,scale_y)# 计算偏移量使图形居中offset_x=(screen_width-width*scale)/2-min_x*scale offset_y=(screen_height-height*scale)/2-min_y*scale# 创建画笔pen=turtle.Turtle()pen.speed(0)# 最快速度pen.width(2)# 移动到第一个点pen.penup()ifvertices:first_x,first_y=vertices[0]scaled_x=first_x*scale+offset_x-screen_width/2scaled_y=first_y*scale+offset_y-screen_height/2pen.goto(scaled_x,scaled_y)# 绘制多边形pen.pendown()pen.color("blue")forx,yinvertices:scaled_x=x*scale+offset_x-screen_width/2scaled_y=y*scale+offset_y-screen_height/2pen.goto(scaled_x,scaled_y)# 绘制参考圆pen.penup()center_x=(min_x+max_x)/2*scale+offset_x-screen_width/2center_y=(min_y+max_y)/2*scale+offset_y-screen_height/2pen.goto(center_x,center_y-radius*scale)# 圆的底部pen.pendown()pen.color("red")pen.circle(radius*scale)# 标记顶点(只标记部分顶点,避免太密集)pen.penup()pen.color("green")fori,(x,y)inenumerate(vertices):ifi%max(1,len(vertices)//20)==0:# 只标记约20个点scaled_x=x*scale+offset_x-screen_width/2scaled_y=y*scale+offset_y-screen_height/2pen.goto(scaled_x,scaled_y-10)pen.write(f"{i}",align="center",font=("Arial",8,"normal"))pen.hideturtle()screen.mainloop()# 测试函数deftest_circle_polygon():"""测试生成近似圆的多边形"""print("测试1: 半径10,单位长度1")radius1=10unit1=1vertices1=generate_polygon_circle(radius1,unit1)#draw_polygon_with_turtle(vertices1, radius1)print("顶点坐标:")fori,(x,y)inenumerate(vertices1):print(f"{x:.0f},{y:.0f}")print(f"\n共{len(vertices1)}个顶点")print("\n"+"="*50)print("测试2: 半径10,单位长度2")radius2=10unit2=2vertices2=generate_polygon_circle(radius2,unit2)#draw_polygon_with_turtle(vertices2, radius2)print("顶点坐标:")fori,(x,y)inenumerate(vertices2):print(f"{x:.0f},{y:.0f}")print(f"\n共{len(vertices2)}个顶点")print("\n"+"="*50)print("测试3: 半径21,单位长度3")radius3=21unit3=3vertices3=generate_polygon_circle(radius3,unit3)draw_polygon_with_turtle(vertices3,radius3)print("顶点坐标:")fori,(x,y)inenumerate(vertices3):print(f"{x:.0f},{y:.0f}")print(f"\n共{len(vertices3)}个顶点")# 使用turtle绘制验证#draw_option = input("\n是否使用turtle绘制验证图形?(y/n): ")#if draw_option.lower() == 'y':# draw_polygon_with_turtle(vertices1, radius1)# 主程序if__name__=="__main__":test_circle_polygon()
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/23 10:48:43

雷池 WAF vs React 高危漏洞:1 毫秒检测延迟,护住全栈业务安全

刚被 React 19/RSC 满分漏洞的预警刷屏&#xff1f;这次 CVSS 10.0 的高危漏洞&#xff0c;让 React 19.x、Next.js 14.3 等版本的业务瞬间暴露在‘单请求 RCE’的风险里&#xff0c;不少团队连夜紧急升级框架……在这个事件中&#xff0c;雷池 WAF 的社区官网&#xff0c;用的…

作者头像 李华
网站建设 2026/6/23 3:12:33

csp信奥赛C++标准模板库STL(3):list的使用详解

csp信奥赛C标准模板库STL&#xff08;3&#xff09;&#xff1a;list的使用详解 1. list基本概念 list是C标准模板库(STL)中的双向链表容器。与vector和deque不同&#xff0c;list不支持随机访问&#xff0c;但可以在任意位置快速插入和删除元素。 特点&#xff1a; 双向链表…

作者头像 李华
网站建设 2026/6/23 18:16:22

csp信奥赛C++标准模板库STL(2):deque的使用详解

csp信奥赛C标准模板库STL&#xff08;2&#xff09;&#xff1a;deque的使用详解 一、deque基本概念 1.1 什么是deque deque&#xff08;double-ended queue&#xff0c;双端队列&#xff09;是一种可以在两端进行高效插入和删除操作的序列容器结合了vector和list的优点&…

作者头像 李华
网站建设 2026/6/23 19:53:40

LobeChat部署在Docker中遇到的问题及解决办法总结

LobeChat 部署在 Docker 中的实战问题与深度解析 在构建 AI 聊天系统时&#xff0c;前端体验往往决定了用户是否愿意持续使用。即便底层模型再强大&#xff0c;一个卡顿、掉线或配置丢失的界面也会让用户迅速流失。LobeChat 作为近年来备受关注的开源聊天框架&#xff0c;凭借其…

作者头像 李华
网站建设 2026/6/23 19:46:06

AutoGPT在城市交通流量预测中的建模实验

AutoGPT在城市交通流量预测中的建模实验 在智慧城市的发展浪潮中&#xff0c;交通拥堵已成为制约城市运行效率的核心痛点。传统的交通流量预测系统往往依赖固定的建模流程&#xff1a;数据工程师手动采集数据、算法团队编写脚本清洗特征、模型训练后输出结果——整个过程耗时数…

作者头像 李华
网站建设 2026/6/23 19:52:52

AutoGPT镜像部署最佳实践:提升效率的关键一步

AutoGPT镜像部署最佳实践&#xff1a;提升效率的关键一步 在生成式AI迅猛发展的今天&#xff0c;我们正见证一个关键转折——大模型不再只是“回答问题的工具”&#xff0c;而是逐渐演变为能主动思考、规划并执行复杂任务的智能体。传统聊天机器人依赖用户逐条输入指令&#x…

作者头像 李华