0

I am trying to make a Re-sizable touch view which I made successfully. You can find code How to make view resizable on touch event.

It has 4 corners. You can re-size that rectangle by dragging one of corner. But now I want to enhance that logic and want to put rotation in that code. I successfully find angle when user touch center of one of the edge of rectangle. But now problem is I can't get new position of corners so that I can redraw that rectangle and rotation is possible.

Question is : How can I calculate 4 corners new position based on Angle?.

Community
  • 1
  • 1
Chintan Rathod
  • 25,864
  • 13
  • 83
  • 93

2 Answers2

0

If you know the angle by which to rotate then you don't need to calculate the rectangle's vertices. A simple way to do it would be as below

@Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            canvas.save();
            canvas.rotate(60.0f);

            paint.setColor(Color.BLACK);
            paint.setStyle(Paint.Style.STROKE);

            canvas.drawRect(10, 10, 100, 100, paint);
            canvas.restore();
        }
asloob
  • 1,308
  • 20
  • 34
  • I know that scenario to rotate canvas.. but then after points were on their old place. Next time I can't find new place of 4 corner points on canvas. This will not work my friend. – Chintan Rathod May 06 '13 at 11:32
0

As I understood, you want to calculate new coordinates based on a rotation angle... It's probably simpler than you think:

x' = x × (cosα - sinα)
y' = y × sinα × cosα

So, you just apply this programmatically, considering where x' is the rotation result of x and the same for y' and y, and α is the rotation angle.

cosα and sinα functions are available in Java as Math.cos(α) and Math.sin(α), but attention: in Java, all trigonometric functions uses radians and not degrees as angle, so you can consider this: rad = deg * 180 / π Applicable as:

double deg = 45d; //Put instead your degrees
double rad = deg / 180 * Math.PI; //The radians convertion
Davide Cannizzo
  • 2,826
  • 1
  • 29
  • 31