自动化生成代码


import re
import copy

data = "  \
    M 5.32 6.32 \
    L 8.45 10.78 \
    C 10.3 10.5 12.65 12.35 15.11 15.79 \
    L 20.45 20.78 \
    Z "

offset_x = 5.32
offset_y = 6.32

def generateCode(cmd, data):
    print("===============================================================")
    for index,i in enumerate(data):
        ss = "/* " + cmd[index] + " */ "
        braced = True
        for j in i:
            if(braced):
                ss = ss + "{" + str(j) + "f, "
                braced = False
            else:
                ss = ss + str(j) + "f}, "
                braced = True
        print(ss)
        
    print(cmd)
    print("===============================================================")


def convert(s, offsetX, offsetY):
    cmds = []
    chars = ""
    chars_list = []
    started = False
    for i in s:
        if(i == '\n'):
            continue

        if(i == 'M' or i == 'L' or i == 'C' or i == 'Z'):
            if(started):
                chars_list.append(chars)
                chars = ""
            cmds.append(i)
            started = True
            continue

        if(started):
            chars += i

    chars_list = [i.split() for i in chars_list]

    for i in range(len(chars_list)):
        for j in range(len(chars_list[i])):
            if(j % 2 == 0):
                chars_list[i][j] = round(float(chars_list[i][j])- offsetX, 4)
            else:
                chars_list[i][j] = round(float(chars_list[i][j]) - offsetY, 4)

    generateCode(cmds, chars_list)


if __name__ == '__main__':
    convert(data, offset_x, offset_y)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值