Position between two points? - geometry

I have a python app where I need to find a position that is exactly in the middle between two screen coordinates, but I can't seem to find an algorithm to do this. How can this be accomplished?

X coordinate is (x1 + x2) / 2
y coordinate is (y1 + y2) / 2

You want to find the midpoint of a line.
Here is a little article to explain the math behind it.
http://regentsprep.org/regents/math/midpoint/Lmidpoint.htm (Dead Link)
https://en.wikipedia.org/wiki/Midpoint
Basically your algorithm will look like this:
midX = (x1 + x2) / 2
midY = (y1 + y2) / 2

This is elementary geometry:
point1(x1,y1)
point2(x2,y2)
point_in_the_middle(x=(x1+x2)/2,y=(y1+y2)/2)
Or did you mean something else ?
dmckee: Yes dear! :)

The middle point (C) should be the average of the two points (A, B):
Cx = (Ax + Bx) / 2
Cy = (Ay + By) / 2

Related

Transform a triangle on a plane

There is a triangle with points P1(x1,y1),P2(x2,y2),P3(x3,y3) on an XY plane.
The final position after transformation are known to us, P1'(x,y) and P2'(x,y)
How can I find the third point?
Using slope (or distance) formula gives two solutions (one is mirror image of another). Assuming the transformation is a combination of translation and rotation, how do I get the new coordinates of the final point P3' ?
If you already have got solution using distance formula, you need only to choose what mirror point is needed. To clarify, find sign of cross product of P1P2 vector and P1P3 vector. Then find sign of cross product of of P1'P2' vector and P1'Px vector. If signs differ, get another point.
CrossProduct = (P2.X - P1.X) * (P3.Y - P1.Y) - (P2.Y - P1.Y) * (P3.X - P1.X)
In general case you can find transformation matrix coefficients and apply this matrix to the third point
c -s 0
M = s c 0
dx dy 1
equation system
c * x1 + s * y1 + dx = x1'
-s * x1 + c * y1 + dy = y1'
c * x2 + s * y2 + dx = x2'
-s * x2 + c * y2 + dy = y2'
solve it for unknown c, s, dx, dy (really c and s are not independent)

Spectrogram of two audio files (Added together)

Assume for a moment I have two input signals f1 and f2. I could add these signals to produce a third signal f3 = f1 + f2. I would then compute the spectrogram of f3 as log(|stft(f3)|^2).
Unfortunately I don't have the original signals f1 and f2. I have, however, their spectrograms A = log(|stft(f1)|^2) and B = log(|stft(f2)|^2). What I'm looking for is a way to approximate log(|stft(f3)|^2) as closely as possible using A and B. If we do some math we can derive:
log(|stft(f1 + f2)|^2) = log(|stft(f1) + stft(f2)|^2)
express stft(f1) = x1 + i * y1 & stft(f2) = x2 + i * y2 to write
... = log(|x1 + i * y1 + x2 + i * y2|^2)
... = log((x1 + x2)^2 + (y1 + y2)^2)
... = log(x1^2 + x2^2 + y1^2 + y2^2 + 2 * (x1 * x2 + y1 * y2))
... = log(|stft(f1)|^2 + |stft(f2)|^2 + 2 * (x1 * x2 + y1 * y2))
So at this point I could use the approximation:
log(|stft(f3)|^2) ~ log(exp(A) + exp(B))
but I would ignore the last part 2 * (x1 * x2 + y1 * y2). So my question is: Is there a better approximation for this?
Any ideas? Thanks.
I'm not 100% understanding your notation but I'll give it a shot. Addition in the time domain corresponds to addition in the frequency domain. Adding two time domain signals x1 and x2 produces a 3rd time domain signal x3. x1, x2 and x3 all have a frequency domain spectrum, F(x1), F(x2) and F(x3). F(x3) is also equal to F(x1) + F(x2) where the addition is performed by adding the real parts of F(x1) to the real parts of F(x2) and adding the imaginary parts of F(x1) to the imaginary parts of F(x2). So if x1[0] is 1+0j and x2[0] is 0.5+0.5j then the sum is 1.5+0.5j. Judging from your notation you are trying to add the magnitudes, which with this example would be |1+0j| + |0.5+0.5j| = sqrt(1*1) + sqrt(0.5*0.5+0.5*0.5) = sqrt(2) + sqrt(0.5). Obviously not the same thing. I think you want something like this:
log((|stft(a) + stft(b)|)^2) = log(|stft(a)|^2) + log(|stft(b)|^2)
Take the exp() of the 2 log magnitudes, add them, then take the log of the sum.
Stepping back from the math for a minute, we can see that at a fundamental level, this just isn't possible.
Consider a 1st signal f1 that is a pure tone at frequency F and amplitude A.
Consider a 2nd signal f2 that is a pure tone at frequency F and amplitude A, but perfectly out of phase with f1.
In this case, the spectrograms of f1 & f2 are identical.
Now consider two possible combined signals.
f1 added to itself is a pure tone at frequency F and amplitude 2A.
f1 added to f2 is complete silence.
From the spectrograms of f1 and f2 alone (which are identical), you've no way to know which of these very different situations you're in. And this doesn't just hold for pure tones. Any signal and its reflection about the axis suffer the same problem. Generalizing even further, there's just no way to know how much your underlying signals cancel and how much they reinforce each other. That said, there are limits. If, for a particular frequency, your underlying signals had amplitudes A1 and A2, the biggest possible amplitude is A1+A2 and the smallest possible is abs(A1-A2).

Calculate Bezier AABB

I'd like to calculate the AABB (axis aligned bounding box) for a quadratic or bezier curve.
The only way I know how to do this is evaluating a high number of points on the bezier curve, and then use those points to calculate the AABB.
Is there a better way?
Great resource on Bezier curves, and working example of AABB http://pomax.github.io/bezierinfo/#boundingbox
For quadratic curve if you need it, also calculate this using derivatives.
Always avoid iterative methods when closed form is available.
It should be possible by looking for minimum and maximum thanks to the derivative of the curve in parametric form. have a look at this article: http://nishiohirokazu.blogspot.jp/2009/06/how-to-calculate-bezier-curves-bounding.html
The quadratic bezier curve consists of 2 coordinate functions — x(t) and y(t) where.
These functions may have maximum or minimum (the points where x'(t) = 0 and y'(t) = 0) and these points are the boundary points of the aabb.
So the algorithm is:
Imagine x0, y0, x1, y1, x2, y2 are known and calculate the values t(x0, x1, x2) and t(y0, y1, y2) when x'(t) = 0 and y'(t) = 0 respectively.
Calculate both values and check whether they are >= 0 and <= 1. If they are evaluate the points of the quadratic bezier.
Take the first and the last points.
Now you have 4 points (or maybe less), use them to calculate AABB.
By the way:
t(x0, x1, x2) = (x0 - x1) / (x2 - 2 * x1 + x0)
t(y0, y1, y2) = (y0 - y1) / (y2 - 2 * y1 + y0)
You can find the full code here: https://github.com/keyten/Graphics2D/blob/Delta/Core/Curve.Math.js#L295

Draw 2 parallel lines

How can I calculate the points to draw 2 parallel lines.
I know the start and end points for the centre of the parallel lines. To makes thing a little bit harder, it needs to support straight and Bezier curved lines.
The question is vague, but here's a possibility. Hope that helps.
For a segment (x1,y1)-(x2,y2) you can calculate another segment, n pixels away in a direction represented by angle a this way
x1b = x1 + n cos a
y1b = y1 - n sin a
x2b = x2 + n cos a
y2b = y2 - n sin a

How to set up quadratic equation for a ray/sphere intersection?

I'm researching the math for a ray tracer, but I'm not following a transition that is made in just about every article I've read on the subject. This is what I have:
Formula for a sphere:
(X - Cx)^2 + (Y - Cy)^2 + (Z - Cz)^2 - R^2 = 0
Where R is the radius, C is the center, and X, Y, Z are all the points in the sphere.
Formula for a line:
X + DxT, Y + DyT, Z + DzT
where D is a normalized direction vector for the line and X, Y, Z are all the points on the line, and T is a parameter for some point on the line.
By substituting the components of the line into the sphere equation, we get:
(X + DxT - Cx)^2 + (Y + DyT - Cy)^2 + (Z + DzT - Cz)^2 - R^2 = 0
I follow everything up to that point (at least I think I do), but then every tutorial I've read makes a jump from that to a quadratic equation without explaining it (this is copied from one of the sites, so the terms are a little different from my example):
A = Xd^2 + Yd^2 + Zd^2
B = 2 * (Xd * (X0 - Xc) + Yd * (Y0 - Yc) + Zd * (Z0 - Zc))
C = (X0 - Xc)^2 + (Y0 - Yc)^2 + (Z0 - Zc)^2 - Sr^2
I get how to then solve for T using the quadratic formula, but I don't understand how they get to the quadratic equation from the above formulas. I'm assuming that's just some piece of common math knowledge that I've long since forgotten, but googling for "How to set up a quadratic equation" hasn't really yielded anything either.
I'd really like to understand how to get to this step before moving on, as I don't like writing code I don't fully grasp.
Here's a detailed walkthrough of each step; hopefully this will make things crystal clear. The equation for a three-dimensional sphere is:
(x-a)^2 + (y-b)^2 + (z-c)^2 = r^2
with <a, b, c> being the center of the sphere and r its radius. The point <x, y, z> is on the sphere if it satisfies this equation.
The parametric equations for a ray are:
X = xo + xd*t
Y = yo + yd*t
Z = zo + zd*t
where <xo, yo, zo> is the origin of the ray, and <xd,yd,yd> is camera ray's direction.
To find the intersection, we want to see what points on the ray are the same as points on the sphere. So we substitute the ray equation into the sphere equation:
(xo + xd*t - a)^2 + (yo + yd*t - b)^2 + (zo + zd*t - c)^2 = r^2
which expands to:
(xd^2 + yd^2 + zd^2) * t^2 +
[2[xd * (xo - a) + yd * (yo - b) + zd *(zo - c)]] * t +
[(xo - a)^2 + (yo - b)^2 + (zo - c^)2 - r^2] * 1
= 0
Notice that this is a quadratic equation in the form At^2 + Bt + C = 0, with:
A = (xd^2 + yd^2 + zd^2)
B = [2[xd * (xo - a) + yd * (yo - b) + zd *(zo - c)]]
C = [(xo - a)^2 + (yo - b)^2 + (zo - c^)2 - r^2]
We can apply the general quadratic formula for an unknown variable, which is:
t = [-B +- sqrt(B^2 - 4AC)] / 2A
The B^2 - 4AC portion is called the "discriminant". Depending on the value of the discriminant, we will get zero, one, or two solutions to this equation:
If it is less than zero, the solution is an imaginary number, and the ray and sphere do not intersect in the real plane.
If it is equal to zero, then the ray intersects the sphere at exactly 1 point (it is exactly tangent to the sphere).
If it is greater than zero, then the ray intersects the sphere at exactly 2 points.
If the discriminant indicates that there's no solution, then you're done! The ray doesn't intersect the sphere. If the discriminant indicates at least one solution, you can solve for t to determine the intersection point. The two solutions are:
t_1 = [-B + sqrt(B^2 - 4AC)] / 2A
t_2 = [-B - sqrt(B^2 - 4AC)] / 2A
The smaller solution is the point at which the ray first hits the sphere.
From here:
(X + DxT - Cx)^2 + (Y + DyT - Cy)^2 + (Z + DzT - Cz)^2 - R^2 = 0
Expand the three squared terms, so you have a long expression:
X^2 + Dx^2T^2 + Cx^2 + 2XDxT - 2XCx - 2DxTCx + ...... = 0
(This is due to using the formula (x+y+z)^2 = x^2 + y^2 + z^2 + 2xy + 2xz + 2yz)
Then group so you have factors of T^2, T, and 1:
(....)T^2 + (....)T + .... = 0
These factors are the A,B,C given above. This is a quadratic equation for T, and can be solved using the quadratic formula.

Resources