Find the type of Quadrilateral with given 4 points of the quadrilateral in cpp and python.




Given quadrilateral:

given quadrilateral with four points


You are only given 4 points of the quadrilaterals.

question is how can you find the type of given quadrilateral.

here, points input is in clock wish direction from a,b,c to d.

Explanation:

First of all, we are trying to find all measure angles and edges weights. 

Use the formula below to find the weights of edges.

formula of distance between two points


Use the formula below which references to triangle below to find the measure angles of quadrilaterals.


triangle for example

formula for angle
angle


angle

Here we need to find Diagonal ac and bd given in the image of the quadrilateral.

Now using conditions we can easily find which type of quadrilateral we have.

Code in python Image:

python code image for finding type of quadrilateral




python Cpp:

import math

if __name__ == "__main__":
    print("Enter 4 point coordinates of quadrilateral separated by space in sequence:")
    # array
    # read 8 number
    points = list(map(int,input().split()))    

    # create separate variable for coordinates
    xa = points[0]
    ya = points[1]
    xb = points[2]
    yb = points[3]
    xc = points[4]
    yc = points[5]
    xd = points[6]
    yd = points[7]

    # edge of quadrilateral using edge formula
    # finding edge using distance formula.
    a = math.sqrt((xb - xa) * (xb - xa) + (yb - ya) * (yb - ya))
    b = math.sqrt((xc - xb) * (xc - xb) + (yc - yb) * (yc - yb))
    c = math.sqrt((xd - xc) * (xd - xc) + (yd - yc) * (yd - yc))
    d = math.sqrt((xa - xd) * (xa - xd) + (ya - yd) * (ya - yd))

    # diagonal of quadrilateral
    # find diagonals.
    diagonal_ac = math.sqrt((xc - xa) * (xc - xa) + (yc - ya) * (yc - ya))
    diagonal_bd = math.sqrt((xd - xb) * (xd - xb) + (yd - yb) * (yd - yb))

    # angles
    # angles of quadrilateral
    # find angle using angle formula.
    A = math.acos((a * a + d * d - diagonal_bd * diagonal_bd) / (2 * a * d))
    B = math.acos((b * b + a * a - diagonal_ac * diagonal_ac) / (2 * b * a))
    C = math.acos((c * c + b * b - diagonal_bd * diagonal_bd) / (2 * c * b))
    D = math.acos((d * d + c * c - diagonal_ac * diagonal_ac) / (2 * d * c))

    # result.
    # if angles are equal means(90*)
    if (A == B and A == C and A == D):
   
        # if edge size are equal
        if (a == b and a == c and a == d):
            # square
            print("Quadrilateral is square...\n")
            print("area of square :", a * a)
       
        # else
        else:
            # rectangular
            print("Quadrilateral is rectangular...\n")
            print("area of square :", a * b)
       
   
    # angles are not equal but edges are equal
    elif (a == b and a == c and a == d):
        # diamond
        print("Quadrilateral is diamond(Rhombus)...\n")
   
    # opposite edges(sides) are equal
    elif (a == c and b == d):
        # parallelogram
        print("Quadrilateral is parallelogram...")
   
    else:
        print("Quadrilateral is just a quadrilateral...\n")
   



Code in cpp Image:

cpp code image for finding type of quadrilateral



Code Cpp:

// for input and output:
#include <iostream>
// for math function cos inverse.
#include <cmath>

// namespace.
using namespace std;

// driver
int main()
{
    // array
    int points[8];
    printf("Enter 4 point coordinates of quadrilateral separated by space in sequence:");
    // read 8 number
    for (int i = 0; i < 8; i++)
    {
        cin >> points[i];
    }

    // create separate variable for coordinates
    int xa, ya, xb, yb, xc, yc, xd, yd;
    xa = points[0], ya = points[1];
    xb = points[2], yb = points[3];
    xc = points[4], yc = points[5];
    xd = points[6], yd = points[7];

    // edge of quadrilateral using edge formula
    float a, b, c, d;
    // finding edge using distance formula.
    a = sqrt((xb - xa) * (xb - xa) + (yb - ya) * (yb - ya));
    b = sqrt((xc - xb) * (xc - xb) + (yc - yb) * (yc - yb));
    c = sqrt((xd - xc) * (xd - xc) + (yd - yc) * (yd - yc));
    d = sqrt((xa - xd) * (xa - xd) + (ya - yd) * (ya - yd));

    // diagonal of quadrilateral
    // find diagonals.
    float diagonal_ac = sqrt((xc - xa) * (xc - xa) + (yc - ya) * (yc - ya));
    float diagonal_bd = sqrt((xd - xb) * (xd - xb) + (yd - yb) * (yd - yb));

    // angles
    float A, B, C, D; // angles of quadrilateral
    // find angle using angle formula.
    A = acos((a * a + d * d - diagonal_bd * diagonal_bd) / (2 * a * d));
    B = acos((b * b + a * a - diagonal_ac * diagonal_ac) / (2 * b * a));
    C = acos((c * c + b * b - diagonal_bd * diagonal_bd) / (2 * c * b));
    D = acos((d * d + c * c - diagonal_ac * diagonal_ac) / (2 * d * c));

    // result.
    // if angles are equal means(90*)
    if (A == B && A == C && A == D)
    {
        // if edge size are equal
        if (a == b && a == c && a == d)
        {
            // square
            printf("Quadrilateral is square...\n");
            printf("area of square : %f", a * a);
        }
        // else
        else
        {
            // rectangular
            printf("Quadrilateral is rectangular...\n");
            printf("area of square : %f", a * b);
        }
    }
    // angles are not equal but edges are equal
    else if (a == b && a == c && a == d)
    {
        // diamond
        printf("Quadrilateral is diamond(Rhombus)...\n");
    }
    // opposite edges(sides) are equal
    else if (a == c && b == d)
    {
        // parallelogram
        printf("Quadrilateral is parallelogram...");
    }
    else
    {
        printf("Quadrilateral is just a quadrilateral...\n");
    }
    return 0;
}

Input:

here inputed points are (1,1), (1,0), (0,0) and (0,1).

output:

result after finding type


Read also: 






Post a Comment

0 Comments