python绘制领域矩形

news/2024/7/8 5:11:35 标签: python, 开发语言, 领域

问题描述:

        使用python书写代码实现以下功能:给定四个点的坐标,调用一个函数,可以使原来的四个点分别向四周上下左右移动15距离,分别记录下移动后的坐标,然后画出内侧矩形和外侧矩形

代码: 

import matplotlib.pyplot as plt

def move_points(points, distance=15):
    """
    移动给定的四个点,分别向上下左右移动指定的距离。
    
    Parameters:
    points (list of tuples): 四个点的坐标 [(x1, y1), (x2, y2), (x3, y3), (x4, y4)]
    distance (int): 移动的距离
    
    Returns:
    dict: 包含移动后坐标的字典
    """
    moved_points = {
        'up': [(x, y + distance) for x, y in points],
        'down': [(x, y - distance) for x, y in points],
        'left': [(x - distance, y) for x, y in points],
        'right': [(x + distance, y) for x, y in points]
    }
    return moved_points

def plot_rectangles(original_points, moved_points):
    """
    绘制原始矩形和移动后的矩形。
    
    Parameters:
    original_points (list of tuples): 原始的四个点的坐标
    moved_points (dict): 移动后的点的坐标字典
    """
    fig, ax = plt.subplots()

    # 原始矩形
    original_rect = plt.Polygon(original_points, closed=True, fill=None, edgecolor='b', label='Original')
    ax.add_patch(original_rect)

    # 移动后的矩形(上下左右分别画出)
    for direction, points in moved_points.items():
        moved_rect = plt.Polygon(points, closed=True, fill=None, edgecolor='r', linestyle='--', label=f'Moved {direction}')
        ax.add_patch(moved_rect)

    # 设置轴的范围
    all_points = original_points + [point for points in moved_points.values() for point in points]
    all_x = [p[0] for p in all_points]
    all_y = [p[1] for p in all_points]
    ax.set_xlim(min(all_x) - 10, max(all_x) + 10)
    ax.set_ylim(min(all_y) - 10, max(all_y) + 10)

    plt.xlabel('X')
    plt.ylabel('Y')
    plt.legend()
    plt.title('Original and Moved Rectangles')
    plt.show()

# 主程序
original_points = [(10, 10), (30, 10), (30, 30), (10, 30)]

# 移动点
moved_points = move_points(original_points)

# 绘制矩形
plot_rectangles(original_points, moved_points)

效果:

问题描述:

使用python书写代码实现以下功能:已知给定四个点的坐标,通过调用一个函数,可以使原来的四个点分别向四周上下左右移动15距离,分别记录下移动后的坐标,然后以最外侧的点绘制成一个矩形,内侧的点绘成另外一个矩形,同时保留原来的坐标围成的矩形 

代码:

import matplotlib.pyplot as plt

def move_points(points, distance=15):
    """
    移动给定的四个点,分别向上下左右移动指定的距离。
    
    Parameters:
    points (list of tuples): 四个点的坐标 [(x1, y1), (x2, y2), (x3, y3), (x4, y4)]
    distance (int): 移动的距离
    
    Returns:
    dict: 包含移动后坐标的字典
    """
    moved_points = {
        'up': [(x, y + distance) for x, y in points],
        'down': [(x, y - distance) for x, y in points],
        'left': [(x - distance, y) for x, y in points],
        'right': [(x + distance, y) for x, y in points]
    }
    return moved_points

def get_outermost_and_innermost_points(points_dict):
    """
    获取最外侧和最内侧的点。
    
    Parameters:
    points_dict (dict): 移动后的点的坐标字典
    
    Returns:
    tuple: (最外侧点, 最内侧点)
    """
    all_points = [point for points in points_dict.values() for point in points]
    xs, ys = zip(*all_points)
    
    outermost_points = [(min(xs), min(ys)), (max(xs), min(ys)), (max(xs), max(ys)), (min(xs), max(ys))]
    innermost_points = [(min(xs), max(ys)), (max(xs), max(ys)), (max(xs), min(ys)), (min(xs), min(ys))]
    
    return outermost_points, innermost_points

def plot_rectangles(original_points, outermost_points, innermost_points):
    """
    绘制最外侧矩形、最内侧矩形和原始矩形。
    
    Parameters:
    original_points (list of tuples): 原始的四个点的坐标
    outermost_points (list of tuples): 最外侧的四个点的坐标
    innermost_points (list of tuples): 最内侧的四个点的坐标
    """
    fig, ax = plt.subplots()

    # 原始矩形
    original_rect = plt.Polygon(original_points, closed=True, fill=None, edgecolor='g', label='Original')
    ax.add_patch(original_rect)

    # 最外侧矩形
    outer_rect = plt.Polygon(outermost_points, closed=True, fill=None, edgecolor='b', label='Outermost')
    ax.add_patch(outer_rect)

    # 最内侧矩形
    inner_rect = plt.Polygon(innermost_points, closed=True, fill=None, edgecolor='r', linestyle='--', label='Innermost')
    ax.add_patch(inner_rect)

    # 设置轴的范围
    all_points = original_points + outermost_points + innermost_points
    all_x = [p[0] for p in all_points]
    all_y = [p[1] for p in all_points]
    ax.set_xlim(min(all_x) - 10, max(all_x) + 10)
    ax.set_ylim(min(all_y) - 10, max(all_y) + 10)

    plt.xlabel('X')
    plt.ylabel('Y')
    plt.legend()
    plt.title('Original, Outermost, and Innermost Rectangles')
    plt.show()

# 主程序
original_points = [(10, 10), (30, 10), (30, 30), (10, 30)]

# 移动点
moved_points = move_points(original_points)

# 获取最外侧和最内侧的点
outermost_points, innermost_points = get_outermost_and_innermost_points(moved_points)

# 绘制矩形
plot_rectangles(original_points, outermost_points, innermost_points)

效果:


http://www.niftyadmin.cn/n/5536591.html

相关文章

代码随想录打卡第十四天

代码随想录–二叉树部分 day14 二叉树第二天 文章目录 代码随想录--二叉树部分一、力扣226--反转二叉树二、力扣101--对称二叉树三、力扣104--二叉树的最大深度四、力扣111--二叉树的最小深度 一、力扣226–反转二叉树 代码随想录题目链接:代码随想录 给你一棵二叉…

C#委托事件的实现

1、事件 在C#中事件是一种特殊的委托类型,用于在对象之间提供一种基于观察者模式的通知机制。 1.1、事件的发送方定义了一个委托,委托类型的声明包含了事件的签名,即事件处理器方法的签名。 1.2、事件的订阅者可以通过运算符来注册事件处理器…

(已解决)Adobe Flash Player已不再受支持

文章目录 前言解决方案 前言 一般来说,很少遇到官方网站使用Adobe Flash Player来进行录用名单公示了。但是,今天就偏偏遇到一次, 用谷歌浏览器打不开, 点了没有反应,用其他的浏览器,例如windows自带的那…

react native优质开源项目

React Native 是一个非常流行的用于构建跨平台移动应用程序的框架,开源社区贡献了许多优质的项目和库。以下是一些备受认可的 React Native 开源项目,适合用来学习和参考: ### 1. **React Native Elements** [React Native Elements](https:…

SwiftUI八与UIKIT交互

代码下载 SwiftUI可以在苹果全平台上无缝兼容现有的UI框架。例如,可以在SwiftUI视图中嵌入UIKit视图或UIKit视图控制器,反过来在UIKit视图或UIKit视图控制器中也可以嵌入SwiftUI视图。 本文展示如何把landmark应用的主页混合使用UIPageViewController和…

yolo-seg模型后处理

yolo-seg模型mask处理 YOLOv8-seg模型一共有两个输出。第一个输出是“output0”,它的类型是float32[1,116,8400]。在这个输出中,前84个列与YOLOv8目标检测模型的输出定义相同,包括cx、cy、w、h这4项,再加上80个类别的分数。而后面…

事务的特性-原子性(Atomicity)、一致性(Consistency)、隔离性(Asolation)、持久性(Durability)

一、引言 1、数据库管理系统DBMS为保证定义的事务是一个逻辑工作单元,达到引入事务的目的,实现的事务机制要保证事务具有原子性、一致性、隔离性和持久性,事务的这四个特性也统称为事务的ACID特性 2、当事务保持了ACID特性,才能…

记一次因ThreadPoolExecutor多线程导致服务器内存压满问题

经过下载服务器内存数据得知是通过多线程业务处理查询list集合数据没有得到正确释放导致的。 首先先了解一下list集合数据的存放和回收(可能说的不对,请谅解【挠头】) 存放: 当我们创建一个list或者从数据库查询出的数据用list集…