double Face::getArea() const { Vec3f a = (*this)[0]->get(); Vec3f b = (*this)[1]->get(); Vec3f c = (*this)[2]->get(); Vec3f d = (*this)[3]->get(); return AreaOfTriangle(DistanceBetweenTwoPoints(a,b), DistanceBetweenTwoPoints(a,c), DistanceBetweenTwoPoints(b,c)) + AreaOfTriangle(DistanceBetweenTwoPoints(c,d), DistanceBetweenTwoPoints(a,d), DistanceBetweenTwoPoints(a,c)); }
float Face::getArea() const { glm::vec3 a = (*this)[0]->get(); glm::vec3 b = (*this)[1]->get(); glm::vec3 c = (*this)[2]->get(); glm::vec3 d = (*this)[3]->get(); return AreaOfTriangle(DistanceBetweenTwoPoints(a,b), DistanceBetweenTwoPoints(a,c), DistanceBetweenTwoPoints(b,c)) + AreaOfTriangle(DistanceBetweenTwoPoints(c,d), DistanceBetweenTwoPoints(a,d), DistanceBetweenTwoPoints(a,c)); }
int main(){ int n; double xa[MAX_NUMS]; //多边形各顶点x坐标 double ya[MAX_NUMS]; //多边形各顶点y坐标 double x[3]; //三角形各顶点x坐标 double y[3]; //三角形各顶点y坐标 double area = 0.0; //面积 int i, j; //输入n及各顶点坐标 printf("Enter the sides number of polygon(3≤N≤%d):\n", MAX_NUMS); scanf("%d", &n); for (i = 0; i < n; i++){ printf("Enter the coordinates of vertex A(%d):\n", i + 1); scanf("%lf%lf", &xa[i], &ya[i]); } x[0] = xa[0]; y[0] = ya[0]; for (i = 0; i < n - 2; i++){ for (j = 1; j < 3; j++){ x[j] = xa[i + j]; y[j] = ya[i + j]; } area = area + AreaOfTriangle(x, y); } printf("The area of the polygon is %g", area); }
bool Face::isConvex() const { Vec3f a = (*this)[0]->get(); Vec3f b = (*this)[1]->get(); Vec3f c = (*this)[2]->get(); Vec3f d = (*this)[3]->get(); // no D double t1 = AreaOfTriangle(DistanceBetweenTwoPoints(a,b), DistanceBetweenTwoPoints(a,c), DistanceBetweenTwoPoints(b,c)); // no B double t2 = AreaOfTriangle(DistanceBetweenTwoPoints(a,c), DistanceBetweenTwoPoints(a,d), DistanceBetweenTwoPoints(c,d)); // no C double t3 = AreaOfTriangle(DistanceBetweenTwoPoints(a,d), DistanceBetweenTwoPoints(a,b), DistanceBetweenTwoPoints(b,d)); // no A double t4 = AreaOfTriangle(DistanceBetweenTwoPoints(c,d), DistanceBetweenTwoPoints(b,d), DistanceBetweenTwoPoints(b,c)); if (!fabs((t1+t2)-(t3+t4) < .001)){ std::cout << " t1: " << t1 << std::endl; std::cout << " t2: " << t2 << std::endl; std::cout << " t3: " << t3 << std::endl; std::cout << " t4: " << t4 << std::endl; } //assert(fabs((t1+t2)-(t3+t4) < .001)); if (fabs((t1+t2)-(t3+t4) < .001)){ return true; } else{ return false; } }
bool isLinear(const Vec3f &p1, const Vec3f &p2, const Vec3f &p3){ double triangle_area = AreaOfTriangle(DistanceBetweenTwoPoints(p1,p2), DistanceBetweenTwoPoints(p1, p3), DistanceBetweenTwoPoints(p2,p3)); //Non-zero triangle area means that the points are not colinear if(triangle_area > EPSILON){ return false; } return true; }
double vtTin::GetArea2D() { double area = 0.0; uint tris = NumTris(); for (uint i = 0; i < tris; i++) { const DPoint2 &p1 = m_vert[m_tri[i*3]]; const DPoint2 &p2 = m_vert[m_tri[i*3+1]]; const DPoint2 &p3 = m_vert[m_tri[i*3+2]]; area += AreaOfTriangle(p1, p2, p3); } return area; }
// Get random point in poly fPoint CWorldServer::RandInPoly( fPoint p[], int pcount ) { int tnum = 0; float tval = 0; float totalarea = 0; float* areas = new float[pcount-1]; for(int i=0; i<pcount-2; i++) { totalarea += AreaOfTriangle(p[0], p[i+1], p[i+2]); areas[i+1] = totalarea; } tval = rand() * totalarea / RAND_MAX; for (tnum=1; tnum<pcount-1; tnum++) { if(tval<=areas[tnum]&&tval>areas[tnum-1]) break; } return RandInTriangle(p[0], p[tnum], p[tnum+1]); }
double vtTin::GetArea3D() { double area = 0.0; uint tris = NumTris(); for (uint i = 0; i < tris; i++) { const int v0 = m_tri[i*3]; const int v1 = m_tri[i*3+1]; const int v2 = m_tri[i*3+2]; const DPoint2 &p1 = m_vert[v0]; const DPoint2 &p2 = m_vert[v1]; const DPoint2 &p3 = m_vert[v2]; DPoint3 dp1(p1.x, p1.y, m_z[v0]); DPoint3 dp2(p2.x, p2.y, m_z[v1]); DPoint3 dp3(p3.x, p3.y, m_z[v2]); area += AreaOfTriangle(dp1, dp2, dp3); } return area; }