Java语言: Codee#16213
01
import
java.io.*
;
02
import
java.util.*
;
03
import
java.math.*
;
04
import
java.text.*
;
05
//给定四面体4个点,求体积
06
//学习了解了数量积(点积or内积)写作(a,b)运算结果为数字
07
//向量积(叉积or外积)写作[a,b]运算结果为向量
08
//那么混合积为定义三个向量AB,AC,AD,则其混合积为([AB,AC],AD)(这里A,B,C,D是四面体4个点)
09
//其绝对值除6就是四面体体积
10
//(ab与ac的叉积 点积上 ad)是体积的6倍
11
//(ab与ac的叉积的模)是面积的两倍
12
//有个PPT讲解的很不错,有助于了解混合积数学及物理含义
13
//http://wlkc.zzuli.edu.cn/kejianweb/xiandai/2/2-3.ppt
14
public
class
Main
{
15
final
static
int
maxn
=
4
;
16
public
static
class
Point
{
17
double
x
,
y
,
z
;
18
19
Point
(){};
20
Point
(
double
xx
,
double
yy
,
double
zz
){
21
x
=
xx
;
y
=
yy
;
z
=
zz
;
22
}
23
void
out
(){
24
System
.
out
.
println
(
x
+
" "
+
y
+
" "
+
z
);
25
}
26
27
};
28
29
public
static
double
Dot
(
Point
p
,
Point
a
,
Point
b
){
30
return
(
a
.
x
-
p
.
x
)*(
b
.
x
-
p
.
x
)+(
a
.
y
-
p
.
y
)*(
b
.
y
-
p
.
y
);
31
}
//pa与pb的点积 (2维)
32
33
public
static
double
Cross
(
Point
p
,
Point
a
,
Point
b
){
34
return
(
a
.
y
-
p
.
y
)*(
b
.
x
-
p
.
x
)-(
a
.
x
-
p
.
x
)*(
b
.
y
-
p
.
y
);
35
}
//pa与pb的叉积(2维)
36
37
public
static
double
Cross
(
Point
a
,
Point
b
){
38
return
a
.
y
*
b
.
x
-
a
.
x
*
b
.
y
;
39
}
//向量a与b的叉积(2维)
40
41
public
static
double
Dot
(
Point
a
,
Point
b
){
42
return
a
.
x
*
b
.
x
+
a
.
y
*
b
.
y
;
43
}
//向量a和b的点积(2维)
44
45
46
47
public
static
double
ThreeDot
(
Point
p
,
Point
a
,
Point
b
){
48
return
(
a
.
x
-
p
.
x
)*(
b
.
x
-
p
.
x
)+(
a
.
y
-
p
.
y
)*(
b
.
y
-
p
.
y
)+(
a
.
z
-
p
.
z
)*(
b
.
z
-
p
.
z
);
49
}
//pa与pb的点积 (3维)
50
51
52
public
static
Point
ThreeCross
(
Point
p
,
Point
a
,
Point
b
){
53
Point
C
=
new
Point
();
54
C
.
x
=(
a
.
y
-
p
.
y
)*(
b
.
z
-
p
.
z
)-(
a
.
z
-
p
.
z
)*(
b
.
y
-
p
.
y
);
55
C
.
y
=(
a
.
z
-
p
.
z
)*(
b
.
x
-
p
.
x
)-(
a
.
x
-
p
.
x
)*(
b
.
z
-
p
.
z
);
56
C
.
z
=(
a
.
x
-
p
.
x
)*(
b
.
y
-
p
.
y
)-(
a
.
y
-
p
.
y
)*(
b
.
x
-
p
.
x
);
57
return
C
;
58
}
//pa与pb的叉积(3维)
59
60
public
static
double
ThreeDot
(
Point
a
,
Point
b
){
61
return
a
.
x
*
b
.
x
+
a
.
y
*
b
.
y
+
a
.
z
*
b
.
z
;
62
}
//向量a与b的点积 (3维)
63
64
public
static
Point
ThreeCross
(
Point
a
,
Point
b
){
65
Point
C
=
new
Point
();
66
C
.
x
=
a
.
y
*
b
.
z
-
a
.
z
*
b
.
y
;
67
C
.
y
=
a
.
z
*
b
.
x
-
a
.
x
*
b
.
z
;
68
C
.
z
=
a
.
x
*
b
.
y
-
a
.
y
*
b
.
x
;
69
return
C
;
70
}
//向量a与b的叉积(3维)
71
72
public
static
double
TetrahedronArea
(
Point
a
,
Point
b
,
Point
c
,
Point
d
){
73
double
ans
=
Math
.
abs
(
ThreeDot
(
ThreeCross
(
a
,
b
,
c
),
74
new
Point
(
d
.
x
-
a
.
x
,
d
.
y
-
a
.
y
,
d
.
z
-
a
.
z
)))/
6
;
75
return
ans
;
76
}
77
78
public
static
Point
p
[]=
new
Point
[
maxn
];
79
80
public
static
void
main
(
String
args
[]){
81
Scanner
in
=
new
Scanner
(
System
.
in
);
82
83
int
test
=
in
.
nextInt
();
84
for
(
int
caseid
=
1
;
caseid
<=
test
;
caseid
++){
85
for
(
int
i
=
0
;
i
<
maxn
;
i
++){
86
double
x
=
in
.
nextDouble
();
87
double
y
=
in
.
nextDouble
();
88
double
z
=
in
.
nextDouble
();
89
p
[
i
]=
new
Point
(
x
,
y
,
z
);
90
}
91
92
93
DecimalFormat
g
=
new
DecimalFormat
(
"0.000"
);
94
System
.
out
.
println
(
"Case #"
+
caseid
+
": "
+
95
g
.
format
(
TetrahedronArea
(
p
[
0
],
p
[
1
],
p
[
2
],
p
[
3
])));
96
}
97
}
98
}