/* main function */ int main(void){ /* variable declaration */ double side1 = 0.0; double side2 = 0.0; double side3 = 0.0; double area = 0.0; /* accept sides from the user */ printf("Please enter the first side of the triangle: "); scanf("%lf", &side1); printf("Please enter the second side of the triangle: "); scanf("%lf", &side2); printf("Please enter the third side of the triangle: "); scanf("%lf", &side3); /* proceed with the main program objective */ /* use if and else to branch out first the validity of the triangle */ /* then the shape, and check if the triangle is also isoceles */ /* 0 is false, 1 is true */ /* calculate the area of the triangle */ area = area_of_triangle(side1, side2, side3); /* this is valid */ if (valid_triangle(side1, side2, side3) == 1){ //equilateral triangles are never isoceles if (equilateral_triangle(side1, side2, side3) == 1){ printf("\nEquilateral!\n"); } //right triangle else if (right_triangle(side1, side2, side3)== 1){ //check if triangle is isoceles also if (isoceles_triangle(side1, side2, side3) == 1){ printf("\nIsoceles right triangle!\n"); } else { printf("\nRight triangle!\n"); } } //obtuse triangle else if (obtuse_triangle(side1, side2, side3) == 1){ //check if triangle is isoceles also if (isoceles_triangle(side1, side2, side3) == 1){ printf("\nIsoceles obtuse triangle!\n"); } else { printf("\nObtuse triangle!\n"); } } //acute triangle else if (acute_triangle(side1, side2, side3) == 1){ //check if triangle is isoceles also if (isoceles_triangle(side1, side2, side3) == 1){ printf("\nIsoceles acute triangle!\n"); } else { printf("\nAcute triangle!\n"); } } //print out the area of the triangle after determining the shape printf("The area of the triangle is: %.2f\n", area); } //this is invalid else { printf("\nThe triangle is invalid because the length of one side is greater than or equal to the sum of the lengths of the other two sides\n"); } return (0); }
TEST(GeometryTests, AreaOfTriangle){ EXPECT_DOUBLE_EQ(15, area_of_triangle(6,5)); }
void PolygonToParticle::mapping( const kvs::PolygonObject* polygon, const size_t nparticles ) { if ( polygon->polygonType() != kvs::PolygonObject::Triangle ) { kvsMessageError( "Only support triangle polyon." ); return; } size_t nvertices = polygon->nvertices(); size_t npolygons = polygon->nconnections(); kvs::ValueArray<kvs::Real32> polygon_coords( polygon->coords() ); kvs::ValueArray<kvs::UInt32> polygon_conncections; kvs::ValueArray<kvs::Real32> polygon_normals; if ( npolygons == 0 ) { npolygons = nvertices / 3; polygon_conncections.allocate( 3 * npolygons ); for ( size_t i = 0; i < 3 * npolygons; i++ ) polygon_conncections[i] = i; } else { polygon_conncections = polygon->connections(); } if ( polygon->nnormals() == nvertices ) { polygon_normals = polygon->normals(); } else { polygon_normals.allocate( 3 * nvertices ); polygon_normals.fill( 0x00 ); for ( size_t i = 0; i < npolygons; i++ ) { kvs::Vector3f v0( polygon_coords.pointer() + 3 * polygon_conncections[ i * 3 ] ); kvs::Vector3f v1( polygon_coords.pointer() + 3 * polygon_conncections[ i * 3 + 1 ] ); kvs::Vector3f v2( polygon_coords.pointer() + 3 * polygon_conncections[ i * 3 + 2 ] ); kvs::Vector3f normal = normal_in_triangle( v0, v1, v2 ); for ( size_t j = 0; j < 3; j++ ) { polygon_normals[ 3 * polygon_conncections[ i * 3 + j ] ] += normal.x(); polygon_normals[ 3 * polygon_conncections[ i * 3 + j ] + 1 ] += normal.y(); polygon_normals[ 3 * polygon_conncections[ i * 3 + j ] + 2 ] += normal.z(); } } for ( size_t i = 0; i < nvertices; i++ ) { kvs::Vector3f normal( polygon_normals.pointer() + 3 * i ); normal.normalize(); polygon_normals[ i * 3 ] = normal.x(); polygon_normals[ i * 3 + 1 ] = normal.y(); polygon_normals[ i * 3 + 2 ] = normal.z(); } } std::cout << "nvertices : " << nvertices << std::endl; std::cout << "npolygons : " << npolygons << std::endl; float total_area = 0.0f; for ( size_t i = 0; i < npolygons; i++ ) { kvs::Vector3f v0( polygon_coords.pointer() + 3 * polygon_conncections[ i * 3 ] ); kvs::Vector3f v1( polygon_coords.pointer() + 3 * polygon_conncections[ i * 3 + 1 ] ); kvs::Vector3f v2( polygon_coords.pointer() + 3 * polygon_conncections[ i * 3 + 2 ] ); total_area += area_of_triangle( v0, v1, v2 ); } std::cout << "total_area : " << total_area << std::endl; const float lattice = sqrtf( total_area / ( nparticles * 0.88 ) ); const float np_unit = ( nparticles * 0.8 ) / total_area; std::cout << "lattice : " << lattice << std::endl; std::cout << "np_unit : " << np_unit << std::endl; std::vector<kvs::Real32> coords; std::vector<kvs::UInt8> colors; std::vector<kvs::Real32> normals; for ( size_t i = 0; i < npolygons; i++ ) { kvs::Vector3f v0( polygon_coords.pointer() + 3 * polygon_conncections[ i * 3 ] ); kvs::Vector3f v1( polygon_coords.pointer() + 3 * polygon_conncections[ i * 3 + 1 ] ); kvs::Vector3f v2( polygon_coords.pointer() + 3 * polygon_conncections[ i * 3 + 2 ] ); kvs::Vector3f n0( polygon_normals.pointer() + 3 * polygon_conncections[ i * 3 ] ); kvs::Vector3f n1( polygon_normals.pointer() + 3 * polygon_conncections[ i * 3 + 1 ] ); kvs::Vector3f n2( polygon_normals.pointer() + 3 * polygon_conncections[ i * 3 + 2 ] ); const float area = area_of_triangle( v0, v1, v2 ); const float np_in_triangle_f = area * np_unit; size_t np_in_triangle = static_cast<size_t>( np_in_triangle_f ); if ( np_in_triangle_f < 1.0f ) { np_in_triangle = ( kvs::CellByCellParticleGenerator::GetRandomNumber() < np_in_triangle_f ) ? 1 : 0; } for ( size_t j = 0; j < np_in_triangle; j++ ) { kvs::Vector3f coord = sample_in_triangle( v0, v1, v2 ); coords.push_back( coord.x() ); coords.push_back( coord.y() ); coords.push_back( coord.z() ); kvs::Vector3f normal = interp_in_triangle( coord, v0, n0, v1, n1, v2, n2 ); normals.push_back( normal.x() ); normals.push_back( normal.y() ); normals.push_back( normal.z() ); colors.push_back( 255 ); colors.push_back( 255 ); colors.push_back( 255 ); } } BaseClass::setCoords( kvs::ValueArray<kvs::Real32>( coords ) ); BaseClass::setColors( kvs::ValueArray<kvs::UInt8>( colors ) ); BaseClass::setNormals( kvs::ValueArray<kvs::Real32>( normals ) ); BaseClass::updateMinMaxCoords(); std::cout << "generated particles : " << coords.size() / 3 << std::endl; }