剧情提要:
阿伟看到了一本比较有趣的书,是关于《计算几何》的,2008年由北清派出版。很好奇
它里面讲了些什么,就来看看啦。
正剧开始:
星历2016年10月21日 14:37:23, 银河系厄尔斯星球中华帝国江南行省。
阿伟看到了一本比较有趣的书,是关于《计算几何》的,2008年由北清派出版。很好奇
它里面讲了些什么,就来看看啦。
正剧开始:
星历2016年10月21日 14:37:23, 银河系厄尔斯星球中华帝国江南行省。
[工程师阿伟]正在和[机器小伟]一起研究[计算几何]]。
一、生成随机多边形
<span style="font-size:18px;">#
import math
from collections import namedtuple
from decimal import Decimal, getcontext
import re
from random import randint
# from subprocess import call
import os
getcontext().prec = 8
horizontal = Decimal('-Infinity')
Point = namedtuple('Point', 'x y')
DoublePoint = namedtuple('DoublePoint', 'x y')
#生成随机多边形
def RandomPoly(maxWidth, maxHeight, vertCnt):
result = []
for _ in range(vertCnt):
result.append(Point(randint(0, maxWidth), randint(0, maxHeight)))
return result
def tmp():
scale = 1;
a = RandomPoly(640 * scale, 480 * scale, 10);
print('a = ', a);
b = RandomPoly(640 * scale, 480 * scale, 16);
print('b = ', b);
if __name__ == '__main__':
tmp();
>>>
a = [Point(x=376, y=223), Point(x=471, y=386), Point(x=110, y=381), Point(x=206, y=85), Point(x=190, y=41), Point(x=547, y=249), Point(x=501, y=75), Point(x=501, y=183), Point(x=497, y=240), Point(x=528, y=83)]
b = [Point(x=432, y=428), Point(x=558, y=145), Point(x=64, y=52), Point(x=381, y=467), Point(x=460, y=321), Point(x=614, y=318), Point(x=173, y=80), Point(x=301, y=399), Point(x=431, y=129), Point(x=82, y=79), Point(x=449, y=71), Point(x=300, y=29), Point(x=558, y=225), Point(x=5, y=87), Point(x=613, y=327), Point(x=342, y=353)]
>>>
#</span>
二、有关数据的文件操作
<span style="font-size:18px;">#
#读取/保存多边形数据到文件
def LoadFile1(lines):
# File type 1: first line is total polygons count and subsequent lines
# contain the polygon vertex count followed by its coords
try:
polygons = []
poly = []
for l in lines:
vals = re.split(' |, |,', l.strip())
if len(vals) < 2:
if (len(poly) > 2):
polygons.append(poly)
poly = []
else:
poly.append(Point(int(vals[0]), int(vals[1])))
if (len(poly) > 2):
polygons.append(poly)
return polygons
except:
return None
def LoadFile2(lines):
# File type 2: vertex coords on consecutive lines for each polygon
# where each polygon is separated by an empty line
try:
polygons = []
poly = []
for l in lines:
l = l.strip()
if (l == ''):
if (len(poly) > 2):
polygons.append(poly)
poly = []
else:
vals = re.split(' |, |,', l)
poly.append(Point(int(vals[0]), int(vals[1])))
if (len(poly) > 2):
polygons.append(poly)
return polygons
except:
return None
def LoadFile(filename):
try:
f = open(filename, 'r')
try:
lines = f.readlines()
finally:
f.close()
# pick file type from format of first line ...
if len(lines) == 0: return []
elif not ',' in lines[0]: return LoadFile1(lines)
else: return LoadFile2(lines)
except:
return None
def SaveToFile(filename, polys, scale = 1.0):
invScale = 1.0 / scale
try:
f = open(filename, 'w')
try:
if invScale == 1:
for poly in polys:
for pt in poly:
f.write("{0}, {1}\n".format(pt.x, pt.y))
f.write("\n")
else:
for poly in polys:
for pt in poly:
f.write("{0:.4f}, {1:.4f}\n".format(pt.x * invScale, pt.y * invScale))
f.write("\n")
finally:
f.close()
except:
return
#测试多边形数据生成
def tmp_0():
scale = 1;
a = RandomPoly(640 * scale, 480 * scale, 10);
print('a = ', a);
b = RandomPoly(640 * scale, 480 * scale, 16);
print('b = ', b);
#测试多边形数据写入文件
def tmp_1():
scaleExp = 0
scale = math.pow(10, scaleExp)
invScale = 1.0 / scale
subj, clip = [], []
subj.append(RandomPoly(640 * scale, 480 * scale, 10))
clip.append(RandomPoly(640 * scale, 480 * scale, 16))
SaveTo