C++ C和x2B之间的差异+;应该相同的代码段
对于计算机图形学的家庭作业,我们必须编写一个函数,确定点C++ C和x2B之间的差异+;应该相同的代码段,c++,C++,对于计算机图形学的家庭作业,我们必须编写一个函数,确定点p是否位于边p1到p2的左侧。现在,我的搭档和我有两个不同版本的函数,尽管计算的是同一个东西,但结果却不同 片段1: // The direction of the line from p1 to p: float lineX = p.x - p1.x; float lineY = p.y - p1.y; // The direction of the triangle's edge from p1 to p2: float edgeX =
p
是否位于边p1
到p2
的左侧。现在,我的搭档和我有两个不同版本的函数,尽管计算的是同一个东西,但结果却不同
片段1:
// The direction of the line from p1 to p:
float lineX = p.x - p1.x;
float lineY = p.y - p1.y;
// The direction of the triangle's edge from p1 to p2:
float edgeX = p2.x - p1.x;
float edgeY = p2.y - p1.x;
// The z component of the cross product (line x edge):
float F = lineX * edgeY - lineY * edgeX;
float x = p.x;
float y = p.y;
float X = p1.x;
float Y = p1.y;
float dX = p2.x - p1.x;
float dY = p2.y - p1.y;
float F = ((x-X) * dY - (y-Y) * dX);
片段2:
// The direction of the line from p1 to p:
float lineX = p.x - p1.x;
float lineY = p.y - p1.y;
// The direction of the triangle's edge from p1 to p2:
float edgeX = p2.x - p1.x;
float edgeY = p2.y - p1.x;
// The z component of the cross product (line x edge):
float F = lineX * edgeY - lineY * edgeX;
float x = p.x;
float y = p.y;
float X = p1.x;
float Y = p1.y;
float dX = p2.x - p1.x;
float dY = p2.y - p1.y;
float F = ((x-X) * dY - (y-Y) * dX);
我知道浮点运算不是交换的,也不是结合的。然而,据我所知,计算顺序应该完全相同,因为lineX=x-x
和edgeX=dX
是否有一些编译器优化会干扰计算?复制和粘贴错误:
float edgeX = p2.x - p1.x;
float edgeY = p2.y - p1.x;
(p1.x)?复制和粘贴错误:
float edgeX = p2.x - p1.x;
float edgeY = p2.y - p1.x;
(p1.x)