Calculate Bounding Rect Given Two Aspect Ratios - math

I have two rectangles, each with fixed aspect ratios. Given a width of x how do I calculate the height of a rectangle that would enclose the two rectangles without leaving any gaps?

You want to solve the following equations for h:
r1 = w1 / h
r2 = w2 / h
w = w1 + w2
The solution is:
h = w / (r1 + r2)

Related

Draw a square with grid in ggplot2

I am trying to create my own geom. On screen, the geom should always be a square, irrespective of scaling. Given the values w and h (for width and height), transformed by ggplot from user coordinates to grid coordinates, the idea is to shrink one of them such that on screen, the aspect of the resulting rectangle is exactly w/h. If w==h, we get a square.
It is doable in basic R by consulting par("pin"):
## w and h are box width and height in user coordinates
## if w == h, we will should get a square
aspect <- 1 # change to get a rectangle with a given aspect ratio
pp <- par("pin")
pu <- par("usr")
pu <- c(pu[2]-pu[1], pu[4]-pu[3])
ppu <- pp/pu
wi <- w * ppu[1] # box width in inches
hi <- h * ppu[2] # box height in inches
if(wi > hi * aspect) {
wi <- hi * aspect
} else {
hi <- wi / aspect
}
w <- wi / ppu[1]
h <- hi / ppu[2]
However, I have hard time figuring out how to do the same with grid. Is it possible?
What I want to do: I want to make a little plot-in-plot which, in some cases, needs to be exactly a square otherwise it looks ugly.

Estimate visible bounds of webcam using diagonal fov

I'm using a Logitech C920 webcam (specs here) and I need to estimate the visible bounds of it before installing it at the user place.
I see that it has a Diagonal FOV of 78°. So, following the math described here we have:
Where H is the vertical Fov, W is the horizontal Fov, D is the diagonal Fov and the aspect ratio is r.
Considering an aspect ratio of 16/9, that gives me approx. W = 67.9829 and H = 38.2403
So I create a frustum using W and H.
The problem is: a slice of this frustum isn't 16:9. Is it due because of the numeric approximations or I'm doing something else wrong?
Does the camera crop a bigger image?
How can I compute effectively what will be the visible frustum?
Thank you very much!
The formulae you have are for distances, not for angles. You would need to calculate the distance using tangens:
D = 2 * tan(diagonalFov / 2)
Then you can go ahead with your formula. H and W will again be distance values. If you need the according angles, you can use arc tan:
verticalFov = 2 * arc tan (H / 2)
horizontalFov = 2 * arc tan (W / 2)
For your values, you'll get
verticalFov = 43.3067°
horizontalFov = 70.428°

How to implement surface calculation of a elliptical cone and elliptical paraboloid (in R)?

How can I calculate the surface of an elliptical paraboloid in R, having only the two major axes a and b, and the height h?
Background: got a population of things which which resemble elliptical paraboloids. (Think of hummocks, e.g.). What I'm statistically interested in is: how large is the mean and SD error I introduce when I approximate the surfaces of the population as rectangular (a * b) instead of properly modelling their surface? I could just go for a cone, but I've got some really long ellipses in that population of data. This made me look for how to calculate an elliptical paraboloid, and and elliptic cone. Which did not help me answering my question so far.
I can't translate the neither Wolfram nor Wikipedia properly, just started for the cone with
paraboloid.surf <- function(a,b,h){2*a*sqrt(b^2+h^2)* E * sqrt((1-b^2/a^2)/(1+b^2/a^2))}
without knowing how to calculate E, or even understanding if this is E multiplied by the term, or a function E(k) of k = the term.
Can somebody help me out and provide some code? I would be interested in both the conical as the parabolical solution, but any is better than none. I hope this is really non-trivial, and I'm not totally stupid. =)
So I assume you want the surface area of an elliptic paraboloid. The basic framework is given here, from which the following image is taken.
So the question is how to compute this in R? The hardest part is the double integral, which can be evaluated using the adaptIntegrate(...) function in the cubature package.
A <- function(a,b,h) {
require(cubature)
integrand <- function(x,a,b){
u <- x[1]
v <- x[2]
E <- 1+((a*cos(v))^2 + (b*sin(v))^2)/(4*u)
F <- (b^2 - a^2)*sin(2*v)/4
G <- u*((a*sin(v))^2+(b*cos(v))^2)
return(sqrt(E*G-F^2))
}
adaptIntegrate(integrand,
lowerLimit=c(0,0), upperLimit=c(h,2*pi), a=a,b=b)$integral
}
We can confirm this by noting that when
a = b = h = 1
E = 1 + 1/4u
F = 0
G = u
A = 2π ∫du sqrt(u+1/4)
Which can be evaluated in closed form as:
A = 4π/3 [ (5/4)3/2 - (1/4)3/2 ] = 5.330414
A(1,1,1)
# [1] 5.330413
(4*pi/3)*((5/4)^(3/2) - (1/4)^(3/2))
# [1] 5.330414
From https://en.wikipedia.org/wiki/Elliptic_paraboloid the defining equation for an elliptic parabola is z/c = x^2/a^2 + y^2/b^2. We can transform this to make a hill with height 'h' and base at z=0 being an ellipse.
For a elliptic parabola
z = h*(1 - ((x/a)^2+(y/b)^2)).
Where a and b are the semi-major and semi-minor axis of the ellipse generated by cutting the cone by z=0 and h is the height of the point above (0,0).
For a elliptical cone
z = h*(1 - sqrt((x/a)^2+(y/b)^2))`.
Where a and b are the semi-major and semi-minor axis of the ellipse generated by cutting the cone by z=0.
To plot both you need to clip by the plane z=0 use max(0,h*(1 - ((x/a)^2+(y/b)^2)).
Volumes
The area of an ellipse is pi a b, the volume of a cone is 1/3 basearea * h so volume of the cone is
1/3 pi a b h.
As mentioned elsewhere the volume of the elliptic paraboloid is a bit tricky.
We can try doing it by slicing in the z-direction.
int_{z=0}^{h} area of ellipse at height z dz.
If the ellipse at height z has semis a(z) and b(z) then the area is
A(z) = pi a(z) b(z).
To find the a(z) and b(z) slice the paraboloid by the planes y=0, x=0, these give two parabolas z = h *(1 - (x/a)^2), z = h *(1 - (y/b)^2). Rearrange z/h = 1 - (x/a)^2, (x/a)^2 = 1 - z/h x/a = sqrt(1-z/h) x=a*sqrt(1-z/h) this is our a(z). Similary b(z)=b*sqrt(1-z/h). The area of the ellipse at height z is
A(z) = pi a(z) b(z)
= pi a sqrt(1-z/h) b sqrt(1-z/h)
= pi a b (1-z/h)
Integrate
int_{z=0}^h pi a b (1-z/h)
= pi a b [z - z^2/(2 h)]_0^h
= pi a b (h - h^2/(2 h))
= 1/2 pi a b h
remarkably simple.
For comparison the volume of the cuboid with height h and lengths 2a and 2b would be
4 a b h.
Surface areas
From https://en.wikipedia.org/wiki/Elliptic_integral#Complete_elliptic_integral_of_the_second_kind the circumference of a ellipse with semi-major axis a and eccentricity e is
4 a E(e)
where E(e) is a complete Elliptical integral of the second kind. The eccentricity e is
e = sqrt( 1 - b^2/a^2 ).
In both the cone and elliptic paraboloid cases the eccentricity is constant for all horizontal slices. The integrals for surface areas then simplify nicely. For the cone
a(z) = a * ( h - z). So
int_z 4 a(z) E(e) dz
= 4 E(e) int_z a(z) dz
= 4 E(e) int_{z=0}^h a * (h -z ) dz
= 4 a E(e) int_{z=0}^h h - z dz
= 4 a E(e) [h z - z^2/2]_{z=0}^h
= 4 a E(e) h^2/2
= 2 a E(e) h^2
For the elliptic paraboloid a(z)=a*sqrt(1-z/h) and the integral is
int_z 4 a(z) E(e) dz
= 4 E(e) int_z a(z) dz
= 4 E(e) int_{z=0}^h a * sqrt(1-z/h) dz
change variables with w=1-z/h limits are 1 and 0 dz/dw = -h so
= 4 E(e) int_{w=1}^0 a * sqrt(w) * -h dw
= 4 a E(e) h int_{w=0}^1 sqrt(w) dw // switching limits round
= 4 a E(e) h [ 2 w^(3/2) /3]_{w=0}^1
= 8/3 a E(e) h.
Multivariate normal distribution
An alternative and more standard model is as a 2D gaussian https://en.wikipedia.org/wiki/Multivariate_normal_distribution#Density_function
z = 1/(2 pi a b) exp( -( (x/a)^2 + (y/b)^2 )/2).
This will look pretty close to the paraboloid apart from at the base.

Find Corners of Rectangle, Given Plane equation, height and width

Essentially I want to make a rectangular plane face an object at all times.
1) I have found my plane equation (ax + by + cz + d = 0)
2) I have the center point of the rectangle (P0 = (x0,y0,z0)), which lays on the plane.
3) I have the width and Height of the rectangle. (W, H)
4) I know that the top two corners of the rectangle will have equal Y values, this goes for bottom 2 corners also. (Y is my up and down axis, the rectangle top and bottom lines will always be parallel to the x,z plane)
Does anyone know how to find the x,y,z values of the four corners C1,C2,C3,C4?
Compute the vector from your plane center to the object you want to face. Call that vector V. Then normalize(V) = (a, b, c) and d = - a*x0 - b*y0 - c*z0. You have the equation for your plane.
Now you can rotate the plane however you want. If you to have the plane to have 0 roll (that is, only ever modify yaw and pitch), you can take the normalized cross product of the world "up" vector (0,0,1) and normalize(V) to get the horizontal vector U for the rectangle. Take the normalized cross product of normalize(V) and U to get the vertical vector W for the rectangle.
The corners of your rectangle are now:
C1 = P0 + (width / 2) * U + (height / 2) * W
C2 = P0 + (width / 2) * U - (height / 2) * W
C3 = P0 - (width / 2) * U + (height / 2) * W
C4 = P0 - (width / 2) * U - (height / 2) * W
Note that this approach has a singularity when the rectangle is directly above or below the object it is supposed to face. You should check for that if appropriate and handle it however makes sense in your scenario.

Finding points of intersection when two spheres intersect

I have the center (xyz - in 3 dimensional space) and the radius of two spheres A and B.
Now I have to figure out a point or more than 1 point where these spheres meet. It is fairly easy to figure out if the two spheres collide or not, but how do I find out the points of intersection of 2 spheres?
Any help would be greatly appreciated.
The curve where they intersect is a circle. The equation for the radius of the circle is a bit complicated, but is shown here, in eqn. 8, and this distance of the circle from the center of one of the spheres is shown in eqn. 5.
If the radius of the smaller sphere is A, and the bigger is B, and their centers are D units apart, then the points of intersection are on a circle of radius r centered on a point directly between the centers of the two spheres, which is y units from the center of the bigger sphere, and x units from the center of the other, where
y = 1/2 (D + (B^2 - A^2)/D)
and
x = 1/2 (D - (B^2 - A^2)/D)
with radius
r = B^2 - x^2 = A^2 - y^2
If you need the equation for this circle the best way is to represent it as a set of three parameterized equations, where the x, y, and z coordinates are each expressed a a function of some t, which represents the radius vector trraveling around the circle once, from zero to 2PI...
To construct these equations, think about expressing the point which is the radius r from the center, on the 2D plane which is normal to the line between the two spheres.
Check out this link for some iedas on how to do this..
Derivation is as follows: draw a line between the centers of the two spheres. Label it as D
Designate a point on this line as the center of the final solution circle label it as point O
Label the smaller portion of D as x, and the large portion as y
draw a line from O perpindicular to D, for some distance r to represent the radius of the solution circle
Label the end of this radius as Q
Now draw B between the center of the larger sphere to Q and A from the center of smaller sphere and Q
From Pythagoras:
B^2 = y^2 + r^2 and A^2 = x^2 + r^2
so, after eliminating r and a bit of algebra,
y-x = (B^2 - A*2) / (x+y)
But x+y = D so,
y-x = (B^2 - A*2) / D
Adding the equation x+y=D to the above eliminates the x, giving
2y = D + (B^2 - A*2) / D
or,
y = 1/2 ( D + (B^2 - A*2) / D )

Resources