Here is a basic C++ program that incorporates the features you've asked for:
#include <iostream>
#include <cmath> // For sqrt and pow functions
// Define a struct to represent a point in 2D space
struct Point {
double x; // x-coordinate
double y; // y-coordinate
// Default constructor with no arguments that initializes coordinates to (0,0)
Point() : x(0), y(0) {}
// Overloaded constructor that initializes coordinates with given x and y values
Point(double _x, double _y) : x(_x), y(_y) {}
// Function to print the coordinates of the Point
void print() const {
std::cout << "(" << x << ", " << y << ")" << std::endl;
}
};
// Function to calculate the Euclidean distance between two points
double distanceBetweenPoints(const Point& a, const Point& b) {
return sqrt(pow(b.x - a.x, 2) + pow(b.y - a.y, 2));
}
// Function that checks if a triangle with vertices at the given points can exist
bool canTriangleExist(const Point& a, const Point& b, const Point& c) {
double ab = distanceBetweenPoints(a, b); // Length of side AB
double bc = distanceBetweenPoints(b, c); // Length of side BC
double ca = distanceBetweenPoints(c, a); // Length of side CA
// Triangle inequality theorem: the sum of the lengths of any two sides must be greater than the length of the remaining side
return (ab + bc > ca) && (ab + ca > bc) && (bc + ca > ab);
}
int main() {
// Create three points
Point p1(0, 0);
Point p2(3, 4);
Point p3(-1, -1);
// Print points
std::cout << "Point P1: "; p1.print();
std::cout << "Point P2: "; p2.print();
std::cout << "Point P3: "; p3.print();
// Calculate distances between points
std::cout << "Distance between P1 and P2: " << distanceBetweenPoints(p1, p2) << std::endl;
std::cout << "Distance between P2 and P3: " << distanceBetweenPoints(p2, p3) << std::endl;
std::cout << "Distance between P3 and P1: " << distanceBetweenPoints(p3, p1) << std::endl;
// Check if a triangle can be formed with points as vertices
if (canTriangleExist(p1, p2, p3)) {
std::cout << "A triangle can be formed with vertices at the given points." << std::endl;
} else {
std::cout << "A triangle cannot be formed with vertices at the given points." << std::endl;
}
return 0;
}
Step-by-step explanation:
-
Include necessary headers:
<iostream> is included for input/output operations.
<cmath> is included for mathematical operations like sqrt (square root) and pow (power).
-
Define Point struct:
- A
struct named Point is created to represent a point in 2-dimensional space.
- It has two member variables,
x and y, representing the x and y coordinates respectively.
- Two constructors are defined: a default constructor and an overloaded constructor that takes two
double arguments.
- A member function
print is defined to output the point to the console.
-
Define distanceBetweenPoints function:
- This function takes two constant references to
Point objects a and b.
- It returns the Euclidean distance between these points using the distance formula.
-
Define canTriangleExist function:
- This function takes three constant references to
Point objects a, b, and c.
- It calculates the lengths of each of the three possible sides of a triangle.
- It then checks the triangle inequality theorem which states that in any triangle, the sum of the lengths of any two sides must be greater than the length of the third side.
- The function returns a boolean value indicating whether a triangle can exist with these vertices.
-
main() function:
- Three
Point objects are created. p1 uses the default constructor, while p2 and p3 use the overloaded constructor.
- The points are printed using the
print function of the Point struct.
- The distances between each pair of points are calculated and printed.
- Finally, the program checks and prints whether a triangle can be formed using the three points as vertices.
The program demonstrates the use of structs, overloaded constructors, member functions, and the application of the Euclidean distance formula and the triangle inequality theorem. It showcases how encapsulation and abstraction in C++ can create a clean and understandable code structure.