1

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;
Community
  • 1
  • 1
vamsiampolu
  • 6,328
  • 19
  • 82
  • 183
  • 1
    Well, with just a quick glance, I see a couple problems with your code: The centers should be the coordinates plus _half_ the dimension (you're missing /2 on both lines). And Math.pow(y-centerX, 2) should be Math.pow(y-centerY, 2). – Mike M. Dec 02 '13 at 06:27
  • @MikeM. thanks man,made the changes and it works perfectly now...guess a fresh eye always helps – vamsiampolu Dec 02 '13 at 06:36
  • Sure,post your answer – vamsiampolu Dec 02 '13 at 06:41

1 Answers1

0

Your code just has a few minor math errors. In calculating centerX/centerY, you need to add half the width/height, like so:

int centerX = colorBalls.get(i).getX() + colorBalls.get(i).getWidthOfBall() / 2;
int centerY = colorBalls.get(i).getY() + colorBalls.get(i).getHeightOfBall() / 2;

And, in figuring the radius, you accidentally subtracted centerX instead of centerY from the y coordinate. Should be:

double radCircle = Math.sqrt(Math.pow(x-centerX, 2) + Math.pow(y-centerY, 2));
Mike M.
  • 38,532
  • 8
  • 99
  • 95