无法检查二维字符阵列中的重叠 [英] Trouble checking for overlap in a 2-D array of chars

查看:65
本文介绍了无法检查二维字符阵列中的重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


I am having trouble with a part of a program that takes a dynamically allocated object called userRect and checks for it overlapping on an array of chars arranged in the shape of a rectangle. The other rectangles rect[0] and rect[1] are randomly placed on a imaginary grid in a console window.

rect[0] prints out with '0' rect[1] prints out with '1' userRect prints out with '#' if no overlap is present. userRect prints out with '+' at each char in the array that is overlapping another object.

The object userRect is movable with the w,a,s,d keys. What is supposed to happen is when the user moves the userRect object and it overlaps another rect object. Each character that overlaps is replaced with a '+'.

The program is not printing a '+' when the userRect is overlapping another rectangle. Can anyone point out what is causing this? 

I believe it to be a problem in the function bool isOverlapping always return false. This causes the if statement if (userRect->isOverlapping(rect[i])) to always return false causing the default print out of the '#'. Am I on track with my theory?

Here is a sample of my code with most parts cut out for ease of reading.

	bool isOverlapping(Rect const & r) const
	{
		return !(min.x >= r.max.x || max.x <= r.min.x
			|| min.y >= r.max.y || max.y <= r.min.y);
	}
	

int main()
{
	Rect * userRect;
	const int rectSize =2;
	Rect rect[rectSize];
	const int ARRAY_SIZE = 13;	// size of userRect
        userRect = new Rect();

	// set
	userRect->setMin(7, 5);
	userRect->setMax(10, 9);

	do
	{
		// draw
		rect[0].draw('0');	
		rect[1].draw('1');	
		
		for (int i = 0; i < ARRAY_SIZE; i++)
		{
			if (userRect->isOverlapping(rect[i]))
			{
				userRect->draw('+');
			}
			else userRect->draw('#');
		}
	return 0;
}





我的尝试:



我试过删除! isOverlapping的返回语句前面的符号。

我也试过在isOverlapping函数中翻转来自< = to> =内部的符号。



What I have tried:

I have tried removing the ! symbol in front of the return statement for isOverlapping.
I have also tried flipping the signs from <= to >= inside of the if statement in the isOverlapping function.

推荐答案

我弄清楚出了什么问题。问题出在我的for循环中。如果userRect位于rect [0]的边界内但不在rect [1]的范围内,则
I have figured out what was wrong. The problem was in my for loop. If the userRect was within the boundaries of the rect[0] but not within the bounds of rect[1] the
userRect->draw('#');

会覆盖

userRect->draw('+');

刚刚执行的操作。


这篇关于无法检查二维字符阵列中的重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆