Rectangle Check for Given Coordinates

using System;

using System.Collections.Generic;

using System.Text;

using System.Text.RegularExpressions;

 

namespace ConsoleApplication1

{

    public class Point

    {

        public int x;

        public int y;

    }

    class Program

    {

        static void Main(string[] args)

        {

            try

            {

                int numberOfCoordinates = 4;

                char[] name = { ‘A’, ‘B’, ‘C’, ‘D’ };

                Point[] coordinates = new Point[numberOfCoordinates];

                Regex reg = new Regex(" ");

                Console.WriteLine("Enter coordinates of the quadrilateral separated by spaces:");

                //Reading the user input

                string inputString = Console.ReadLine();

                int i = 0, flag = 0;

                //Initializing the coordinates

                foreach (string str in reg.Split(inputString))

                {

                    if (flag == 0)

                    {

                        coordinates[i] = new Point();

                        coordinates[i].x = int.Parse(str);

                        flag++;

                    }

                    else

                    {

                        coordinates[i] = new Point();

                        coordinates[i].y = int.Parse(str);

                        flag = 0;

                        i++;

                    }

                }

                int[] pointer = new int[numberOfCoordinates];

                for (i = 0; i < numberOfCoordinates; i++)

                {

                    pointer[i] = i;

                }

                i–;

                pointer[i]–;

                do

                {

                    i = numberOfCoordinates – 1;

                label1:

                    pointer[i]++;

                    if (pointer[i] == numberOfCoordinates && i != 0)

                    {

                        pointer[i] = 0;

                        i–;

                        goto label1;

                    }

                    else if(pointer[0] == numberOfCoordinates)

                    {

                        break;

                    }

                    int[] check = new int[numberOfCoordinates];

                    for (int j = 0; j < numberOfCoordinates; j++)

                    {

                        check[j] = 0;

                    }

                    flag = 0;

                    for (int j = 0; j < numberOfCoordinates; j++)

                    {

                        check[pointer[j]]++;

                        if (check[pointer[j]] > 1)

                        {

                            flag = 1;

                            break;

                        }

                    }

                    if (flag == 0)

                    {

                        if (CheckRectangle(coordinates[pointer[0]], coordinates[pointer[1]], coordinates[pointer[2]], coordinates[pointer[3]]))

                        {

                            flag = -1;

                            Console.WriteLine("The given coordinates form a rectangle in the given sequence:");

                            Console.WriteLine("{0}, {1}, {2}, {3}", name[pointer[0]], name[pointer[1]], name[pointer[2]], name[pointer[3]]);

                            break;

                        }

                    }

 

                } while (pointer[0] != numberOfCoordinates);

                if (flag != -1)

                {

                    Console.WriteLine("The given coordinates doesn’t form a rectangle.");

                }

            }

            catch (Exception e)

            {

                Console.WriteLine(e.Message);

            }

        }

        public static bool CheckRectangle(Point A, Point B, Point C, Point D)

        {

            if (A != null && B != null && C != null && D != null)

            {

                //Diagonal Length Check

                double d1 = Math.Sqrt((A.x – C.x) ^ 2 + (A.y – C.y) ^ 2);

                double d2 = Math.Sqrt((A.x – C.x) ^ 2 + (A.y – C.y) ^ 2);

                //Side Length Check

                double AB = Math.Sqrt((A.x – B.x) ^ 2 + (A.y – B.y) ^ 2);

                double CD = Math.Sqrt((C.x – D.x) ^ 2 + (C.y – D.y) ^ 2);

                double BC = Math.Sqrt((B.x – C.x) ^ 2 + (B.y – C.y) ^ 2);

                double AD = Math.Sqrt((A.x – D.x) ^ 2 + (A.y – D.y) ^ 2);

                if (d1 == d2 && AB == CD && BC == AD)

                {

                    return true;

                }

                else

                {

                    return false;

                }

            }

            else

            {

                return false;

            }

        }

    }

}
 
Enter coordinates of the quadrilateral separated by spaces:
0 0 1 1 0 1 1 0
The given coordinates form a rectangle in the given sequence:
A, D, B, C
Press any key to continue . . .
Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s