I have a Canvas on which I've drawn a circle / 360 degree arc. I have the arc start drawing from -90 (the top) rather than the right (0) as is default.
I want to place a rectangle at the top of the same canvas and to reduce the sweep of the arc so that the two do not intersect. I've attached an image to illustrate
So what I need to do is work out what angle is represented by half of the rectangle so that I can adjust where to start drawing my arc. The centre of the circle is at the centre of my Canvas:
[canvas.width /2, canvas.height /2]
I've read some resources like this question but they haven't helped to do much more than make me feel like I know nothing. I tried a few failing formulas ending with this
double adjustment = Math.atan2(rectangleY - circleY, rectangleX - circleX) - Math.atan2(rectangleY - circleY, (rectangleX + rectangleWidth) - circleX);
Can somebody tell me what is the right way to calculate this in Java? I'd also like to know how to find where the rectangle intersects if that's possible (I.e. the width of the shaded orange part on the image) although this is of lesser importance to me right now
Let W be the width of the rectangle, R be radius of the circle, and A be the angle you're looking for.
There's a right-angle triangle with angle A at the center, R as the hypotenuse and W/2 as the side opposite angle A, so
W/2R = sin(A)
so
A = Math.asin(0.5*W/R);
Of course you can't use asin(0.5W/R) when W > 2R
EDIT
To answer the second problem (finding the intersection)
Let H be the distance from the center of the circle to the rectangle. When the rectangle is wide enough that the circle intersects the lower side, there's a right angle triangle with A at the center, R as the hypotenuse, and H as the side adjacent to A
H/R = cos(A) and A = Math.acos(H/R). Calculate both angles and use the smaller one.
Related
I would like to know more about the coordinates of a Rectangle,
in particular:
lower left X
lower left Y
upper right X
upper right Y
Every time, I get confused about how to make dimensions based on these coordinates to draw rectangle.
If possible, can I get a graphical representation briefly about these coordinates positions?
Before someone can explain what the lower-left X, lower-left Y, upper-right X and upper-right Y of a rectangle are about, you need to know about the coordinate system: Where is the Origin (x,y) of a PDF page?
The answer to that question contains all the information you need, except for the graphical representation you are asking for. This is a simple representation of the coordinate system:
The origin of the coordinate system is (0, 0). Positive X values are to the right of the origin, positive Y values are above the origin.
I have drawn a Rectangle and indicates where you can find the lower-left corner (with coordinate (llx, lly)) and the upper-right corner (with coordinate (urx, ury)).
The sides of the rectangle are always in parallel with the X and the Y axis, hence you only need two coordinates to define the rectangle.
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.
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
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.
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.
I have a cylinder of 2f length, and the data for RotX and RotZ, given by user. I also know that the "bottom" of my cylinder is at (0,0,0) (the center of the circle forming the "bottom")
What I need is to calculate the "top" point (the center of the circle at the other end) having the data exposed in the upper lines.
In my second image, the A point is calculated as follows (sin(zAngle),cos(zAngle),sin(xAngle) with zAngle=PI/4 (alpha angle) and xAngle=0.
Point B is calculated as (sin(xAngle),cos(xAngle),sin(zAngle)) with xAngle=PI/4 (beta angle) and zAngle=0;
C point is the "top" of a cylinder with xAngle=PI/4 and zAngle=PI/4.
I need to find an algorithm to determine the "top" point for any given xAngle and zAngle.
I'd be extremly thankful if anyone could help me.
Before your rotations, the top is at 2f*(0,1,0).
After you rotate the cylinder around the z axis by angle alpha, the top is at 2f*(sin alpha, cos alpha, 0).
If you now rotate the cylinder by angle beta around the x axis, the top goes to 2f*(sin alpha, cos alpha*cos beta, cos alpha*sin beta).
I'm developing a tube shooter-esque game in java that simulates 3D without actually using any 3D libraries. Right now I have a player-controlled ship that rotates around the center point of the screen, using (in this case, for moving right).
angle += 0.1;
x = Math.cos(angle) * radius + cX;
y = Math.sin(angle) * radius + cY;
Where angle is the placement in relation to the center point (ex. 270 is directly under the center), x and y are the current ship position, radius is the distance from the center, and cX and cY are the center point's location.
Right now revolving around the point works smoothly, but I'm not sure how to handle rotating the actual ship to always point towards the center. I've looked around a lot online but can't figure out how an individual Image (or if that doesn't work, an array of drawLines) can be rotated without affecting other objects on the screen.
Long story short, how would one go about rotating an individual Image to constantly point towards a remote x,y location?
What you need is the AffineTransform class which is basically a matrix class in java. Graphics2D has a draw image variant which accepts an AffineTransform instance:
boolean java.awt.Graphics2D.drawImage(Image img, AffineTransform xform, ImageObserver obs)
To create a transform, you can use 2D matrix operations:
AffineTransform trans = new AffineTransform();
trans.translate(x, y);
trans.rotate(theta);
trans.scale(scalex, scaley);
etc...
Mind that the order is important, probably you will want to scale first, rotate and then traslate the image to the corresponding location. It should do fine.
Java has uses some 3D power to draw as fast as it can, it is faster than a software renderer, but quite far from native opengl.
I understand this code here. The point of origin is 0,0 or top left of the JFrame and the width of the rectangle is 9 and height covers from bottom to top.
Rectangle left = new Rectangle(0,0,WIDTH/9,HEIGHT);
But I don't quite understand this. What is the point of origin here? Is 9 being multiplied by 8 or is it saying the measurement is 9 by 8? What is the purpose of the multiplication sign?
Rectangle right = new Rectangle((WIDTH/9)*8,0,WIDTH/9,HEIGHT);
What is the purpose of the multiplication sign?
The x origin of the rectangle is 8/9 of the way across the JFrame. It's right justified (I assume).
Rectangle right = new Rectangle( (WIDTH/9)*8, 0, WIDTH/9, HEIGHT);
This means that the x origin is real 9/8th of the WIDTH. And its width is 1/9th WIDTH variable. Looks like this would move the rectangle horizontally.
Without seeing the whole code it's hard to know, but I'd assume that WIDTH is the total width of whatever will contain the two rectangles. In that case, you'll end up with two rectangles that themselves have a width one-ninth of the total width, and occupy the left and right sides of the container.
Since the co-ordinates are the top left corner of the rectangle, to make the one-ninth width rectangle occupy the right side of the container, the x co-ordinate needs to be eight-ninths of the total width, which is what (WIDTH/9)*8 calculates.
a bit of reworking of the values gives us
Rectangle right = new Rectangle(WIDTH-(WIDTH/9),0,WIDTH/9,HEIGHT);
this means that the right side of right falls on WIDTH