0

I tried to post a different topic for this but people didn't really seem to understand what I was trying to do, so, now I've closed that one and opened this one to give more detail and rephrase the question as a whole.

Ok.

So basically, I have an application which draws an Ellipse. Now, I have a certain number of points (that can be random) in which I have to rotate an image and draw at.

Using Maths I know that to get a point on an Ellipse based by using an angle I use the following equation;

final int radiusW = (width / 2);
final int radiusH = (height / 2);
final int angle = 120;

int pointX = (int) (radiusW + (radiusW * Math.cos(Math.toRadians(angle))));
int pointY = (int) (radiusH + (radiusH * Math.sin(Math.toRadians(angle))));

And that works fine, I can locate an absolute point around the perimeter of the Ellipse.

However, now I'm trying to draw an image on this point so that the image is rotated facing the center of ellipse and is centered on the point.

So, to get the image rotated to the center of the point I do the following;

final AffineTransform at = new AffineTransform();
at.rotate(Math.toRadians(angle - 90), image.getWidth() / 2, image.getHeight() / 2);
final AffineTransformOp ato = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);

And then I get my new rotated BufferedImage using;

BufferedImage rotated = ato.filter(image, null);

However, I can't seem to be able to get the central point of the image anymore.

If the angle was 0 so that the Image was the original direction then I would simply do;

graphics.drawImage(rotated, pointX - rotated.getWidth() / 2, pointY - rotated.getHeight() / 2, this);

However I'm not sure how to find the central point and draw it based upon that on a rotated image.

I know it involves using cos and sin to multiply the original pointX and pointY by the rotation matrix but everytime I try and work out a solution it always draws completely wrong.

Any help would be very much appreciated as I've spent the best part of a day trying to resolve this.

Thank you.

ZBScripts
  • 55
  • 1
  • 7

3 Answers3

0

The thing is that if you just use sin and cos to rotate the corner of the image you will end up with the new rotated position of that corner - when actually what you want to find is the new width and height.

The center is width/2, height/2

Use this to calculate your new width and height:

Calculate Bounding box coordinates from a rotated rectangle

Community
  • 1
  • 1
Tim B
  • 40,716
  • 16
  • 83
  • 128
0

I have done this with OpenCV, and there the image have been rotated but the resulted image was having the same width and height as the initial on. The image was cropped if it was getting out of the initial dimentions and there were black pixels if no information (the rotated image has no pixels in that place).

If you think that rotated has more pixels than the initial image you can verify with size() or length(). Or you can play with the diagonals of the initial rectangle (image size): compute the projection of the diagonals and thake the greatest or what you think. But I am sure that it is similar to the OpenCV case.

I don't know if this can help you, but I hope so.

thedarkside ofthemoon
  • 2,251
  • 6
  • 31
  • 48
0

Your question is not entirely clear but it seems to me that you have a misunderstanding of the nature of the angle used to parametrise your ellipse. The angle as you have it is merely used to parametrise the form of an ellipse equation. It is not the same as the polar angle (except at particular angles). That is to say if you evaluate a point on your ellipse using an angle of (pi/4) radians (45 degrees), then measured the angle that the line from the ellipse centre to your point makes with the axis, it will not measure 45 degrees (except for the case where the ellipse is actually a circle).

That is to say that

int pointX = (int) (radiusW + (radiusW * Math.cos(Math.toRadians(angle))));
int pointY = (int) (radiusH + (radiusH * Math.sin(Math.toRadians(angle))));

is just a parametrisation of an ellipse and that angle is not a polar angle and treating this angle as a rotation angle will not give accurate results (except at integer multiples of (pi/2) radians)

It seems to me that you require the polar form of an ellipse relative to its centre in order for your code to make sense in the context of using this angle for rotation.

It is also possible that I have misunderstood your question though, in which case this answer will be downvoted on a grand scale and I will delete it.

mathematician1975
  • 21,161
  • 6
  • 59
  • 101