Triangular Vertices

                 1

                / \

               2 – 3

              / \ / \

             4 – 5 – 6           

            / \ / \ / \

           7 – 8 – 9 – 10

          / \ / \ / \ / \

         11- 12- 13- 14- 15

        / \ / \ / \ / \ / \

       16- 17- 18- 19- 20- 21

      / \ / \ / \ / \ / \ / \

     22- 23- 24- 25- 26- 27- 28

    / \ / \ / \ / \ / \ / \ / \

   29- 30- 31- 32- 33- 34- 35- 36

  / \ / \ / \ / \ / \ / \ / \ / \

 37- 38- 39- 40- 41- 42- 43- 44- 45

                .

                .

Write a program which will accept a set of points on this triangular grid, analyze it, and determine whether the points are the vertices of an equilateral triangle. In order for a figure to be acceptable, each side of the triangle must coincide with an edge in the grid.

 

using System;

using System.Collections.Generic;

using System.Text;

 

namespace TriangularVertices

{

    class Program

    {

        static void Main(string[] args)

        {

            int[] vertices = new int[3];

            int[] lineNumbers = new int[3];

            for (int i = 0; i < 3; i++)

            {

                Console.Write("Enter vertex " + (i + 1) + ": ");

                vertices[i] = int.Parse(Console.ReadLine());

            }

            Array.Sort(vertices);

            for (int i = 0; i < 3; i++)

            {

                lineNumbers[i] = LineNumber(vertices[i]);

            }

            Console.WriteLine("Equilateral Check: " + CheckEquilateral(vertices, lineNumbers));

        }

        public static int LineNumber(int num)

        {

            for (int i = 1, j = 1; i <= 32767; i++)

            {

                for (int k = 0; k < i; j++, k++)

                {

                    if (j == num)

                    {

                        return (i);

                    }

                }

            }

            return (0);

        }

        public static bool ValidLineNumbers(int[] lineNumbers)

        {

            int count = 1;

            for (int i = 0; i < lineNumbers.Length – 1; i++)

            {

                if (lineNumbers[i] != lineNumbers[i + 1])

                {

                    count++;

                }

            }

            if (count == 2)

            {

                return true;

            }

            else

            {

                return false;

            }

        }

        public static bool CheckEquilateral(int[] vertices, int[] lineNumbers)

        {

            if (ValidLineNumbers(lineNumbers))

            {

                int triangleBase = 0, triangleHeight = 0;

                if (lineNumbers[0] == lineNumbers[1])//down

                {

                    triangleBase = vertices[1] – vertices[0];

                    triangleHeight = lineNumbers[2] – lineNumbers[1];

                    if (triangleBase == triangleHeight)

                    {

                        for (int i = 0; i < triangleHeight; i++)

                        {

                            vertices[0] = vertices[0] + (LineNumber(vertices[0]) + 1);

                        }

                        if (vertices[0] == vertices[2])

                        {

                            return true;

                        }

                        else

                        {

                            return false;

                        }

                    }

                    else

                    {

                        return false;

                    }

                }

                else if (lineNumbers[1] == lineNumbers[2])//up

                {

                    triangleBase = vertices[2] – vertices[1];

                    triangleHeight = lineNumbers[1] – lineNumbers[0];

                    if (triangleBase == triangleHeight)

                    {

                        for (int i = 0; i < triangleHeight; i++)

                        {

                            vertices[1] = vertices[1] – (LineNumber(vertices[1]) – 1);

                        }

                        if (vertices[1] == vertices[0])

                        {

                            return true;

                        }

                        else

                        {

                            return false;

                        }

                    }

                    else

                    {

                        return false;

                    }

                }

            }

            return false;

        }

    }

}

 
Output:
Enter vertex 1: 7
Enter vertex 2: 9
Enter vertex 3: 18
Equilateral Check: True
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