Intersection between arc and rectangle - geometry

Say that there is an arc and a rectangle.
The arc has a position, radius, minimum and maximum angles, and the width of the arc itself. The rectangle has a position, width and height, and rotation.
How would one determine whether the arc and rectangle are intersecting?
Provided is a visual aid that may increase clarity. The green rectangles are those that would be considered to be intersecting, while the red rectangles are not intersecting.
I have determined that it is common to check intersections on each line segment of the rectangle individually, but I am not yet certain as to how one would account for the rectangle being on the inside of the arc but not close enough to be intersecting it.

For rectangles being completely inside the arc you can check - whether any corner point (x, y) belongs to thick arc. For arc center (cx, cy), inner and outer radii r and R and angles a0,a1:
dist = length(x - cx, y - cy)
if dist lies in range r..R:
angle = atan2(y-cy, x-cx)
if angle in range a0..a1:
rectangle is inside
One more non-standard case: to find whether arc completely lies inside large rectangle - just check if any point of arc lies in that rectangle.

Related

Having the coordinates of the two triangles of a twisted triangle prism, how can I know if a point is inside it?

Here some examples of twisted triangle prisms.
I want to know if a moving triangle will hit a certain point. That's why I need to solve this problem.
The idea is that a triangle with random coordinates becomes the other random triangle whose vertices all move between then
related: How to determine point/time of intersection for ray hitting a moving triangle?
One of my students made this little animation in Mathematica.
It shows the twisting of a prism to the Schönhardt polyhedron.
See the Wikipedia page for its significance.
It would be easy to determine if a particular point is inside the polyhedron.
But whether it is inside a particular smooth twisting, as in your image, depends on the details (the rate) of the twisting.
Let's bottom triangle lies in plane z=0, it has rotation angle 0, top triangle has rotation angle Fi. Height of twisted prism is Hgt.
Rotation angle linearly depends on height, so layer at height h has rotation angle
a(h) = Fi * h / Hgt
If point coordinates are (x,y,z), then shift point to z=0 and rotate (x,y) coordinates about rotation axis (rx, ry) by -a(z) angle
t = -a(z) = - Fi * z / Hgt
xn = rx + (x-rx) * Cos(t) - (y-ry) * Sin(t)
yn = ry + (x-rx) * Sin(t) - (y-ry) * Cos(t)
Then check whether (xn, yn) lies inside bottom triangle

Outline of a sphere after perspective projection?

I'm working on a 3D mapping application, and I've got to do some work with things like figuring out the visible region of a sphere (Earth) from a given point in space for things like clipping mapped regions and such.
Several things get easier if I can project the outline of Earth into screen space, clip polygons there, and then project back to the surface of the Earth (lat/lon), but I'm lost as to how to do that.
Is there a reasonable way to compute the outline of a sphere after perspective projection, and then a reasonable way to project things back onto the sphere?
You can clip the polygons in 3D. The silhouette of the sphere - back-projected into 3D - will always be a circle on a plane. Perspective projection does not change that. Thus, you can clip all polygons at the plane.
Calculating the plane is not too hard. If you consider the sphere's center the origin, then the plane could be represented in normal form as:
dot(n, x) = d
n is the normal. This one is easy. It is just the unit direction vector from the sphere center to the observer.
d is the distance from the sphere center. This is a bit harder but not too hard. If l is the distance of the observer to the sphere center and r is the sphere radius, then
d = r^2 / l
This is the plane which you can use to clip your polygons in 3D. If you need the radius of the circle on it, you can use the following formula:
r_c = r / sqrt(1 - r^2/(l-d)^2)
Let us take a point on a sphere in spherical coordinates (cos(u)sin(v),sin(u)sin(v),cos(v)) and an arbitrary projection center (x,y,z).
We express that a projecting line is tangent to the sphere by the perpendicularity condition of the direction of the line and the vector from the origin of the sphere:
(x-cos(u)sin(v))cos(u)sin(v) + (y-sin(u)sinv))sin(u)sin(v) + (z-cos(v)) cos(v) = 0
This simplifies to
x cos(u)sin(v) + y sin(u)sin(v) + z cos(v) = 1
which is a curve in the longitude/latitude coordinates. You can solve u as a function of v or conversely.

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.

how to draw circular arc with give two points and radius and clockwise direction

The problem is draw arc with two pints on bitmap with radius and clockwise direction.
From your one-sentence question, I'm gonna assume you're ok with drawing Bezier curves. If not, there is plenty of information about them out there.
Anyway, you cannot create a perfect circular arc with Bezier curves (or splines). What you can do is approximating a circle to a level where the eye won't be able to see the difference. This is usually done with 8 quadratic Bezier curve segments, each covering 1/8th of the circle. This is i.e. how Adobe Flash creates circles.
If you're after a plain parametrization using sin and cos, it's way easier:
for (float t = 0; t < 2 * Math.PI; t+=0.05) {
float x = radius * sin(t);
float y = radius * cos(t);
}

Resources