Advertisement
Guest User

30.08 Exam - Problem 4

a guest
Sep 1st, 2015
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.86 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Teleportation
  8. {
  9.     class TeleportationMain
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             double[] aCoordinates = Console.ReadLine().Split(' ').Select(x => double.Parse(x)).ToArray();
  14.             var A = new Point(aCoordinates[0], aCoordinates[1]);
  15.             double[] bCoordinates = Console.ReadLine().Split(' ').Select(x => double.Parse(x)).ToArray();
  16.             var B = new Point(bCoordinates[0], bCoordinates[1]);
  17.             double[] cCoordinates = Console.ReadLine().Split(' ').Select(x => double.Parse(x)).ToArray();
  18.             var C = new Point(cCoordinates[0], cCoordinates[1]);
  19.             double[] dCoordinates = Console.ReadLine().Split(' ').Select(x => double.Parse(x)).ToArray();
  20.             var D = new Point(dCoordinates[0], dCoordinates[1]);
  21.  
  22.             var Rectangle = new List<Point>();
  23.  
  24.             Rectangle.Add(A);
  25.             Rectangle.Add(B);
  26.             Rectangle.Add(C);
  27.             Rectangle.Add(D);
  28.  
  29.             double radius = double.Parse(Console.ReadLine());
  30.  
  31.             double step = double.Parse(Console.ReadLine());
  32.  
  33.             double AB = Math.Sqrt((aCoordinates[0] - bCoordinates[0]) * (aCoordinates[0] - bCoordinates[0]) + (aCoordinates[1] - bCoordinates[1]) * (aCoordinates[1] - bCoordinates[1]));
  34.             double BC = Math.Sqrt((cCoordinates[0] - bCoordinates[0]) * (cCoordinates[0] - bCoordinates[0]) + (cCoordinates[1] - bCoordinates[1]) * (cCoordinates[1] - bCoordinates[1]));
  35.             double AD = Math.Sqrt((dCoordinates[0] - aCoordinates[0]) * (dCoordinates[0] - aCoordinates[0]) + (dCoordinates[1] - aCoordinates[1]) * (dCoordinates[1] - aCoordinates[1]));
  36.             double CD = Math.Sqrt((cCoordinates[0] - dCoordinates[0]) * (cCoordinates[0] - dCoordinates[0]) + (cCoordinates[1] - dCoordinates[1]) * (cCoordinates[1] - dCoordinates[1]));
  37.  
  38.             int count = 0;
  39.  
  40.             for (double X = step; X < radius; X += step)
  41.             {
  42.                 for (double Y = step; Y < radius; Y += step)
  43.                 {
  44.                     var point = new Point(X, Y);
  45.  
  46.                     if (point.PointInsideACircle(radius))
  47.                     {
  48.                         if (point.PointInsideARectangle(Rectangle, AB, BC, CD, AD))
  49.                         {
  50.                             count++;
  51.                         }
  52.                     }
  53.  
  54.                     point = new Point(-X, -Y);
  55.  
  56.                     if (point.PointInsideACircle(radius))
  57.                     {
  58.                         if (point.PointInsideARectangle(Rectangle, AB, BC, CD, AD))
  59.                         {
  60.                             count++;
  61.                         }
  62.                     }
  63.  
  64.                     point = new Point(X, -Y);
  65.  
  66.                     if (point.PointInsideACircle(radius))
  67.                     {
  68.                         if (point.PointInsideARectangle(Rectangle, AB, BC, CD, AD))
  69.                         {
  70.                             count++;
  71.                         }
  72.                     }
  73.  
  74.                     point = new Point(-X, Y);
  75.  
  76.                     if (point.PointInsideACircle(radius))
  77.                     {
  78.                         if (point.PointInsideARectangle(Rectangle, AB, BC, CD, AD))
  79.                         {
  80.                             count++;
  81.                         }
  82.                     }
  83.  
  84.                     point = new Point(0, -Y);
  85.  
  86.                     if (point.PointInsideACircle(radius))
  87.                     {
  88.                         if (point.PointInsideARectangle(Rectangle, AB, BC, CD, AD))
  89.                         {
  90.                             count++;
  91.                         }
  92.                     }
  93.  
  94.                     point = new Point(0, Y);
  95.  
  96.                     if (point.PointInsideACircle(radius))
  97.                     {
  98.                         if (point.PointInsideARectangle(Rectangle, AB, BC, CD, AD))
  99.                         {
  100.                             count++;
  101.                         }
  102.                     }
  103.  
  104.                     point = new Point(-X, 0);
  105.  
  106.                     if (point.PointInsideACircle(radius))
  107.                     {
  108.                         if (point.PointInsideARectangle(Rectangle, AB, BC, CD, AD))
  109.                         {
  110.                             count++;
  111.                         }
  112.                     }
  113.  
  114.                     point = new Point(X, 0);
  115.  
  116.                     if (point.PointInsideACircle(radius))
  117.                     {
  118.                         if (point.PointInsideARectangle(Rectangle, AB, BC, CD, AD))
  119.                         {
  120.                             count++;
  121.                         }
  122.                     }
  123.  
  124.                 }
  125.                
  126.             }
  127.             Console.WriteLine(count);
  128.         }
  129.  
  130.         class Point
  131.         {
  132.             public double X { get; set; }
  133.             public double Y { get; set; }
  134.  
  135.             public Point(double x, double y)
  136.             {
  137.                 this.X = x;
  138.                 this.Y = y;
  139.             }
  140.  
  141.             public void ResetPoint()
  142.             {
  143.                 this.X = 0;
  144.                 this.Y = 0;
  145.             }
  146.  
  147.             public bool PointInsideACircle(double radius)
  148.             {
  149.                 return ((this.X - 0) * (this.X - 0) + (this.Y - 0) * (this.Y - 0)) < radius * radius;
  150.             }
  151.  
  152.             public bool PointInsideARectangle(List<Point> points, double AB, double BC, double CD, double AD)
  153.             {
  154.                 double AX = Math.Sqrt((this.X - points[0].X) * (this.X - points[0].X) + (this.Y - points[0].Y) * (this.Y - points[0].Y));
  155.                 double BX = Math.Sqrt((this.X - points[1].X) * (this.X - points[1].X) + (this.Y - points[1].Y) * (this.Y - points[1].Y));
  156.                 double CX = Math.Sqrt((this.X - points[2].X) * (this.X - points[2].X) + (this.Y - points[2].Y) * (this.Y - points[2].Y));
  157.                 double DX = Math.Sqrt((this.X - points[3].X) * (this.X - points[3].X) + (this.Y - points[3].Y) * (this.Y - points[3].Y));
  158.  
  159.                 double p = (AX + BX + AB) / 2;
  160.                 double areaAXB = Math.Sqrt(p * (p - AX) * (p - BX) * (p - AB));
  161.  
  162.                 p = (BX + CX + BC) / 2;
  163.                 double areaBXC = Math.Sqrt(p * (p - CX) * (p - BX) * (p - BC));
  164.  
  165.                 p = (CD + CX + DX) / 2;
  166.                 double areaCXD = Math.Sqrt(p * (p - CX) * (p - DX) * (p - CD));
  167.  
  168.                 p = (AD + AX + DX) / 2;
  169.                 double areaDXA = Math.Sqrt(p * (p - AX) * (p - DX) * (p - AD));
  170.  
  171.                 double rectArea = AB * BC;
  172.  
  173.                 if (rectArea > areaAXB + areaBXC + areaCXD + areaDXA)
  174.                 {
  175.                     return true;
  176.                 }
  177.                 else
  178.                 {
  179.                     return false;
  180.                 }
  181.             }
  182.         }
  183.     }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement