Thursday, October 8, 2015

GenericShape or Polygon Area Calculator in C#



Shape.cs

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GenericAreaCalculator
{
    public class Shape
    {
        public Dictionary<int,int> x;
        public Dictionary<int, int> y;

        public Shape()
        {
            x = new Dictionary<int, int>();
            y = new Dictionary<int, int>();
        }
    }
}


Area.cs

 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
46
47
48
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GenericAreaCalculator
{
    public class Area:Shape
    {
        public float CalculateArea(Shape Shapes,int sides)
        {
            float area = 0;
             int x1 = 0;
             int y2 = 0;
             int y1 = 0;
             int x2 = 0;

            if (sides > 1)
            {
                for (int index = 1; index < sides; index++)
                {
                     x1 = Shapes.x[index];
                     y2 = Shapes.y[index + 1];
                     y1 = Shapes.y[index];
                     x2 = Shapes.x[index + 1];
                    area = area + (((x1 * y2)) - ((y1 * x2)));

                }

                 int xn = Shapes.x[sides];              
                 y1 = Shapes.y[1];
                 int yn = Shapes.y[sides];
                 x1 = Shapes.x[1];
                 area = (area + (((xn * y1)) - ((yn * x1))))/2;
                
            }
            else if (sides == 1)
            {
                area = (float)(3.14 * Shapes.x[0] * Shapes.x[0]);
            }
            

            

            return Math.Abs(area);
        }
    }
}


Program.cs

 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GenericAreaCalculator
{
    class Program
    {
        static void Main(string[] args)
        {
            Shape shapeobj = new Shape();

            shapeobj.x.Add(1, 0);
            shapeobj.x.Add(2, 10);
            shapeobj.x.Add(3, 10);
            shapeobj.x.Add(4, 0);

            shapeobj.y.Add(1, 0);
            shapeobj.y.Add(2, 0);
            shapeobj.y.Add(3, 10);
            shapeobj.y.Add(4, 10);

            Area area = new Area();

            float ShapeArea = area.CalculateArea(shapeobj, 4);

            Console.WriteLine("Area of Square is:{0}", ShapeArea);

            shapeobj.x.Clear();
            shapeobj.y.Clear();

            shapeobj.x.Add(0, 10);

            ShapeArea = area.CalculateArea(shapeobj, 1);

            Console.WriteLine("Area of Circle is:{0}", ShapeArea);

            Console.ReadLine();
        }
    }
}


Output:

No comments:

Post a Comment