资源简介
基于python的多边形几何操作的代码
实现任意多边形(凹、凸、带内环)的输入及显示 ,可自定义多边形(边界、内部)的颜色; 实现两个任意多边形的裁剪; 实现任意多边形的平移、旋转、缩放、翻转; 支持多步以上操作;
代码片段和文件信息
import numpy as np
import cv2
# ============================================================================
CANVAS_SIZE = (600800)
CANVAS_COLOR = (0 0 0)
FINAL_LINE_COLOR = (255 255 255)
FINAL_POLY_COLOR = (50 149 216)
WORKING_LINE_COLOR = (127 127 127)
# ============================================================================
class PolygonDrawer(object):
def __init__(self window_name):
self.window_name = window_name # Name for our window
self.done = False # Flag signalling we‘re done
self.innerdone = False #Flag signalling inner done
self.cutdone = False # Flag signalling we‘re done
self.cutinnerdone = False # Flag signalling we‘re done
self.transform = False # Flag transformation is done
self.current = (0 0) # Current position so we can draw the line-in-progress
self.cutcurrent = (0 0) # Current position so we can draw the line-in-progress
self.points = [] # List of points defining our polygon
self.innerpoints = [] #List of inner points defining out polygon
self.cutpoints = [] #List of points defining our cut-tube
self.cutinnerpoints = [] #List of points definig our cutinner-tube
def on_mouse(self event x y buttons user_param):
# Mouse callback that gets called for every mouse event (i.e. moving clicking etc.)
if self.done: # Nothing more to do
return
if event == cv2.EVENT_MOUSEMOVE:
# We want to be able to draw the line-in-progress so update current mouse position
self.current = (x y)
elif event == cv2.EVENT_LBUTTONDOWN:
# Left click means adding a point at current position to the list of points
print(“Adding point #%d with position(%d%d)“ % (len(self.points) x y))
self.points.append((x y))
elif event == cv2.EVENT_RBUTTONDOWN:
# Right click means we‘re done
print(“Completing polygon with %d points.“ % len(self.points))
self.done = True
def on_innermouse(self event x y buttons user_param):
# Mouse callback that gets called for every mouse event (i.e. moving clicking etc.)
if self.innerdone: # Nothing more to do
return
if event == cv2.EVENT_MOUSEMOVE:
# We want to be able to draw the line-in-progress so update current mouse position
self.current = (x y)
elif event == cv2.EVENT_LBUTTONDOWN:
# Left click means adding a point at current position to the list of points
print(“Adding inner point #%d with position(%d%d)“ % (len(self.innerpoints) x y))
self.innerpoints.append((x y))
elif event == cv2.EVENT_RBUTTONDOWN:
# Right click means we‘re done
print(“Completing inner polygon with %d points.“ % len(self.innerpoints))
self.innerdone = True
def on_secmouse(sel
评论
共有 条评论