Advertisement
Guest User

Perimeter and Area of Polygon (UNSOLVED)

a guest
Apr 5th, 2014
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.60 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace PerimetreAndAreaOfPolygon
  5. {
  6.     class CalculatePerimetreAndSurface
  7.     {
  8.         class Point
  9.         {
  10.             private double x, y;
  11.  
  12.             public Point(double x, double y)
  13.             {
  14.                 X = x;
  15.                 Y = y;
  16.             }
  17.  
  18.             public double X
  19.             {
  20.                 get
  21.                 {
  22.                     return this.x;
  23.                 }
  24.                 set
  25.                 {
  26.                     try
  27.                     {
  28.                         this.x = value;
  29.                     }
  30.                     catch (InvalidCastException)
  31.                     {
  32.                         Console.Error.WriteLine("Invalid argument value of `X'");
  33.                     }
  34.                    
  35.                 }
  36.             }
  37.             public double Y
  38.             {
  39.                 get
  40.                 {
  41.                     return this.y;
  42.                 }
  43.                 set
  44.                 {
  45.                     try
  46.                     {
  47.                         this.y = value;
  48.                     }
  49.                     catch (InvalidCastException)
  50.                     {
  51.                         Console.Error.WriteLine("Invalid argument value of `Y'");
  52.                     }
  53.                 }
  54.             }
  55.             public double DistanceTo(Point point)
  56.             {
  57.                 return Math.Sqrt((this.X - point.X) * (this.X - point.X) + (this.Y - point.Y) * (this.Y - point.Y));
  58.             }
  59.         }
  60.  
  61.         public class Polygon
  62.         {
  63.             private List<Point> points;
  64.  
  65.             //public Polygon()
  66.             //{
  67.             //    points = new List<Point>();
  68.             //}
  69.             public Polygon(params Point[] points)
  70.             {
  71.                 this.points = new List<Point>(points);
  72.             }
  73.             public Polygon(List<Point> points)
  74.             {
  75.                 this.points = new List<Point>(points);
  76.             }
  77.  
  78.             public double GetPerimetre()
  79.             {
  80.                 points.Add(points[0]);
  81.                 double result = 0;
  82.                 for (int i = 0; i < points.Count - 1; i++)
  83.                 {
  84.                     result += points[i].DistanceTo(points[i + 1]);
  85.                 }
  86.                 points.RemoveAt(points.Count - 1);
  87.                 return result;
  88.             }
  89.             public double GetArea()
  90.             {
  91.                 points.Add(points[0]);
  92.                 double result = 0;
  93.                 for (int i = 0; i < points.Count - 1; i++)
  94.                 {
  95.                     result += points[i].X * points[i + 1].Y - points[i].Y - points[i + 1].X;
  96.                 }
  97.                 points.RemoveAt(points.Count - 1);
  98.                 result = Math.Abs(result / 2);
  99.                 return result;
  100.             }
  101.         }
  102.  
  103.         static void Main()
  104.         {
  105.             Console.Write("n = ");
  106.             int n = int.Parse(Console.ReadLine());
  107.             Console.Write("Now enter {0} points (described with their coordiantes (x and y) :", n);
  108.             string[] values;
  109.             List<Point> points = new List<Point>();
  110.             for (int i = 0; i < n; i++)
  111.             {
  112.                 values = Console.ReadLine().Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
  113.                 points.Add(new Point(double.Parse(values[0]), double.Parse(values[1])));
  114.             }
  115.             Polygon poly = new Polygon(points);
  116.             Console.WriteLine("\nperimetre = {0}\narea = {1}", poly.GetPerimetre(), poly.GetArea());
  117.         }
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement