Marching squares (triangle)


2*2网格划分为三角网

def get_triangle_bit(v1, v2, v3):
	return v1 << 2 | v2 << 1 | v3

def get_triangle_shift(bitval):
	if bitval == 0 or bitval == 7:
		return (0,None,None,None,None)
	if bitval == 1 or bitval == 6:
		return (1,-0.5,0.5,0,1)
	if bitval == 2 or bitval == 5:
		return (1,0,1,0.5,0.5)
	if bitval == 3 or bitval == 4:
		return (1,-0.5,0.5,0.5,0.5)

class MarchSquareTriangleUtlis(MarchSquareUtlis):
	
	def trancing_contours(self):
		ret = []
		width,height = self.net.net_info.shape
		arr = self.net.net_info
		for i in range(width-1):
			for j in range(height-1):
				v1 = int(arr[i][j])
				v2 = int(arr[i + 1][j])
				v3 = int(arr[i + 1][j + 1])
				v4 = int(arr[i][j + 1])
				bitv = get_triangle_bit(v1,v2,v4)
				#net_shift = get_triangle_shift(bitv)
				#ret.append(net_shift)
				ret.append(bitv)
				#again
				bitv = get_triangle_bit(v2,v3,v4)
				#net_shift = get_triangle_shift(bitv)
				#ret.append(net_shift)
				ret.append(bitv)
		return ret


class PlotTriangleDemo(PlotDemo):
	def show_contour(self):
		self._set_default_figure(self.m, self.n)
		net = self._net
		utils = MarchSquareTriangleUtlis(net)
		lines = utils.trancing_contours()

		width,height = net.net_info.shape
		arr = net.net_info
		idx = 0
		for i in range(width-1):
			for j in range(height-1):
				x,y = i,j
				bitval = lines[idx]
				idx = idx + 1
				self._plot_by_bitval_left(bitval,x,y)
				bitval = lines[idx]
				idx = idx + 1
				self._plot_by_bitval_right(bitval,x+1,y)
		show()

	def _plot_by_bitval_left(self, bitval, topleftx, toplefty):
		if bitval == 0 or bitval == 7:
			pass
		if bitval == 1 or bitval == 6:
			x1 = topleftx + 0
			y1 = toplefty + 0.5
			x2 = topleftx + 0.5
			y2 = toplefty + 0.5
			plot(x1,y1,x2,y2,'ro')
		if bitval == 2 or bitval == 5:
			x1 = topleftx + 0.5
			y1 = toplefty + 0
			x2 = topleftx + 0.5
			y2 = toplefty + 0.5
			plot(x1,y1,x2,y2,'ro')
		if bitval == 3 or bitval == 4:
			x1 = topleftx + 0
			y1 = toplefty + 0.5
			x2 = topleftx + 0.5
			y2 = toplefty + 0.5
			plot(x1,y1,x2,y2,'ro')

	def _plot_by_bitval_right(self, bitval, topleftx, toplefty):
		if bitval == 0 or bitval == 7:
			pass
		if bitval == 1 or bitval == 6:
			x1 = topleftx - 0.5
			y1 = toplefty + 0.5
			x2 = topleftx + 0.5
			y2 = toplefty + 1
			plot(x1,y1,x2,y2,'ro')
		if bitval == 2 or bitval == 5:
			x1 = topleftx - 0.5
			y1 = toplefty + 1
			x2 = topleftx + 0
			y2 = toplefty + 0.5
			plot(x1,y1,x2,y2,'ro')
		if bitval == 3 or bitval == 4:
			x1 = topleftx - 0.5
			y1 = toplefty + 0.5
			x2 = topleftx + 0
			y2 = toplefty + 0.5
			plot(x1,y1,x2,y2,'ro')


demo = PlotTriangleDemo(100,100)
netinfo = RandomGenNet(100,100)
netinfo.add_circle(20,20,10,1)
netinfo.add_circle(70,50,20,1)
netinfo.add_retangle(20,45,40,20,1)
netinfo.add_retangle(70,50,10,10,1)


demo.set_net_info(netinfo)
demo.show_contour()


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值