.txt坐标保存格式:分行保存,每行为一点的(x,y)坐标,每四行(四对坐标)构成一个矩形框的四个点。
rec=[]
r=[]
with open(path,'r') as file:
i=0
for line in file:
s=line.split()
x=float(s[0])
y=float(s[1])
r.append([x,y])
i+=1
if i==4:
rec.append(r)
r=[]
i=0
使用matplotlib的函数Rectangle去画矩形框,先要将数据读取成坐标元组的列表,参考代码:
data=f.readlines()
pos=[]
for i in range(0,len(data),2):
a=data[i:i+2]
b=tuple()
for x in a:
b+=tuple([float(m) for m in x.strip('\n').split()])
pos.append(b)
max_x=int(max([max(x[0],x[2]) for x in pos]))
max_y=int(max([max(x[1],x[3]) for x in pos]))
min_x=int(min([min(x[0],x[2]) for x in pos]))
min_y=int(min([min(x[1],x[3]) for x in pos]))
for i in range(len(pos)):
plt.gca().add_patch(plt.Rectangle(xy=(pos[i][0], pos[i][1]),
width=pos[i][2]-pos[i][0],
height=pos[i][3] -pos[i][1],
fill=False, linewidth=2,color='red'))
plt.xlim((min_x-10,max_x+10))
plt.ylim((min_y-10,max_y+10))
plt.show()
如有帮助,请点采纳。
矩形四个点,以对角线为圆心,对角线的一半为半径,移动轨迹是一个圆型,连线规律,按照距离x轴距离,从小到大排列坐标,
import turtle
filepath="aa.txt"
def make_points(filepath,maxlen=4):
points=[]
with open(filepath,"r") as r:
for i in r:
x,y=i.strip().split(" ")
points.append((float(x),float(y)))
if len(points)==maxlen:
#转换坐标数值,并按照x坐标从小到大排序,并返回
yield sorted(points)
points.clear()
def draw_rect(points,startx=150,starty=150):
#设置左下角坐标(0,0),右上角坐标(500,500)
turtle.setworldcoordinates(startx, starty, 500, 500)
turtle.penup()
turtle.goto(*points[0])
turtle.pendown()
turtle.goto(*points[1])
turtle.goto(*points[3])
turtle.goto(*points[2])
turtle.goto(*points[0])
#turtle.done()
if __name__=="__main__":
for i in make_points(filepath):
draw_rect(i)