A two-dimensional projection of a
three-dimensional cube.
Although we can easily perceive the 3d cube, the
.gif above is really a series of still images, each
with some lines of different lengths.
When working with geometric objects, it is often incredibly useful to know how certain objects appear from other perspectives. For example, when determining the shape and direction of shadows from a light source, or displaying multi-dimensional objects using fewer dimensions (as in the animated example above).
Observing the animated cube above, it should easy to understand that while in 3d-space the edge-lengths of the cube are constant, the lengths of the 2d lines representing each edge may change, depending on the relative orientation of that edge and the "view" (the axis of your line-of-sight). When the edge is perpendicular to the view, we can see its entire length - however when the edge is more parallel to the view, the length appears shorter.
Let's start breaking down the math. In a general
case where an "edge" is described by some vector
a and the "view" is described by some vector b,
then the length of a projected onto b is
$$ \large proj(\overrightarrow{a}, \overrightarrow{b}) = \overrightarrow{a} \cdot \hat{b} $$
Note that the "dot multiplication" symbol is referring to the vector dot product, which is defined as
$$ \large \overrightarrow{a} \cdot \overrightarrow{b} = a_{x} b_{x} + a_{y} b_{y} + a_{z} b_{z} $$
Additionally, the b vector with the "hat" denotes
the Unit Vector of b, which is the vector
uniformly scaled such that its total length is
equal to 1, given as
$$ \large \hat{b} = \frac{\overrightarrow{b}}{\lVert \overrightarrow{b} \rVert} $$
The "absolute value" of b should be understood as
the Magnitude of b, which refers to its total
length. It can be quickly calculated with the
equation
$$ \large \lVert \overrightarrow{a} \rVert = \sqrt{\overrightarrow{a} \cdot \overrightarrow{a}} $$
For visual clarity of what is going on:
a1 is length of the projection of a onto b
Also understand that if the vectors a and b form an angle greater than 90°, then their resulting
dot product will be negative.
Input Data
First line will be Q, the quantity of testcases.
Q lines will then follow, each in the format ax ay az bx by bz, describing
to two 3d vectors a and b.
Answer
Should consist of Q space-separated values, corresponding to the length of the
projection of a
onto b for each testcase.
Error should be less than 1e-6
Example
input data:
2
1.2 2.3 3.4 9.8 8.7 7.6
-1.3 -3.5 -5.7 2.4 4.6 6.8
answer:
3.802910 -6.778621