ellapt

PointInRectangular

Jun 23rd, 2013
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.73 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace T2.CheckPointVsTriangle
  6. {
  7.     class PointInTriangle
  8.     {
  9.         static void Main()
  10.         {
  11.             Console.WriteLine("Given 3 points A, B and C, forming triangle, and a point P");
  12.             Console.WriteLine("Check if the point P is in the triangle or not\n");
  13.  
  14.             //  triangle points
  15.             Point a = new Point(-3.45, -2.12);
  16.             Point b = new Point(6.82, 1.9);
  17.             Point c = new Point(3.1, 10.4);
  18.  
  19.             //  points to check
  20.  
  21.  //           Point p = new Point(-4.8, 9.2);
  22.             Point p = new Point(3.2, 4.82);
  23.  
  24.             //  find outermost coordinates of the triangle
  25.             double minX = Math.Min(Math.Min(a.X,b.X),c.X);
  26.             double maxX = Math.Max(Math.Max(a.X,b.X),c.X);
  27.  
  28.             double minY = Math.Min(Math.Min(a.Y,b.Y),c.Y);
  29.             double maxY = Math.Max(Math.Max(a.Y,b.Y),c.Y);
  30.  
  31.             //  check if the point is in the triangle or not
  32.             bool inTriangle = minX < p.X && p.X < maxX && minY < p.Y && p.Y < maxY;
  33.             Console.Write("Is the point {0} inside the triangle\n\n{1} {2} {3} ? \t", p, a, b, c);
  34.             Console.WriteLine(inTriangle);
  35.             Console.WriteLine();
  36.         }
  37.     }
  38. }
  39.  
  40. using System;
  41.  
  42. namespace T2.CheckPointVsTriangle
  43. {
  44.     class Point
  45.     {
  46.         public double X { get; set; }
  47.         public double Y { get; set; }
  48.  
  49.         public Point(double x, double y)
  50.         {
  51.             this.X = x;
  52.             this.Y = y;
  53.         }
  54.  
  55.         public override string ToString()
  56.         {
  57.             string result = string.Format("({0}, {1})", this.X, this.Y);
  58.             return result;
  59.         }
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment