How to get the inscribed rectangle in a canvas clip - java

In android canvas, I've defined a clipping area with many shapes (a rectangle and 2 circles). I want to get the inscribed rectangle defined by the clipping area.
Canvas has a method called getClipBounds() which gives me the circumscribed rectangle, how do I get the inscribed rectangle instead?
Edit: Here's some info on how the original shape is made:
First add a clip for the big circle with Region.Op.INTERSECT
Then add a clip for a vertical rectangle with Region.Op.INTERSECT
Then add a clip for a smaller circle with Region.Op.DIFFERENCE

This can be determined mathematically if you know the radius & center of both circles.
Find two the intersection points of circles with the getClipBounds() rectangle.
-The the second highest intersection point between the red circle and the getClipBounds() rectangle holds the upper y coordinate of the rectangle.
-The high intersection point between the white circle and the getClipBounds() rectangle holds the lower y coordinate of the rectangle.
-The x bounds are already given by the getClipBounds() rectangle.
You can construct your three shapes from there.

Related

How to Rotate AWT rectangle in JAVA?

I am creating a small java 2D game and i want to know if there is any way to rotate AWT rectangle
AffineTransform origXform = g2d.getTransform();
AffineTransform newXform = (AffineTransform) origXform.clone();
newXform.rotate(angle, pivotX, pivotY); // pivotX ,pivotY is the starting
point of the hand image
g2d.setTransform(newXform);
Rectangle arm = new Rectangle(bowX + 5, bowY + 55, 60, 5);
g2d.drawImage(playerBowReadyImg, bowX, bowY, null); //hand image
on the above code i simply draw the hand image which rotates based on the mouse position, i also set the rectangle on the hand but the problem is rectangle is not rotating along with the hand image.
also i am not using rectangle for any drawing purpose but to detect the collision.
however using g2d.draw(arm); draws the rotated rectangle but it not actually rotate the rectangle it just draws the rotated one.
any suggestion is appreciated.
Ok my question is marked as duplicate so i tried the answers i find there but the code i get only rotate my rectangle for the draw purpose only.
Image to depict the problem
now to be more specific the arrow in the image can only detect the collision for the blue rectangle (the original position) instead of the red one (rotated rectangle).
again i don't want to actually draw the rectangle but want to detect the collision when the arrow collide with the rectangle.
See AffineTransform.createTransformedShape(Shape) which:
Returns a new Shape object defined by the geometry of the specified Shape after it has been transformed by this transform.

How do I create a hitbox for a semi-circle?

I want to collide a rectangle with a semi-circle that comes rotating. A rectangular hitbox for the semi-circle, of course, is not appropriate as the side of the rectangular hitbox on the round side of the semi-circle will touch other targets before the semi-circle touches the targets. I am not inclined to use pixel collision detection as the two images are large.
I am thinking of using stripes of narrow hitbox rectangles of varying heights that approximately cover the contours of the semi-circle. How well would this work?
Not sure how you are doing collision detection, so I will just assume parametric equations and you can easily test for collisions between basic shapes.
If you think of a semi-circle as a circle missing a triangular element, then this is easy.
if (collide(rect, circle) && !collide(rect, triangle)) {
// then collision semi-circle
}
Use an isosceles triangle. The tip of the triangle is the center of the circle. The height of the triangle is the radius of the circle. The angle is based on what is missing from the circle.

LibGDX collision bounding Rectangle

I'm creating some rectangles surrounding my sprites for my player and bullet class to detect collisions with the overlaps method in Intersector class of LibGDX.
I have a question:
When I instantiate Player and Bullet, I create a bounding box around the sprite using sprite.getBoundingRectangle() which returns a Rectangle object. I update the movement of these somewhere else in the main class.
When I update the movement of the bullet/player sprite, do I also need to update the position of the bounding box surrounding the bullet/player sprite? Or since the bounding Rectangle surrounds the sprite, will the box automatically move with the sprite?
Thanks
As per getBoundingRectangle javadoc:
Returns the bounding axis aligned Rectangle that bounds this sprite. The rectangles x and y coordinates describe its bottom left corner. If you change the position or size of the sprite, you have to fetch the triangle again for it to be recomputed.
Indeed, if you open Sprite source code, you'll see that the bounding rectangle is updated only when getBoundRectangle is called.

Java AWT bounds

I have an assignment to draw a certain number of circles using java.awt.Graphics.
Drawing the circles is fairly simple, but I am only supposed to draw the circle if it appears within the visible area. I know I can call method getClipBounds() to determine the drawing area, but I'm having trouble finding a java implementation of a way to determine if a circle intersects a Rectangle.
Is that the right way to go about determining if the circle I want to draw will be completely visible or is there a simpler way?
Don't use the Graphics.fillOval(...) method to do the painting.
Instead you can use the Graphics2D.fill(Shape) method. You can create oval Shape objects using the Ellipse2D class.
but I'm having trouble finding a java implementation of a way to determine if a circle intersects a Rectangle.
The Shape object has a method that will allow you to get the rectangular bounds of the Shape. Then you can use the Rectangle.contains(...) method of the your Graphics area to determine if the Shape is fully contained within your panel.
Check out Playing With Shapes for more information and ideas.
use Ellipse2D.Float to instanciate an object for example:
Shape circle = new Ellipse2D.Float(100.0f, 100.0f, 100.0f, 100.0f);
basically the parameters,from left to right, are: Height, Width, X of the Top left and Y of the top left, and by keeping the X and Y both greater or equal to zero, your circle will always be visible.
the parameters of the Float(...) are documented for the Ellipse2D.Float in Java SE 7 in
http://docs.oracle.com/javase/7/docs/api/java/awt/geom/Ellipse2D.Float.html

Paddle collision side detection using Graphics

I've implemented a kind of Pong where the paddles (Rectangle2D) can rotate.
To obtain more accuracy, many things are managed by Graphics2D.
The rotation too is managed by the methods rotate(...) of the previous told class.
To reach a realistic bounce, I need to know where the ball hits the paddle (only the side, not the particular point).
I've tried to define (and rotate) two Rectangle2D that represent the back and the front side of the paddle and then recognize the bounce in one of these two by the method hit(Rectangle2D r, Shape s, boolean onStroke), but it doesn't work properly.
Here is the java class Graphics2D:
http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics2D.html
Have you any idea?
Assuming you have one rectangle for your paddle and know the center of rectangle and circle and the rotation of your rectangle.
Assuming a rotation of zero means, your rectangle is aligned horizontally (width > height).
Calculate the difference vector (center of circle) - (center of rectangle)
Get the angle of that vector and subtract the rotation of your rectangle (angle of vector is Math.atan2(y, x))
The resulting value a tells you the relative direction of your circle
Make sure a lies between 0 and 2*pi
q = Math.atan2(height of rectangle, width of rectangle)
If a is between q and pi-q, your circle has hit on the front long (upper) side.
If a is lower than q or bigger than 2*pi-q it's on the right side.
If a is between pi-q and pi+q it's on the left side.
If a is between pi+q and 2pi-q it's hit the bottom of your rectangle

Categories