int rect_x = it.x() - RECT_SIZE / 2; int rect_y = it.y() - RECT_SIZE / 2;这是为什么要这么写
时间: 2024-03-28 08:41:46 浏览: 29
这两行代码是为了计算出每个点周围的矩形区域的左上角坐标。假设当前处理的点的坐标是 `(it.x(), it.y())`,矩形区域的大小是 `RECT_SIZE`,那么我们可以将矩形区域的中心点放在当前点的位置,左上角坐标就可以通过以下方式计算得到:
```c++
int rect_x = it.x() - RECT_SIZE / 2;
int rect_y = it.y() - RECT_SIZE / 2;
```
这里将矩形区域的宽度和高度都设为 `RECT_SIZE`,然后将左上角坐标向左和向上移动矩形区域宽度和高度的一半,就可以将矩形区域的中心点对齐到当前点的位置上了。这样做的好处是可以确保矩形区域的中心点和当前点重合,从而更好地反映出当前点周围的像素分布情况。
相关问题
在qgraphicsview中用此函数遍历多个点集for (auto it : m_pts) { /*int rect_x = it.x() - RECT_SIZE/4 ; int rect_y = it.y() - RECT_SIZE /4; cv::Rect rect(rect_x, rect_y, RECT_SIZE, RECT_SIZE); rect = rect & cv::Rect(0, 0, ImgBin.cols, ImgBin.rows); for (int nRow = rect_y - rect.height; nRow < rect_y + rect.height; nRow++) { for (int nCol = rect_x - rect.width; nCol < rect_x + rect.width; nCol++) {*/ ImgBin.row(it.y()).data[it.x()] = 255; }在超出图片的地方会进入断点
根据您提供的代码,可能是由于循环中的 rect 超出了图像的范围,导致在超出图片的地方进入了断点。
您可以在循环前加入判断,确保 rect 不会超出图像的范围。例如:
```cpp
for (auto it : m_pts) {
int rect_x = it.x() - RECT_SIZE / 4;
int rect_y = it.y() - RECT_SIZE / 4;
cv::Rect rect(rect_x, rect_y, RECT_SIZE, RECT_SIZE);
rect = rect & cv::Rect(0, 0, ImgBin.cols, ImgBin.rows);
for (int nRow = rect.y; nRow < rect.y + rect.height; nRow++) {
for (int nCol = rect.x; nCol < rect.x + rect.width; nCol++) {
ImgBin.row(nRow).data[nCol] = 255;
}
}
}
```
这里,使用 `cv::Rect` 的 `&` 运算符来确保 `rect` 不会超出图像的范围。同时,在循环中使用 `rect.x` 和 `rect.y` 作为起始值,确保不会越界。
def custom_draw(self, player): # getting the offset self.offset.x = player.rect.centerx - self.half_width self.offset.y = player.rect.centery - self.half_height # drawing the floor floor_offset_pos = self.floor_rect.topleft - self.offset self.display_surface.blit(self.floor_surf, floor_offset_pos) # for sprite in self.sprites(): for sprite in sorted(self.sprites(), key=lambda sprite: sprite.rect.centery): offset_pos = sprite.rect.topleft - self.offset self.display_surface.blit(sprite.image, offset_pos)对代码进行注解
好的,下面是对这段代码的详细注释:
```python
def custom_draw(self, player):
# getting the offset
# 计算偏移量,使玩家的位置居中
self.offset.x = player.rect.centerx - self.half_width
self.offset.y = player.rect.centery - self.half_height
# drawing the floor
# 绘制地面
floor_offset_pos = self.floor_rect.topleft - self.offset
self.display_surface.blit(self.floor_surf, floor_offset_pos)
# for sprite in self.sprites():
# 对所有精灵进行循环绘制
for sprite in sorted(self.sprites(), key=lambda sprite: sprite.rect.centery):
# 计算精灵在屏幕上的位置
offset_pos = sprite.rect.topleft - self.offset
# 绘制精灵
self.display_surface.blit(sprite.image, offset_pos)
```
以上是对代码的注释,希望能够帮助你更好地理解它的作用。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)