多边形分解以及数据计算

对下图所示多边形进行分解:我们输入包含0、1的矩阵,其中1表示线条0表示空白。将图案从外到里进行拆解,分解计算拆解图案的长度、面积以及是否旋转对称。其中我们考虑这样的输入是错误的:1、边长大于502、出现除了1和0以外的字符3、拆解之后剩下单个的1无法组成多边形4、存在不连续的线条而输入可以包含无限多的空格与空行,则需要在最开始进行处理。上图对应的输入为:0 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 .
摘要由CSDN通过智能技术生成

对下图所示多边形进行分解:

 我们输入包含0、1的矩阵,其中1表示线条0表示空白。将图案从外到里进行拆解,分解计算拆解图案的长度、面积以及是否旋转对称。

其中我们考虑这样的输入是错误的:

1、边长大于50

2、出现除了1和0以外的字符

3、拆解之后剩下单个的1无法组成多边形

4、存在不连续的线条

而输入可以包含无限多的空格与空行,则需要在最开始进行处理。

上图对应的输入为:

0 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 0

1 0 1 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 1 0 1

0 1 0 0 1 0 0 0 0 0 1 0 0 1 1 0 0 0 1 0 0 1 0 0 0 1 1 0 0 1 0 0 0 0 0 1 0 0 1 0

0 1 0 0 0 1 0 0 0 0 1 0 0 1 1 0 0 1 0 0 0 0 1 0 0 1 1 0 0 1 0 0 0 0 1 0 0 0 1 0

0 0 1 0 0 1 0 0 0 0 1 0 0 1 1 0 1 0 0 0 0 0 0 1 0 1 1 0 0 1 0 0 0 0 1 0 0 1 0 0

0 0 1 0 0 1 0 0 0 0 1 0 0 1 0 1 0 0 1 1 1 1 0 0 1 0 1 0 0 1 0 0 0 0 1 0 0 1 0 0

0 0 1 0 1 0 0 0 0 0 1 0 0 0 1 0 0 1 0 0 0 0 1 0 0 1 0 0 0 1 0 0 0 0 0 1 0 1 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1 1 1 1 1 1 1 1 1 1

0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 1 0 0 1 1 1 1 0 0 1 1 0 0 1 1 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 1 0 1 0 1 1 0 0 0 0 0 0 0 0 1 1 0 1 0 1 0 0 0 0 0 0 0 0 0 0

0 0 0 1 0 0 0 0 0 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 1 0 1 0 0 1 0 0 0 0 0 0 1 0 0 0

0 0 1 0 1 0 0 0 0 0 1 0 0 0 1 0 1 1 1 1 1 1 1 1 0 1 0 0 0 1 0 0 0 0 0 1 0 1 0 0

0 0 1 0 0 1 0 0 0 0 1 0 0 1 0 1 0 0 0 0 0 0 0 0 1 0 1 0 0 1 0 0 0 0 1 0 0 1 0 0

0 0 1 0 0 1 0 0 0 0 1 0 0 1 1 0 1 0 0 0 0 0 0 1 0 1 1 0 0 1 0 0 0 0 1 0 0 1 0 0

0 1 0 0 0 1 0 0 0 0 1 0 0 1 1 0 0 1 0 0 0 0 1 0 0 1 1 0 0 1 0 0 0 0 1 0 0 0 1 0

0 1 0 0 1 0 0 0 0 0 1 0 0 1 1 0 0 0 1 0 0 1 0 0 0 1 1 0 0 1 0 0 0 0 0 1 0 0 1 0

1 0 1 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 1 0 1

0 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 0

我们需要得到的分析为:

>>> polys = Polygons('polys_3.txt')
>>> polys.analyse()
Polygon 1:
Perimeter: 2.4 + 9*sqrt(.32)
Area: 2.80
Convex: no
Nb of invariant rotations: 1
Depth: 0
Polygon 2:
Perimeter: 51.2 + 4*sqrt(.32)
Area: 117.28
Convex: no
Nb of invariant rotations: 2
Depth: 0
Polygon 3:
Perimeter: 2.4 + 9*sqrt(.32)
Area: 2.80
Convex: no
Nb of invariant rotations: 1
Depth: 0
Polygon 4:
Perimeter: 17.6 + 40*sqrt(.32)
Area: 59.04
Convex: no
Nb of invariant rotations: 2
Depth: 1
Polygon 5:
Perimeter: 3.2 + 28*sqrt(.32)
Area: 9.76
Convex: no
Nb of invariant rotations: 1
Depth: 2
Polygon 6:
Perimeter: 27.2 + 6*sqrt(.32)
Area: 5.76
Convex: no
Nb of invariant rotations: 1
Depth: 2
Polygon 7:
Perimeter: 4.8 + 14*sqrt(.32)
Area: 6.72
Convex: no
Nb of invariant rotations: 1
Depth: 1
Polygon 8:
Perimeter: 4.8 + 14*sqrt(.32)
Area: 6.72
Convex: no
Nb of invariant rotations: 1
16
Depth: 1
Polygon 9:
Perimeter: 3.2 + 2*sqrt(.32)
Area: 1.12
Convex: yes
Nb of invariant rotations: 1
Depth: 2
Polygon 10:
Perimeter: 3.2 + 2*sqrt(.32)
Area: 1.12
Convex: yes
Nb of invariant rotations: 1
Depth: 2
Polygon 11:
Perimeter: 2.4 + 9*sqrt(.32)
Area: 2.80
Convex: no
Nb of invariant rotations: 1
Depth: 0
Polygon 12:
Perimeter: 2.4 + 9*sqrt(.32)
Area: 2.80
Convex: no
Nb of invariant rotations: 1
Depth: 

以下为实现代码:

class PolygonsError(Exception):
    pass
class Polygons:
    
    def __init__(self,file):
        self.file=file
        poly=open(file)
        temp=[]
        for i in poly.readlines():
            temp.append(i.strip())
        while '' in temp:
            temp.remove('')
        LLL=[]
        LL=[]
        L=[]
        for i in temp:
            LLL.append(i.replace(' ',''))
        for i in LLL:
            LL.append(i.replace('0',' '))
        for i in LL:
            L.append(i.replace('1','A'))

        for i in L:
            for j in i:
                if j!=' ' and j!='A':
                    raise PolygonsError('Incorrect input')
        n=len(L)
        m=len(L[0])
        if m>50 or n>50:
            raise PolygonsError('Incorrect input')
        if m<2 or n<2:
            raise PolygonsError('Incorrect input')
                
        n=len(L)
        m=len(L[0])
        result_combine=[]
        depth=[]
        result=[]
        perimeter=[]
        area=[]
        convex=[]
        rotations=[]
        

        def find_point(result):#找到指向
            nonlocal L
            a=result[-2]
            b=result[-1]
            if a[0]==b[0] and a[1]==b[1]-1:
                return 1
            if a[0]==b[0]-1 and a[1]==b[1]-1:
                return 2
            if a[0]==b[0]-1 and a[1]==b[1]:
                return 3
            if a[0]==b[0]-1 and a[1]==b[1]+1:
                return 4
            if a[0]==b[0] and a[1]==b[1]+1:
                return 5
            if a[0]==b[0]+1 and a[1]==b[1]+1:
                return 6
            if a[0]==b[0]+1 and a[1]==b[1]:
                return 7
            if a[0]==b[0]+1 and a[1]==b[1]-1:
                return 8
        
        def next_position(result,position,point):#找到下一个点,否则返回(-1,-1)
            nonlocal L
            nonlocal m
            nonlocal n
            answer=(-1,-1)
            flag=0
            if point==0:
                if position[1]+1<m:
                    if L[position[0]][position[1]+1]!=' ':
                        answer= (position[0],position[1]+1)
                        flag=1
                if position[1]+1<m and position[0]+1<n and flag==0:
                    if L[position[0]+1][position[1]+1
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值