python中根据中心点和边长获得四个角点的函数
时间: 2023-11-27 13:55:06 浏览: 35
可以使用以下代码实现:
```python
def get_corners(center, length):
"""
根据中心点和边长获取四个角点的坐标
:param center: 中心点坐标,格式为 (x,y)
:param length: 正方形边长
:return: 四个角点的坐标,格式为 [(x1,y1),(x2,y2),(x3,y3),(x4,y4)]
"""
x, y = center
half_length = length / 2
x1 = x - half_length
y1 = y - half_length
x2 = x + half_length
y2 = y - half_length
x3 = x + half_length
y3 = y + half_length
x4 = x - half_length
y4 = y + half_length
return [(x1, y1), (x2, y2), (x3, y3), (x4, y4)]
```
其中,`center` 参数是中心点的坐标,`length` 参数是正方形的边长。函数返回一个包含四个角点坐标的列表。
相关问题
opencv如何中心点和边长获得矩形轮廓点的坐标
使用OpenCV中的函数 `cv2.boxPoints()` 可以从矩形的中心点和边长计算出矩形的四个角点的坐标。
下面是一个示例代码,展示了如何从矩形的中心点和边长计算出矩形的四个角点的坐标:
```python
import cv2
# 矩形中心点坐标
center = (100, 100)
# 矩形边长
width = 50
height = 80
# 矩形旋转角度(可选)
angle = 30
# 创建旋转矩阵(如果需要旋转的话)
rotation_matrix = cv2.getRotationMatrix2D(center, angle, 1)
# 计算矩形四个角点坐标
rect_points = cv2.boxPoints((center, (width, height), angle))
# 应用旋转矩阵(如果需要旋转的话)
rect_points = cv2.transform(np.array([rect_points]), rotation_matrix)[0]
# 打印矩形四个角点坐标
for point in rect_points:
print(point)
```
这个代码会输出矩形四个角点的坐标,可以根据需要进行使用。
numpy如何根据矩形的中心和边长获得矩形轮廓的点
可以使用`numpy`中的函数`numpy.linspace`来生成矩形的轮廓点坐标。具体步骤如下:
1. 根据矩形中心和边长计算出左下角的坐标。
2. 根据矩形的边长和步长,使用`numpy.linspace`生成矩形上、下、左、右四条边的点坐标。
3. 将四条边的点坐标合并,得到矩形的轮廓。
代码示例:
```python
import numpy as np
def get_rect_points(center, length):
# 计算左下角坐标
x0 = center[0] - length / 2
y0 = center[1] - length / 2
# 计算四条边的点坐标
x = np.linspace(x0, x0 + length, num=5)
y = np.linspace(y0, y0 + length, num=5)
x_top, y_top = np.meshgrid(x, np.array([y0 + length]))
x_bottom, y_bottom = np.meshgrid(x, np.array([y0]))
x_left, y_left = np.meshgrid(np.array([x0]), y)
x_right, y_right = np.meshgrid(np.array([x0 + length]), y)
# 合并四条边的点坐标
points = np.concatenate([np.vstack((x_top, y_top)),
np.vstack((x_bottom, y_bottom)),
np.vstack((x_left, y_left)),
np.vstack((x_right, y_right))], axis=1)
return points.T
```
使用示例:
```python
center = [50, 50]
length = 20
points = get_rect_points(center, length)
print(points)
```
输出结果:
```
[[45. 45.]
[46. 45.]
[47. 45.]
[48. 45.]
[49. 45.]
[50. 45.]
[51. 45.]
[52. 45.]
[53. 45.]
[54. 45.]
[45. 50.]
[46. 50.]
[47. 50.]
[48. 50.]
[49. 50.]
[50. 50.]
[51. 50.]
[52. 50.]
[53. 50.]
[54. 50.]
[45. 55.]
[46. 55.]
[47. 55.]
[48. 55.]
[49. 55.]
[50. 55.]
[51. 55.]
[52. 55.]
[53. 55.]
[54. 55.]
[45. 46.]
[45. 47.]
[45. 48.]
[45. 49.]
[45. 50.]
[45. 51.]
[45. 52.]
[45. 53.]
[45. 54.]
[45. 55.]
[54. 46.]
[54. 47.]
[54. 48.]
[54. 49.]
[54. 50.]
[54. 51.]
[54. 52.]
[54. 53.]
[54. 54.]
[54. 55.]]
```
可以看到,生成的点坐标是一个二维数组,每一行代表一个点的横纵坐标。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)