金融網(wǎng)站開發(fā)文檔下載網(wǎng)站開發(fā) 微信收款
鶴壁市浩天電氣有限公司
2026/01/24 10:35:33
金融網(wǎng)站開發(fā)文檔下載,網(wǎng)站開發(fā) 微信收款,天津建設(shè)局網(wǎng)站,flash網(wǎng)站與html5為了測試多邊形之間的包含關(guān)系#xff0c;實(shí)現(xiàn)了用戶設(shè)置圓半徑和單位長度#xff0c;程序自動確定圓心位置。
import math
import turtledef generate_polygon_circle(radius, unit_length):生成近似圓的多邊形輪廓頂點(diǎn)坐標(biāo)參數(shù):radius: 半徑unit_length:…為了測試多邊形之間的包含關(guān)系實(shí)現(xiàn)了用戶設(shè)置圓半徑和單位長度程序自動確定圓心位置。importmathimportturtledefgenerate_polygon_circle(radius,unit_length): 生成近似圓的多邊形輪廓頂點(diǎn)坐標(biāo) 參數(shù): radius: 半徑 unit_length: 單位長度多邊形的步長 返回: list: 多邊形頂點(diǎn)坐標(biāo)列表按順時(shí)針順序 ifradius0orunit_length0:return[]vertices[]# 圓心設(shè)為(r, r)以便所有坐標(biāo)為正center_xradius center_yradius# 計(jì)算總步數(shù)每90度圓弧的步數(shù)steps_per_quadrantmax(1,int(radius/unit_length))# 存儲第一象限的點(diǎn)不含起點(diǎn)first_quadrant_points[]# 計(jì)算第一象限的點(diǎn)從(0, r)開始順時(shí)針到(r, 0)foriinrange(steps_per_quadrant1):xi*unit_lengthifxradius:# 防止x大于半徑xradius y_sqmax(0,radius**2-x**2)ymath.sqrt(y_sq)first_quadrant_points.append((x,y))# 起點(diǎn)正上方 (r, 2r)start_xcenter_x start_ycenter_yradius vertices.append((start_x,start_y))# 第一象限右上1/4圓弧從頂部到右側(cè)# 從第一個(gè)點(diǎn)接近頂部開始foriinrange(len(first_quadrant_points)-1,-1,-1):x_offset,y_offsetfirst_quadrant_points[i]xcenter_xx_offset ycenter_yy_offsetifvertices:last_x,last_yvertices[-1]# 插入直角頂點(diǎn)iflast_x!xandlast_y!y:vertices.append((last_x,y))vertices.append((x,y))# 第二象限右下1/4圓弧從右側(cè)到底部foriinrange(len(first_quadrant_points)):x_offset,y_offsetfirst_quadrant_points[i]xcenter_xy_offset# 注意這里x和y交換因?yàn)槭菍ΨQ的ycenter_y-x_offset last_x,last_yvertices[-1]iflast_x!xandlast_y!y:vertices.append((last_x,y))vertices.append((x,y))# 第三象限左下1/4圓弧從底部到左側(cè)foriinrange(len(first_quadrant_points)-1,-1,-1):x_offset,y_offsetfirst_quadrant_points[i]xcenter_x-x_offset ycenter_y-y_offset last_x,last_yvertices[-1]iflast_x!xandlast_y!y:vertices.append((last_x,y))vertices.append((x,y))# 第四象限左上1/4圓弧從左側(cè)到頂部foriinrange(len(first_quadrant_points)):x_offset,y_offsetfirst_quadrant_points[i]xcenter_x-y_offset# 注意這里x和y交換因?yàn)槭菍ΨQ的ycenter_yx_offset last_x,last_yvertices[-1]iflast_x!xandlast_y!y:vertices.append((last_x,y))vertices.append((x,y))# 閉合多邊形回到起點(diǎn)vertices.append(vertices[0])# 對坐標(biāo)進(jìn)行單位長度的整數(shù)倍調(diào)整adjusted_vertices[]forx,yinvertices:ifunit_length2:adj_xround(x/unit_length)*unit_length adj_yround(y/unit_length)*unit_lengthelse:adj_xround(x)adj_yround(y)adjusted_vertices.append((adj_x,adj_y))returnadjusted_verticesdefdraw_polygon_with_turtle(vertices,radius):使用turtle繪制多邊形# 設(shè)置畫布screenturtle.Screen()screen.title(近似圓的多邊形)screen.setup(width800,height800)# 計(jì)算頂點(diǎn)坐標(biāo)的范圍all_x[xforx,_invertices]all_y[yfor_,yinvertices]min_x,max_xmin(all_x),max(all_x)min_y,max_ymin(all_y),max(all_y)# 計(jì)算縮放比例使圖形占據(jù)畫布的70%widthmax_x-min_x heightmax_y-min_y screen_width800screen_height800# 計(jì)算縮放比例scale_x(screen_width*0.7)/max(width,1)scale_y(screen_height*0.7)/max(height,1)scalemin(scale_x,scale_y)# 計(jì)算偏移量使圖形居中offset_x(screen_width-width*scale)/2-min_x*scale offset_y(screen_height-height*scale)/2-min_y*scale# 創(chuàng)建畫筆penturtle.Turtle()pen.speed(0)# 最快速度pen.width(2)# 移動到第一個(gè)點(diǎn)pen.penup()ifvertices:first_x,first_yvertices[0]scaled_xfirst_x*scaleoffset_x-screen_width/2scaled_yfirst_y*scaleoffset_y-screen_height/2pen.goto(scaled_x,scaled_y)# 繪制多邊形pen.pendown()pen.color(blue)forx,yinvertices:scaled_xx*scaleoffset_x-screen_width/2scaled_yy*scaleoffset_y-screen_height/2pen.goto(scaled_x,scaled_y)# 繪制參考圓pen.penup()center_x(min_xmax_x)/2*scaleoffset_x-screen_width/2center_y(min_ymax_y)/2*scaleoffset_y-screen_height/2pen.goto(center_x,center_y-radius*scale)# 圓的底部pen.pendown()pen.color(red)pen.circle(radius*scale)# 標(biāo)記頂點(diǎn)只標(biāo)記部分頂點(diǎn)避免太密集pen.penup()pen.color(green)fori,(x,y)inenumerate(vertices):ifi%max(1,len(vertices)//20)0:# 只標(biāo)記約20個(gè)點(diǎn)scaled_xx*scaleoffset_x-screen_width/2scaled_yy*scaleoffset_y-screen_height/2pen.goto(scaled_x,scaled_y-10)pen.write(f{i},aligncenter,font(Arial,8,normal))pen.hideturtle()screen.mainloop()# 測試函數(shù)deftest_circle_polygon():測試生成近似圓的多邊形print(測試1: 半徑10單位長度1)radius110unit11vertices1generate_polygon_circle(radius1,unit1)#draw_polygon_with_turtle(vertices1, radius1)print(頂點(diǎn)坐標(biāo):)fori,(x,y)inenumerate(vertices1):print(f{x:.0f},{y:.0f})print(f
共{len(vertices1)}個(gè)頂點(diǎn))print(
*50)print(測試2: 半徑10單位長度2)radius210unit22vertices2generate_polygon_circle(radius2,unit2)#draw_polygon_with_turtle(vertices2, radius2)print(頂點(diǎn)坐標(biāo):)fori,(x,y)inenumerate(vertices2):print(f{x:.0f},{y:.0f})print(f
共{len(vertices2)}個(gè)頂點(diǎn))print(
*50)print(測試3: 半徑21單位長度3)radius321unit33vertices3generate_polygon_circle(radius3,unit3)draw_polygon_with_turtle(vertices3,radius3)print(頂點(diǎn)坐標(biāo):)fori,(x,y)inenumerate(vertices3):print(f{x:.0f},{y:.0f})print(f
共{len(vertices3)}個(gè)頂點(diǎn))# 使用turtle繪制驗(yàn)證#draw_option input(
是否使用turtle繪制驗(yàn)證圖形(y/n): )#if draw_option.lower() y:# draw_polygon_with_turtle(vertices1, radius1)# 主程序if__name____main__:test_circle_polygon()