How to rotate an image based on two points

I have some images that I control, and on these images I always have two points (x1, y1) and (x2, y2). eg:

|----------------| | | | . | | | | . | | | |----------------| 

I need to code an algorithm to align the image as follows

 |----------------| | | | | | . . | | | | | |----------------| 

I already read this question, but the angle obtained using

 double angle = Math.Atan2(pointB.Y - pointA.Y, pointB.X - pointA.X); 

Doesn't work when I use this rotation code in java:

 public static BufferedImage tilt(BufferedImage image, double angle) { double sin = Math.abs(Math.sin(angle)), cos = Math.abs(Math.cos(angle)); int w = image.getWidth(), h = image.getHeight(); int neww = (int) Math.floor(w * cos + h * sin), newh = (int) Math .floor(h * cos + w * sin); GraphicsConfiguration gc = getDefaultConfiguration(); BufferedImage result = gc.createCompatibleImage(neww, newh, Transparency.OPAQUE); Graphics2D g = result.createGraphics(); g.setColor(Color.white); g.setBackground(Color.white); g.fillRect(0, 0, neww, newh); g.translate((neww - w) / 2, (newh - h) / 2); g.rotate(angle, w / 2, h / 2); g.drawRenderedImage(image, null); g.dispose(); return result; } 

In the message mentioned above, they use C # code, for example

 myImage.TranslateTransform(-pointA.X, -pointA.Y); myImage.RotateTransform((float) angle, MatrixOrder.Append); myImage.TranslateTransform(pointA.X, pointA.Y, MatrixOrder.Append); 

Is there anyone who can help with the java implementation for this case?

+4
source share
3 answers

Ok ... to solve the problem, I just converted the radians returned by Math.atan2 to degrees, and the rotation worked well.

Thanx everyone

+1
source

If the left point in your example is A and the right point in your example is B, imagine you draw a point C (AX, BY) that has a 90 degree angle to complete the triangle. If you use geometry calculation to calculate angle angle A, you know how much to rotate.

0
source

for me the following worked to align the OMR marking, where pointA is the TopLeft Corner mark, pointB is the BottomLeft corner mark

double angle = Math.Atan2 (point Bx - point Ax, point By - point A.y);

0
source

Source: https://habr.com/ru/post/1347115/


All Articles