次のように、メソッドを 1 つ作成するだけです。
public bool DoesIntersect(Rect topRect, Rect bottomRect, Rect ballRect)
{
topRect.Intersect(ballRect);
bottomRect.Intersect(ballRect);
if (topRect.IsEmpty && bottomRect.IsEmpty)
{
return false;
}
else
{
return true;
}
}
そして、次のDoesIntersect
ように を呼び出します。
var doesBall1Intersect = DoesIntersect(topRect, bottomRect, blueBallRect1);
var doesBall2Intersect = DoesIntersect(topRect, bottomRect, blueBallRect2);
var doesBall3Intersect = DoesIntersect(topRect, bottomRect, blueBallRect3);
var doesBall4Intersect = DoesIntersect(topRect, bottomRect, blueBallRect4);
var doesBall5Intersect = DoesIntersect(topRect, bottomRect, blueBallRect5);
var doesBall6Intersect = DoesIntersect(topRect, bottomRect, blueBallRect6);
var doesBall7Intersect = DoesIntersect(topRect, bottomRect, blueBallRect7);
var doesBall8Intersect = DoesIntersect(topRect, bottomRect, blueBallRect8);
var doesBall9Intersect = DoesIntersect(topRect, bottomRect, blueBallRect9);
.を置き換えるだけで、何度でも何度でも使用できますblueBallRectX
。
BlueBallRect
次のように、オブジェクトのリストをループして、それぞれをDoesIntersect
メソッドに渡すこともできます。
List<BlueBallRect> listOfBlueBallRect = new List<BlueBallRect>();
listOfBlueBallRect = SomeMethodThatGetsListOfBlueBallRect;
foreach(BlueBallRect ball in listOfBlueBallRect)
{
if(DoesIntersect(topRect, bottomRect, ball))
{
// Do something here, because they intersect
}
else
{
// Do something else here, because they do not intersect
}
}