How to make a circle/rectangle collision detection and response? - geometry

I have searched a lot about doing collisions between a circle and a rectangle, but I only found how to detect it.
Can someone explain me how to do it ?
Then, how can can I do it too when the rectangle is inclined ?
This is the values that I got :
Circle X position
Circle Y position
Circle radius
Rectangle X position
Rectangle Y position
Rectangle width
Rectangle height
Anyone know how to do it ? Thanks.

Related

itext7 how to consider rotation angle in TextPlusYExtractionStrategy

The text positioning (y) is determined by the TextPlusYExtractionStrategy perfectly when the rotation angle is 0 (portrait). In case of rotation angle is 90 degree (landscape) the position y provided from left to right instead of top to bottom. How to consider the rotation to get the text position y in when the page is 90 degree.
The below code helped me to resolve my issue.
if(rotation == 90){
chunkY = chunk.getLocation().getStartLocation().get(Vector.I1);
}
Constructor of TextPlusYExtractionStrategy can take the rotation angle, and use the Vector.I1 which is x co-ordinate of the chunk.
The issue will solve only the landscape (rotation=90) issue to find the Y coordinate of the search text.

SVG animations - how to keep text horizontal while its parent rotates around a center point

I have the following animation:
I want the circle containing the ETH / USD animation to rotate around the centerpoint of all these circles, but the text itself I want to stay perfectly horizontal. How do I do this?
If it helps, I'm using the svg.js library (https://github.com/svgdotjs/svg.js)
Rotate the circle counterclockwise around its own center whiile it is rotating clockwise around the main center.
Let group be a group containing the small circle with radius r and the text. Let them be centered on (0,0). Let (cx, cy) be the central rotation point. Loop endlessly with even pace in a full circle taking 15s:
var group = draw.group();
group.circle(r, 0, 0);
group.text('ETH / USD');
group.animate(15000, '-').rotate(-360).loop()
.animate(15000, '-').rotate(360, cx, cy).loop();

Determine if tap is within circular area

In my app I am currently able to work out whether the user's tap is within a rectangular area simply by checking all of the following conditions are true:
Finger X > rectangle X
Finger Y > rectangle Y
Finger X < rectangle X + rectangle Width
Finger Y < rectangle Y + rectangle Height
However, I now have to determine if the user taps within a circular area. Currently I have a circular shape on screen and have resorted to just checking it's bounding rectangle, which works but obviously isn't great.
Any help would be appreciated.
The distance between two points in two dimensions is defined as
dist = sqrt((x2-x1)^2 + (y2-y1)^2)
To check if your tap point is inside a circle, take the centre of your circle as (x1,y1), and the 'tap location' as (x2,y2), and check if
sqrt((x2-x1)^2 + (y2-y1)^2) < R
With R being the radius of your circle.
Edit:
As John mentioned, from a computational point of view it is more interesting to compare vs R^2, to avoid the sqrt for every tap. So the condition becomes:
(x2-x1)^2 + (y2-y1)^2 < R^2

Find size of inner rect of a circle

I have a circle, say radius of 10, and I can find the outer bounding rect easy enough since its width and height is equal to the radius, but what I need is the inner bounding rect. Does anyone know how to calculate the difference in size from the outer and inner bounding rectangles of a circle?
Here's an image to illustrate what I'm talking about. The red rectangle is the outer bounding box of the circle, which I know. The yellow rectangle is the inner bounding rectangle of the circle, which I need to find the difference in size from the outer rectangle.
My first guess to find the difference is to find one of the four points of the inner rectangle by finding that point along the circumference of the circle, each point being at a 45 degree offsets, and then just find the different from that point and the related point in the larger rect.
EDIT: Based off of the solution given by Steve B. I've come up with the algorithm to get what I want which is the following:
r*2 - sqrt(2)*r
If the radius is r, the outer rectangle size will be r*2.
The inner rectangle will have size equals to 2*sqrt(2*r).
So the diff will be equals to 2*(r-sqrt(2*r^2)).
You know the size of the radius and you have a triangle with a corner of 90 degrees with one point as the center of your circle and another two as two corners of your inner square.
Now if you know two sides of a triangle you can use Pythagoras:
x^2 = a^2 + b^2
= 2* r^2
So
x = sqrt(2 * r^2)
With r the radius of the circle, x the side of the square.
It's simple geometry: Outer rectangle has length of edge equal to 2*R, inner - diagonal equal to 2*R. So the edge of inner rectangle is equal to sqrt(2)*R. The ratio of edges of outer rectangle divided by inner is obviously sqrt(2).

How to convert from one co-ordinate system to another (graphics)

I've been having issues with this for a little while now. I feel like I should know this but I can't for the life of me remember.
How can I map the screen pixels to their respective 'graphical' x,y positions? The co-ordinate systems have been configured to start at the bottom left (0,0) and increase to the top-right.
I want to be able to zoom, so I know that I need to configure the zoom distance into the answer.
Screen
|\ Some Quad
| \--------|\Qx
| \ Z | \
| \ \|Qy
\ |
Sx\ |Sy
\|
I want to know which pixels on my screen will have the quad on it. Obviously as Z decreases, the quad will occupy more of the screen, and as Z increases it will occupy less, but how exactly are these calculated?
For further clarification, I want to know how I can map these screen pixels onto the 'graphical' co-ordinates using the zoom factor into the equation.
Thanks for any help.
Use the zoom factor as a multiplier against the coordinates and/or screen size.
For example, if you have a 100x150 pixel square, when zoomed in to 150%, the size of the rectangle should be 150x225.
An equation for this is:
h = height
w = width
z = percent zoom
(100% = 1.00)
new width = W = wz
new height = H = hz
To map screen pixels, apply more basic mathematical principles. The relative coordinates depend entirely on the center of the zoom. This is very easy, if everything zooms in the exact center. If zooming from elsewhere (e.g. stretching the object from a corner or a non-central coordinate), you must apply an offset to your equation.
Zooming a rectangle from its center point is easy. Divide the difference in rectangle width by 2, and then add it to the left and right coordinate value (you can add a negative number). Do the same for height.
If zooming the rectangle from a coordinate that is NOT in its exact center, but is still within the bounds of the rectangle, requires an offset. Simply determine what percentage of height and width change should be applied to each side of the rectangle. Sides in closer proximity to the zoom point will receive a lower percentage of the change.
When the zoom point resides outside the rectangle, the distance from the zoom point must also be taken into account. This offset moves the entire rectangle, in addition to scaling the rectangle.
Get a large piece of paper and draw up some visualizations. That always helps. =)
If (xk, yk) is the center before zooming and the size is (Sx, Sy), zoomed to a factor of Z in (0, 1], the new size will be (Qx, Qy) = (Sx*(1-Z), Sy*(1-Z)) centered on (xk, yk) which means the screen coordinates are:
rectangle: xk - Qx/2, yk - Qy/2, xk + Qx/2, yk + Qy/2
Hope that helps.

Resources