Problem Solving: Geometric Problems
Learn to write a program using a function that determines quadrilateral shapes.
We'll cover the following
We have covered the quadrilateral shapes problem in the geometric-based problems. In this lesson, we will rewrite the same program using functions.
Determining the quadrilateral type
Write a program that takes 4 points in the anti clockwise fashion (with x and y coordinates) as input from the user—p1x
, p1y
, p2x
, p2y
, p3x
, p3y
, p4x
, p4y
. The program should determine whether these points are the coordinates of a rectangle, square, parallelogram, rhombus, or any other quadrilateral using functions.
Sample input
P1 0 0
P2 2 0
P3 2 2
P4 0 2
Sample output
It's a square.
Let’s make a quadType()
function that determines the quadrilateral shape. It returns 1
for square, 2
for rhombus, 3
for rectangle, 4
for parallelogram and 5
for any other quadrilateral shape.
This function will take 8 input parameters (4 points with x, y coordinates). To determine the shape, we need to calculate the distances
between the sides and the diagonal points.
Since we have to calculate the distance between points, we need a generic function named twoPointDistanceSqr()
, which calculates the squared distance between any two given points.
int twoPointDistanceSqr(int x1,int y1, int x2,int y2){// Euclidean distance formulareturn (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);}
We have a quadType()
function to determine the quadrilateral type. Inside the quadType()
function, we can calculate the distance for four sides and the two diagonals by calling the twoPointDistanceSqr()
function.
Now, we have four sides and two diagonal distances, and we’ll compare the sides and diagonals to determine the shape type.
int quadType(int p1x,int p1y, int p2x, int p2y,int p3x,int p3y,int p4x,int p4y){int s1, s2, s3, s4, d1, d2;s1 = twoPointDistanceSqr(p1x,p1y,p2x,p2y);s2 = twoPointDistanceSqr(p2x,p2y,p3x,p3y);s3 = twoPointDistanceSqr(p3x,p3y,p4x,p4y);s4 = twoPointDistanceSqr(p4x,p4y,p1x,p1y);d1 = twoPointDistanceSqr(p1x,p1y,p3x,p3y);d2 = twoPointDistanceSqr(p2x,p2y,p4x,p4y);if (s1 == s2 && s1 == s3 && s1 == s4){if (d1 == d2)return 1; // Squareelsereturn 2; // Rhombus}else if (s1 == s3 && s2 == s4 && s1 != s2){if (d1 == d2)return 3; // Rectangleelsereturn 4; // Parallelogram}elsereturn 5; // Any Quadrilateral}
To print the quadrilateral type on the console, we’ll pass the return value of quadType()
to displayQuadType()
. In displayQuadType()
, we have a switch
statement in which we have an expression to evaluate the type, and the corresponding case will print the quadrilateral type.
void displayQuadType(int type){switch(type){case 1:cout << "Its a square" << endl; break;case 2:cout << "Its a rhombus" << endl; break;case 3:cout << "Its a rectangle" << endl; break;case 4:cout << "Its a parallelogram" << endl; break;case 5:cout << "It’s a quadrilateral" << endl; break;}}
Let’s write the complete code below and run it to see the output:
#include <iostream> using namespace std; int twoPointDistance(int x1,int y1, int x2,int y2) { return (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2); } int quadType(int p1x,int p1y, int p2x,int p2y,int p3x,int p3y,int p4x,int p4y) { int s1,s2,s3,s4,d1,d2; s1 = twoPointDistance(p1x,p1y,p2x,p2y); s2 = twoPointDistance(p2x,p2y,p3x,p3y); s3 = twoPointDistance(p3x,p3y,p4x,p4y); s4 = twoPointDistance(p4x,p4y,p1x,p1y); d1 = twoPointDistance(p1x,p1y,p3x,p3y); d2 = twoPointDistance(p2x,p2y,p4x,p4y); if (s1 == s2 && s1 == s3 && s1 == s4) { // Square or Rhombus if (d1 == d2) return 1; // Square case else return 2; // Rhombus Case } else if (s1 == s3 && s2 == s4 && s1 != s2) { // Rectangle or Parallelogram if (d1 == d2) return 3; // Rectangle else return 4; // Parallelogram } else return 5; // Any other Quadrilateral } void displayQuadType(int type) { switch(type) { case 1: cout << "Its a square" << endl; break; case 2: cout << "Its a rhombus" << endl; break; case 3: cout << "Its a rectangle" << endl; break; case 4: cout << "Its a parallelogram" << endl; break; case 5: cout << "It’s a quadrilateral" << endl; break; } } int main() { int p1x, p1y, p2x, p2y, p3x, p3y, p4x, p4y; cout << "P1: "; cin >> p1x >> p1y; cout << "P2: "; cin >> p2x >> p2y; cout << "P3: "; cin >> p3x >> p3y; cout << "P4: "; cin >> p4x >> p4y; int type = quadType(p1x, p1y, p2x, p2y,p3x, p3y, p4x, p4y); displayQuadType(type); return 0; }
- In lines 3–6, we compute the distance between two points.
- In lines 10–13, we call the
twoPointDistance()
function to compute the distance of four sides. - In line 14–15, we call the
twoPointDistance()
function to compute the diagonal of both sides. - In lines 16–31, we return numbers according to the quadrilateral type.
- In lines 33–48, we take the return value from
quadType()
and pass it to thedisplayQuadType()
to print the quadrilateral type on the console.
In this lesson, we determined quadrilateral types using functions. We also covered how to use the distance
function for computing the sides and diagonal distances. Finally, we use a separate function to display the quadrilateral type on the console.