pClientDC->Rectangle(rect); rect.left -= 5; rect.right += 5; rect.top -= 5; rect.bottom += 5; pClientDC->SelectObject(&pen); pClientDC->Rectangle(rect); pen.DeleteObject(); pen0.DeleteObject(); pClientDC->DeleteDC();
时间: 2024-04-02 16:34:09 浏览: 130
这是一个 MFC 程序中的对话框类的成员函数,用于在指定控件周围绘制一个带有白边框的矩形。首先调用 CDC 对象的 Rectangle() 函数在客户区中绘制一个矩形,矩形的位置和大小由 rect 对象指定。接着通过修改 rect 对象的左、右、上、下四个边界值,将矩形扩大 5 个逻辑单位,以便绘制白色边框。然后调用 SelectObject() 函数将黑色画笔对象选入设备上下文对象中,用于绘制边框。再次调用 Rectangle() 函数绘制带有黑色边框的矩形。接着调用 DeleteObject() 函数删除画笔对象,释放资源。最后调用 DeleteDC() 函数删除设备上下文对象,释放资源。
相关问题
import pygame import math from pygame.sprite import Sprite class Robot(Sprite): def __init__(self, screen): # initialize robot and its location 初始化机器人及其位置 self.screen = screen # load image and get rectangle 加载图像并获取矩形 self.image = pygame.image.load('images/robot.png').convert_alpha() self.rect = self.image.get_rect() self.screen_rect = screen.get_rect() # put sweeper on the center of window 把扫地机器人放在界面中央 self.rect.center = self.screen_rect.center # 初始角度 self.angle = 0 self.moving_speed = [1, 1] self.moving_pos = [self.rect.centerx, self.rect.centery] self.moving_right = False self.moving_left = False def blitme(self): # buld the sweeper at the specific location 把扫地机器人放在特定的位置 self.screen.blit(self.image, self.rect) def update(self, new_robot): # 旋转图片(注意:这里要搞一个新变量,存储旋转后的图片) new_robot.image = pygame.transform.rotate(self.image, self.angle) # 校正旋转图片的中心点 new_robot.rect = new_robot.image.get_rect(center=self.rect.center) self.moving_pos[0] -= math.sin(self.angle / 180 * math.pi) * self.moving_speed[0] self.moving_pos[1] -= math.cos(self.angle / 180 * math.pi) * self.moving_speed[1] self.rect.centerx = self.moving_pos[0] self.rect.centery = self.moving_pos[1] # 右转的处理 if self.moving_right: self.angle -= 1 if self.angle < -180: self.angle = 360 + self.angle # 左转的处理 if self.moving_left: self.angle += 1 if self.angle > 180: self.angle = self.angle - 360 # 上下边界反弹的处理 if (self.rect.top <= 0 and -90 < self.angle < 90) or ( self.rect.bottom >= self.screen_rect.height and (self.angle > 90 or self.angle < -90)): self.angle = 180 - self.angle # 左右边界反弹的处理 if (self.rect.left <= 0 and 0 < self.angle < 180) or ( self.rect.right >= self.screen_rect.width and (self.angle > 180 or self.angle < 0)): self.angle = - self.angle
这是一个使用Pygame库创建的机器人类。它继承自Sprite类,并用于表示一个机器人在屏幕上的移动和旋转。在初始化方法中,它加载机器人图像,并设置其初始位置在屏幕中心。它还定义了一些属性,如角度、移动速度和移动位置。在更新方法中,它根据角度旋转机器人图像,并根据移动速度和角度计算新的移动位置。然后,它根据新的位置更新机器人的矩形。它还处理机器人的旋转方向,当向右或向左移动时,改变角度值。最后,它处理机器人在屏幕边界的反弹逻辑,使机器人在碰到边界时反弹。
!pip install matplotlib import matplotlib.pyplot as plt from matplotlib.patches import Rectangle fig, ax = plt.subplots(figsize=(8, 6)) ax.set_xlim(0, 10) ax.set_ylim(0, 10) ax.axis('off') # UE端发起释放EPS承载 rect_ue = Rectangle((1, 9), 2, 1, linewidth=1, edgecolor='black', facecolor='white') ax.add_patch(rect_ue) ax.text(1.5, 9.5, 'UE\nRelease\nEPS\nBearer', ha='center', va='center') # RRC连接释放 rect_rrc = Rectangle((4, 9), 2, 1, linewidth=1, edgecolor='black', facecolor='white') ax.add_patch(rect_rrc) ax.text(4.5, 9.5, 'RRC\nConnection\nRelease', ha='center', va='center') # Detach请求 rect_detach = Rectangle((7, 9), 2, 1, linewidth=1, edgecolor='black', facecolor='white') ax.add_patch(rect_detach) ax.text(8, 9.5, 'Detach\nRequest', ha='center', va='center') # 信令Attach success/connection success等信令 rect_attach = Rectangle((1, 5), 2, 1, linewidth=1, edgecolor='black', facecolor='white') ax.add_patch(rect_attach) ax.text(1.5, 5.5, 'Attach\nSuccess', ha='center', va='center') rect_conn = Rectangle((4, 5), 2, 1, linewidth=1, edgecolor='black', facecolor='white') ax.add_patch(rect_conn) ax.text(4.5, 5.5, 'Connection\nSuccess', ha='center', va='center') # 释放EPS承载完成 rect_done = Rectangle((7, 5), 2, 1, linewidth=1, edgecolor='black', facecolor='white') ax.add_patch(rect_done) ax.text(8, 5.5, 'Release\nDone', ha='center', va='center') # UE端发起释放EPS承载 -> RRC连接释放 ax.annotate('', xy=(3, 9.5), xytext=(4, 9.5), arrowprops=dict(arrowstyle='->')) # RRC连接释放 -> Detach请求 ax.annotate('', xy=(6, 9.5), xytext=(7, 9.5), arrowprops=dict(arrowstyle='->')) # Detach请求 -> 信令Attach success/connection success等信令 ax.annotate('', xy=(1, 8), xytext=(1, 5.5), arrowprops=dict(arrowstyle='->')) ax.annotate('', xy=(4, 8), xytext=(4, 5.5), arrowprops=dict(arrowstyle='->')) # 信令Attach success/connection success等信令 -> 释放EPS承载完成 ax.annotate('', xy=(3, 5.5), xytext=(7, 5.5), arrowprops=dict(arrowstyle='->')) plt.show(),这段代码画出来的图比例不太对,请调整
可以尝试添加以下代码调整图像大小比例:
```
fig.subplots_adjust(left=0, right=1, bottom=0, top=1)
```
完整代码如下:
```
!pip install matplotlib
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
fig, ax = plt.subplots(figsize=(8, 6))
ax.set_xlim(0, 10)
ax.set_ylim(0, 10)
ax.axis('off')
# UE端发起释放EPS承载
rect_ue = Rectangle((1, 9), 2, 1, linewidth=1, edgecolor='black', facecolor='white')
ax.add_patch(rect_ue)
ax.text(1.5, 9.5, 'UE\nRelease\nEPS\nBearer', ha='center', va='center')
# RRC连接释放
rect_rrc = Rectangle((4, 9), 2, 1, linewidth=1, edgecolor='black', facecolor='white')
ax.add_patch(rect_rrc)
ax.text(4.5, 9.5, 'RRC\nConnection\nRelease', ha='center', va='center')
# Detach请求
rect_detach = Rectangle((7, 9), 2, 1, linewidth=1, edgecolor='black', facecolor='white')
ax.add_patch(rect_detach)
ax.text(8, 9.5, 'Detach\nRequest', ha='center', va='center')
# 信令Attach success/connection success等信令
rect_attach = Rectangle((1, 5), 2, 1, linewidth=1, edgecolor='black', facecolor='white')
ax.add_patch(rect_attach)
ax.text(1.5, 5.5, 'Attach\nSuccess', ha='center', va='center')
rect_conn = Rectangle((4, 5), 2, 1, linewidth=1, edgecolor='black', facecolor='white')
ax.add_patch(rect_conn)
ax.text(4.5, 5.5, 'Connection\nSuccess', ha='center', va='center')
# 释放EPS承载完成
rect_done = Rectangle((7, 5), 2, 1, linewidth=1, edgecolor='black', facecolor='white')
ax.add_patch(rect_done)
ax.text(8, 5.5, 'Release\nDone', ha='center', va='center')
# UE端发起释放EPS承载 -> RRC连接释放
ax.annotate('', xy=(3, 9.5), xytext=(4, 9.5), arrowprops=dict(arrowstyle='->'))
# RRC连接释放 -> Detach请求
ax.annotate('', xy=(6, 9.5), xytext=(7, 9.5), arrowprops=dict(arrowstyle='->'))
# Detach请求 -> 信令Attach success/connection success等信令
ax.annotate('', xy=(1, 8), xytext=(1, 5.5), arrowprops=dict(arrowstyle='->'))
ax.annotate('', xy=(4, 8), xytext=(4, 5.5), arrowprops=dict(arrowstyle='->'))
# 信令Attach success/connection success等信令 -> 释放EPS承载完成
ax.annotate('', xy=(3, 5.5), xytext=(7, 5.5), arrowprops=dict(arrowstyle='->'))
fig.subplots_adjust(left=0, right=1, bottom=0, top=1)
plt.show()
```
阅读全文