题目:
该表的每一行包含一个点的三维坐标,写一个sql查询,计算每个三角形的面积,保留两位小数。
解题思路:
首先需要知道关于三维坐标的三角形面积的求解公式,然后才能进行计算,否则的话就没有办法进行计算处理:
DM的处理
计提思路:
第一步、先进行行转列的处理
select t.id,
(select p.x from Point p where t.`pointId1` =p.id) as px1,
(select p.y from Point p where t.`pointId1` =p.id) as py1,
(select p.z from Point p where t.`pointId1` =p.id) as pz1,
(select p.x from Point p where t.`pointId2` =p.id) as px2,
(select p.y from Point p where t.`pointId2` =p.id) as py2,
(select p.z from Point p where t.`pointId2` =p.id) as pz2,
(select p.x from Point p where t.`pointId3` =p.id) as px3,
(select p.y from Point p where t.`pointId3` =p.id) as py3,
(select p.z from Point p where t.`pointId3` =p.id) as pz3
from `triangle` t where t.id=1
第二步,代入公式计算是否正确:
select id as TriangleId ,round(((px2-px1)*(px3-px1)*(py2-py1)*(py3-py1)*(pz2-pz1)*(pz3-pz1))/2,2) as Area from (
select t.id,
(select p.x from Point p where t.`pointId1` =p.id) as px1,
(select p.y from Point p where t.`pointId1` =p.id) as py1,
(select p.z from Point p where t.`pointId1` =p.id) as pz1,
(select p.x from Point p where t.`pointId2` =p.id) as px2,
(select p.y from Point p where t.`pointId2` =p.id) as py2,
(select p.z from Point p where t.`pointId2` =p.id) as pz2,
(select p.x from Point p where t.`pointId3` =p.id) as px3,
(select p.y from Point p where t.`pointId3` =p.id) as py3,
(select p.z from Point p where t.`pointId3` =p.id) as pz3
from `triangle` t ) tt