Cross product and box volume

The cross product turns two 3D vectors into a third that is perpendicular to both, and it measures area and volume.

What to build

For three vectors in 3D, print a × b, the area of the parallelogram a and b span, and the signed volume of the box all three span.

Requirements

  • Compute the cross product a × b.
  • Compute the area as the length of a × b.
  • Compute the signed volume c · (a × b).

Input

Three lines, each with three whole numbers: the vectors a, b and c.

Output

Three lines: cross X Y Z (whole numbers), area A (to 4 decimal places) and volume V (a whole number, possibly negative).

Constraints

The entries are between -20 and 20.

Example

Input:

1 0 0
0 1 0
0 0 1

Output:

cross 0 0 1
area 1.0000
volume 1

Why it works

The cross product is perpendicular to both inputs and its length is |a||b|sin θ, the area of their parallelogram. The dot product of c with it adds the height of c above that parallelogram, so it equals the box volume; its sign tells whether a, b, c follow the right-hand rule.

Examples

Example 1 · Input
1 0 0
0 1 0
0 0 1
Expected output
cross 0 0 1
area 1.0000
volume 1
Example 2 · Input
1 2 3
4 5 6
7 8 10
Expected output
cross -3 6 -3
area 7.3485
volume -3

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

(a × b) = (a2·b3 − a3·b2, a3·b1 − a1·b3, a1·b2 − a2·b1).

Hint 2

The parallelogram area is the length of the cross product; the volume is the dot product c · (a × b).

Hint 3

Print the area with `:.4f`; the cross product and the volume are whole numbers.

Solution

Show one correct solution (try it yourself first)
from math import sqrt

a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
c = [int(x) for x in input().split()]
cross = [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]]
area = sqrt(sum(v * v for v in cross))
volume = sum(x * y for x, y in zip(c, cross))
print("cross", *cross)
print(f"area {area:.4f}")
print("volume", volume)

▶ Open this solution in the compiler, then press Visualize to watch it run step by step

Lesson: Vectors and the dot product →