Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么';此IF语句是否阻止图像超出范围_C++ - Fatal编程技术网

C++ 为什么';此IF语句是否阻止图像超出范围

C++ 为什么';此IF语句是否阻止图像超出范围,c++,C++,我正在制作一个程序,将.bmp文件中的每个像素复制到另一个像素上,我相信我的if语句试图防止复制的图像超出范围,但不起作用。我对C++很陌生,这真的让我困惑。 请注意,这是button.cc文件,而不是main.cc我90%确定问题出在这里 #include "robot.h" #include <iostream> #include "cpputils/graphics/image.h" #include <string> u

我正在制作一个程序,将.bmp文件中的每个像素复制到另一个像素上,我相信我的
if
语句试图防止复制的图像超出范围,但不起作用。我对C++很陌生,这真的让我困惑。 请注意,这是
button.cc
文件,而不是
main.cc
我90%确定问题出在这里

#include "robot.h"
#include <iostream>
#include "cpputils/graphics/image.h"
#include <string> 

using std::string;; 
using std::endl;  

void Robot::Draw(graphics::Image &my_image) {
  if ((integer % 2) == 0) {
    graphics::Image robot1;
    robot1.Load(fileName1_);
    int width = robot1.GetWidth();
    int height = robot1.GetHeight();

    for (int i = 0; i < width; i++) {
      for (int j = 0; j < height; j++) {
        graphics::Color color = robot1.GetColor(i, j);
//CHECKS IF ITS OUT OF BOUNDS
        if (i <= my_image.GetWidth() && j <= my_image.GetHeight()) {
            my_image.SetColor(i - x_ + (width / 2), j - y_ + (height / 2),
                              color);
        }
      }
    }
    integer++;
  }
else {
  graphics::Image robot2;
  robot2.Load(fileName2_);
  int width = robot2.GetWidth();
  int height = robot2.GetHeight();

  for (int i = 0; i < width; i++) {
    for (int j = 0; j < height; j++) {
      graphics::Color color = robot2.GetColor(i, j);
//CHECKS IF ITS OUT OF BOUNDS
      if (i <= my_image.GetWidth() && j <= my_image.GetHeight()) {
       
          my_image.SetColor(i - x_ + (width / 2), j - y_ + (height / 2), color);
          
        }
      }
    }
    integer++;
  }

}
#包括“robot.h”
#包括
#包括“cpputils/graphics/image.h”
#包括
使用std::string;;
使用std::endl;
void Robot::Draw(图形::图像和我的图像){
如果((整数%2)==0){
图形:图像机器人1;
robot1.Load(文件名1_);
int width=robot1.GetWidth();
int height=robot1.GetHeight();
对于(int i=0;i如果(i<P>C++中的代码在<代码> 0 和<代码> COUNT-1之间的范围,则这反映在for循环中,使用<代码> i <宽度> /COD>和 J>高度条件。
image类的成员函数:它们给出介于
0
total\u amount-1
之间的有效数字总数

请参见此处,了解易于概括的小示例:

0,1,2,3
0..3
范围内的
4
数字。
4
本身不包括在内

通过
my\u image.SetColor(x,y,c)
进行的访问仍然可能超出范围,具体取决于(成员?)变量
x\u
y\u
的值

在没有真实机器人和图像的情况下,检查越界检查器更容易:专注于最小值和最大值的组合,因为x和y的模式是相同的,所以在一维下就足够了。因此,最多可以得到8种不同的场景,甚至可以在纸上计算

毕竟,显示的将机器人放置到图像中的方法似乎效率很低。大多数2D图形系统提供一次执行这些图像操作的功能。这将类似于:

robot1.GetImage().Blit(my_image, x, y);

什么是
x
y
?什么值对
SetColor
有效?您确定您的数字对
i=0
j=0
不是负数,对
i==GetWidth()
j==GetHeight()不是太高吗
?如果您不确定这些问题中的任何一个,那么您很可能知道查找问题的正确位置:)您使用的是什么图形库?SFML?