當前位置: 首頁>>代碼示例>>Python>>正文


Python Math.intersect_linesegs_ray方法代碼示例

本文整理匯總了Python中Math.intersect_linesegs_ray方法的典型用法代碼示例。如果您正苦於以下問題:Python Math.intersect_linesegs_ray方法的具體用法?Python Math.intersect_linesegs_ray怎麽用?Python Math.intersect_linesegs_ray使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Math的用法示例。


在下文中一共展示了Math.intersect_linesegs_ray方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: calculate

# 需要導入模塊: import Math [as 別名]
# 或者: from Math import intersect_linesegs_ray [as 別名]
	def calculate(self, eye, radius, boundary):
		"""Re-calculate the vision polygon.

		WARNING: You should only call this if you want to re-calculate the vision polygon for some reason. 
		
		For normal usage, use L{get_vision} instead!
		"""

		self.cached_radius = radius
		self.cached_position = eye
		self.debug_points = []
		self.debug_linesegs = []

		radius_squared = radius * radius

		
		closest_points = lambda points, reference: sorted(points, key=lambda p: (p - reference).get_length_squared())


		def sub_segment(small, big):
			return Math.distance_point_lineseg_squared(small[0], big[0], big[1]) < 0.0001 and Math.distance_point_lineseg_squared(small[1], big[0], big[1]) < 0.0001
				

		def segment_in_obs(seg):
			for line_segment in self.obs_segs:
				if sub_segment(seg, line_segment):
					return True
			return False

		def check_visibility(p):
			bpoints = set(boundary.points)

			if p not in bpoints:
				if (eye - p).get_length_squared() > radius_squared: return False
				if not boundary.contains_point(p): return False 
			
			for line_segment in obs_segs:
				if Math.check_intersect_lineseg_lineseg( eye, p, line_segment[0], line_segment[1]): 
					if line_segment[0] != p and line_segment[1] != p:
						return False

			return True

		def lineseg_in_radius(seg):
			return Math.distance_point_lineseg_squared(eye, seg[0], seg[1]) <= radius_squared

		obs_segs = filter(lineseg_in_radius, self.obs_segs)

		# add all obstruction points and boundary points directly visible from the eye
		visible_points = list(filter(check_visibility, set(self.obs_points + boundary.points )))

		# find all obstructors intersecting the vision polygon
		boundary_intersection_points = Math.intersect_linesegs_linesegs(obs_segs, zip(boundary.points, boundary.points[1:]) + [(boundary.points[-1], boundary.points[0])])
		
		if self.debug: self.debug_points.extend([(p, 0xFF0000) for p in visible_points])
		if self.debug: self.debug_points.extend([(p, 0x00FFFF) for p in boundary_intersection_points])

		# filter boundary_intersection_points to only include visible points 
		# - need extra code here to handle points on obstructors!
		for line_segment in obs_segs:		
			i = 0
			while i < len(boundary_intersection_points):
				p = boundary_intersection_points[i]
				
				if Math.distance_point_lineseg_squared(p, line_segment[0], line_segment[1]) > 0.0001 and Math.check_intersect_lineseg_lineseg(eye, p, line_segment[0], line_segment[1]):
					boundary_intersection_points.remove(p)
				else:
					i+=1

		visible_points += boundary_intersection_points

		poly = Math.Polygon()
		poly.add_points(visible_points)
		poly.sort_around(eye)

		i = 0
		while i < len(poly.points):
			p = poly.points[i-1]
			c = poly.points[i]
			n = poly.points[ (i+1) % len(poly.points) ]

			# intersect visible point with obstructors and boundary polygon
			intersections = set(Math.intersect_linesegs_ray(obs_segs, eye, c) + Math.intersect_poly_ray(boundary.points, eye, c))

			intersections = [ip for ip in intersections if ip != c and boundary.contains_point(ip)]


			if self.debug: self.debug_points.extend([(pt, 0x00FF00) for pt in intersections])
			if intersections:

				intersection = min(intersections, key=lambda p: (p - eye).length_squared)

				#if self.debug: self.debug_linesegs.append((0xFF00FF, [eye, intersection]))

				#if self.debug: print "%d prev: %s current: %s next: %s" % (i, p, c, n)

				sio_pc = segment_in_obs((p,c))
				sio_cn = segment_in_obs((c,n))

				if not sio_pc:
#.........這裏部分代碼省略.........
開發者ID:tartley,項目名稱:Py2D,代碼行數:103,代碼來源:FOV.py


注:本文中的Math.intersect_linesegs_ray方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。