Advertisement
dimipan80

Advanced Topics 17. Perimeter and Area of Polygon

Jul 4th, 2014
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.72 KB | None | 0 0
  1. // Write a program that calculates the perimeter and the area of given polygon (not necessarily convex) consisting of n floating-point coordinates in the 2D plane. Print the result rounded to two decimal digits after the decimal point. Use the input and output format from the examples. To hold the points, define a class Point(x, y). To hold the polygon use a Polygon class which holds a list of points.
  2.  
  3. namespace _17.PerimeterAndAreaOfPolygon
  4. {
  5.     using System;
  6.     using System.Collections.Generic;
  7.     using System.Linq;
  8.  
  9.     public class Point
  10.     {        
  11.         public Point(double x, double y)
  12.         {
  13.             this.X = x;
  14.             this.Y = y;
  15.         }
  16.  
  17.         public double X { get; set; }
  18.  
  19.         public double Y { get; set; }
  20.  
  21.         public double Distance(Point p2)
  22.         {
  23.             double x1 = this.X;
  24.             double y1 = this.Y;
  25.             double x2 = p2.X;
  26.             double y2 = p2.Y;
  27.             double distance = Math.Sqrt(((x1 - x2) * (x1 - x2)) + ((y1 - y2) * (y1 - y2)));
  28.             return distance;
  29.         }
  30.     }
  31.  
  32.     public class Polygon
  33.     {
  34.         private List<Point> pointList;
  35.  
  36.         public Polygon()
  37.         {
  38.             this.pointList = new List<Point>();
  39.         }        
  40.  
  41.         public Polygon(List<Point> pointList)
  42.         {
  43.             this.pointList = new List<Point>(pointList);
  44.         }
  45.  
  46.         public double Area()
  47.         {
  48.             double left = 0;
  49.             double right = 0;
  50.             for (int i = 0; i < this.pointList.Count - 1; i++)
  51.             {
  52.                 Point p1 = this.pointList[i];
  53.                 Point p2 = this.pointList[i + 1];
  54.                 left += p1.X * p2.Y;
  55.                 right += p2.X * p1.Y;
  56.             }
  57.  
  58.             double area = Math.Abs(left - right) / 2;
  59.             return area;
  60.         }
  61.  
  62.         public double Perimeter()
  63.         {
  64.             this.pointList.Add(this.pointList[0]);
  65.             double perimeter = 0;
  66.             for (int i = 0; i < this.pointList.Count - 1; i++)
  67.             {
  68.                 Point p1 = this.pointList[i];
  69.                 Point p2 = this.pointList[i + 1];
  70.                 perimeter += p1.Distance(p2);
  71.             }
  72.  
  73.             return perimeter;
  74.         }
  75.     }
  76.  
  77.     public class PerimeterAndAreaOfPolygon
  78.     {
  79.         public static void Main(string[] args)
  80.         {
  81.             checked
  82.             {
  83.                 int countPoints;
  84.                 do
  85.                 {
  86.                     Console.Write("Enter Integer number, bigger from 2, for Count of Polygon points: ");
  87.                 }
  88.                 while (!int.TryParse(Console.ReadLine(), out countPoints) || countPoints < 3);
  89.  
  90.                 List<Point> pointList = new List<Point>();
  91.                 Console.WriteLine("Entries your coordinates, each point by line, exactly this format [x y]!");
  92.                 for (int i = 0; i < countPoints; i++)
  93.                 {
  94.                     string inputLine = Console.ReadLine();
  95.                     char[] separators = { ' ', ',', ';' };
  96.                     string[] coordinates = inputLine.Split(separators, StringSplitOptions.RemoveEmptyEntries);
  97.                     double x = double.Parse(coordinates[0]);
  98.                     double y = double.Parse(coordinates[1]);
  99.                     Point p = new Point(x, y);
  100.                     pointList.Add(p);
  101.                 }
  102.  
  103.                 Polygon polygon = new Polygon(pointList);
  104.                 double area = polygon.Area();
  105.                 double perimeter = polygon.Perimeter();
  106.                 Console.WriteLine("The Perimeter of that Polygon is {0:f2} !", perimeter);
  107.                 Console.WriteLine("The Area of that Polygon is {0:f2} !", area);
  108.             }
  109.         }
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement