I have found that the following code fails when I'm trying to implement a resizing rectangle:
for(int i=0;i<colorBalls.size();i++)
{
int centerX=colorBalls.get(i).getX()+colorBalls.get(i).getWidthOfBall();
int centerY=colorBalls.get(i).getY()+colorBalls.get(i).getHeightOfBall();
double radCircle=Math.sqrt(Math.pow(x-centerX,2)+Math.pow(y-centerX, 2));
if(radCircle<colorBalls.get(i).getWidthOfBall())
{
mBallID=colorBalls.get(i).getId();
Log.d("RectangleView", "ID of ball selected "+mBallID);
if(mBallID==1 || mBallID==3)
mGroupID=2;
else
mGroupID=1;
invalidate();
break;
}
}
I know because in the following code,only Resizing balls 2 and 4
is printed but the other one is never printed in my Logcat
:
colorBalls.get(mBallID).setX(x);
colorBalls.get(mBallID).setY(y);
if (mGroupID == 1) {
Log.d("RectangleView","Resizing balls 2 and 4");
colorBalls.get(1).setX(colorBalls.get(0).getX());
colorBalls.get(1).setY(colorBalls.get(2).getY());
colorBalls.get(3).setX(colorBalls.get(2).getX());
colorBalls.get(3).setY(colorBalls.get(0).getY());
}
if(mGroupID==2)
{
Log.d("RectangleView", "Resizing balls 1 and 3");
colorBalls.get(0).setX(colorBalls.get(1).getX());
colorBalls.get(0).setY(colorBalls.get(3).getY());
colorBalls.get(2).setX(colorBalls.get(3).getX());
colorBalls.get(2).setY(colorBalls.get(1).getY());
}
invalidate();
My code is an implementation of the second answer to this
The points are rendered in anti clockwise fashion,0 is the leftmost edge,1 is below 0,2 is on the same line as 1,to the right and 3 is above 2.
EDIT:
Modified the code:
int centerX=colorBalls.get(i).getX()+colorBalls.get(i).getWidthOfBall()/2;//here
int centerY=colorBalls.get(i).getY()+colorBalls.get(i).getHeightOfBall()/2;//here
double radCircle=Math.sqrt(Math.pow(x-centerX,2)+Math.pow(y-centerY, 2));//here
if(radCircle<colorBalls.get(i).getWidthOfBall())
{
mBallID=colorBalls.get(i).getId();
Log.d("RectangleView", "ID of ball selected "+mBallID);
if(mBallID==1 || mBallID==3)
mGroupID=2;
else
mGroupID=1;
invalidate();
break;