-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add bbox auto zoom center and vertex moves independently #86
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -322,6 +322,7 @@ def keysInfo(lang="en"): | |
"Ctrl++\t\t\t缩小\n" | ||
"Ctrl--\t\t\t放大\n" | ||
"↑→↓←\t\t\t移动标记框\n" | ||
"Z、X、C、V、B\t\t\t对选中的标记框,单独移动四个顶点\n" | ||
"———————————————————————\n" | ||
"注:Mac用户Command键替换上述Ctrl键" | ||
) | ||
|
@@ -351,8 +352,60 @@ def keysInfo(lang="en"): | |
"Ctrl++\t\t\tZoom in\n" | ||
"Ctrl--\t\t\tZoom out\n" | ||
"↑→↓←\t\t\tMove selected box" | ||
"Z, X, C, V, B\t\tMove the four vertices of \n" | ||
and "\t\t\tthe selected bounding box individually" | ||
"———————————————————————\n" | ||
"Notice:For Mac users, use the 'Command' key instead of the 'Ctrl' key" | ||
) | ||
|
||
return msg | ||
|
||
|
||
def polygon_bounding_box_center_and_area(points): | ||
""" | ||
计算多边形外接矩形的中心和面积 | ||
""" | ||
area = 0 | ||
min_x = float("inf") | ||
max_x = float("-inf") | ||
min_y = float("inf") | ||
max_y = float("-inf") | ||
|
||
n = len(points) | ||
# 计算多边形的面积和质心坐标 | ||
for i in range(n): | ||
x1 = points[i].x() | ||
y1 = points[i].y() | ||
x2 = points[(i + 1) % n].x() | ||
y2 = points[(i + 1) % n].y() | ||
area += x1 * y2 - x2 * y1 | ||
min_x = min(min_x, x1, x2) | ||
max_x = max(max_x, x1, x2) | ||
min_y = min(min_y, y1, y2) | ||
max_y = max(max_y, y1, y2) | ||
Comment on lines
+368
to
+385
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这段逻辑太复杂了,如果只是计算外接矩形,下面这段代码是不是也可以 for point in points:
x = point.x()
y = point.y()
min_x = min(min_x, x)
max_x = max(max_x, x)
min_y = min(min_y, y)
max_y = max(max_y, y) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 还需要计算面积,外接矩形应该改成你这种写法 |
||
|
||
# 计算面积 | ||
area = abs(area) / 2.0 | ||
|
||
# 计算外接矩形的中心 | ||
center_x = (min_x + max_x) / 2 | ||
center_y = (min_y + max_y) / 2 | ||
|
||
return center_x, center_y, area | ||
|
||
|
||
def map_value(x, in_min, in_max, out_min, out_max): | ||
""" | ||
将数值x从[in_min, in_max]的范围映射到[out_min, out_max]的范围 | ||
|
||
参数: | ||
x -- 要映射的数值 | ||
in_min -- 原始范围的最小值 | ||
in_max -- 原始范围的最大值 | ||
out_min -- 新范围的最小值 | ||
out_max -- 新范围的最大值 | ||
|
||
返回: | ||
映射后的数值 | ||
""" | ||
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 当in_max - in_min为零时,程序将抛出ZeroDivisionError。应在函数中添加检查,避免这种情况: if in_max == in_min:
raise ValueError("in_max and in_min cannot be equal") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
min_x, max_x, min_y, max_y初始值为正无穷和负无穷,如果points为空,这些值将保持不变,导致计算的中心坐标为nan。
建议开头添加检查