0
votes

I am aware that there are a few questions about points and rotation out here, and I feel like Im almost there. I youst need a little push.

I Have a shape with 6 points like this one.

enter image description here

I want to rotate the Point P around Point C

And I need to do this manually so I am not interested in using AffineTransform

thanks in advance


Thread thread = new Thread() {

    public void run() {

        //THE RADIUS OF THE SHAPE IS 100

        //GET THE POINT P
        PointClass point_class = points.get(0);

        //GET THE CENTER POINT C
        Point center = new Point(point_class.point.x - 100, point_class.point.y);

        int deg = 0;

        while(deg < 360) {

            //GET THE ANGLE IN RADIANS
            double angle = Math.toRadians(deg);

            //FIRST TRANSLATE THE DIFFERENCE
            int x1 = point_class.point.x - center.x;
            int y1 = point_class.point.y - center.y;

            //APPLY ROTATION
            x1 = (int) ((double) x1 * Math.cos(angle) - y1 * Math.sin(angle));
            y1 = (int) ((double) x1 * Math.sin(angle) + y1 * Math.cos(angle));

            //TRANSLATE BACK
            point_class.point.x = x1 + center.x;
            point_class.point.y = y1 + center.y;

            //ROTATE + 1 DEEGRE NEXT TIME
            deg++;

            try {

                //SLEEP TO SEE THE DIFFERENCE
                sleep(100);

            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }
};

thread.start();

What happens with this code is that the Point P ends up in the center like this

enter image description here

2

2 Answers

4
votes

I think your radius is shrinking each time through the while loop due to the casting of doubles to ints. This might work better instead:

double x1 = point_class.point.x - center.x;
double y1 = point_class.point.y - center.y;

//APPLY ROTATION
x1 = x1 * Math.cos(angle) - y1 * Math.sin(angle));
y1 = x1 * Math.sin(angle) + y1 * Math.cos(angle));

//TRANSLATE BACK
point_class.point.x = (int)Math.ceil(x1) + center.x;
point_class.point.y = (int)Math.ceil(y1) + center.y;
2
votes

So I figured out what was wrong.

The Translation of the two points,

//FIRST TRANSLATE THE DIFFERENCE
double x1 = point_class.point.x - center.x;
double y1 = point_class.point.y - center.y;

has to go outside of the loop, because I need to take base at that location when applying the rotation matrix. And also in the loop, I should have the deegre fixed at 1, so that it is only incrementet by 1 and not 81+82+83...Dont know why i did that.

Hope this helps someone =)