ellapt

PointInTriangle

Jun 23rd, 2013
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.95 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, 0);
  16.             Point b = new Point(6.82, 1.9, 0);
  17.             Point c = new Point(3.1, 10.4, 0);
  18.  
  19.  /*           Point a = new Point(1, 1, 0);
  20.             Point b = new Point(1, 2, 0);
  21.             Point c = new Point(2, 1, 0); */
  22.  
  23.             Vector3D va = new Vector3D(a.X,a.Y, 0);
  24.             Vector3D vb = new Vector3D(b.X,b.Y, 0);
  25.             Vector3D vc = new Vector3D(c.X,c.Y, 0);
  26.  
  27.             //  points to check
  28.  
  29. //            Point p = new Point(-4.8, 9.2, 0);
  30. //            Vector3D vp = new Vector3D(p.X, p.Y, 0);
  31.  
  32.             Point p = new Point(3.2, 4.82, 0);
  33.             Vector3D vp = new Vector3D(p.X, p.Y, 0);
  34.  
  35. //            Point p = new Point(1.9, 1.9, 0);
  36. //            Vector3D vp = new Vector3D(p.X,p.Y,0);
  37.  
  38.  
  39.             //  check if the point is in the triangle or not
  40.             bool inTriangle = Vector3D.PointTriangle(vp, va, vb, vc);
  41.             Console.Write("Is the point {0} inside the triangle\n\n{1} {2} {3} ? \t", p, a, b, c);
  42.             Console.WriteLine(inTriangle);
  43.             Console.WriteLine();
  44.         }
  45.     }
  46. }
  47.  
  48. using System;
  49.  
  50. namespace T2.CheckPointVsTriangle
  51. {
  52.     class Vector3D
  53.     {
  54.         private double x;
  55.         private double y;
  56.         private double z;
  57.  
  58.         public double X
  59.         {
  60.             get
  61.             {
  62.                 return this.x;
  63.             }
  64.         }
  65.  
  66.         public double Y
  67.         {
  68.             get
  69.             {
  70.                 return this.y;
  71.             }
  72.         }
  73.  
  74.         public double Z
  75.         {
  76.             get
  77.             {
  78.                 return this.z;
  79.             }
  80.         }
  81.  
  82.         public double Length
  83.         {
  84.             get
  85.             {
  86.                 double result = Math.Sqrt(this.X * this.X + this.Y * this.Y + this.Z * this.Z);
  87.                 return result;
  88.             }
  89.         }
  90.  
  91.         public Vector3D(double x, double y, double z)
  92.         {
  93.             this.x = x;
  94.             this.y = y;
  95.             this.z = z;
  96.         }
  97.  
  98.         public static Vector3D operator +(Vector3D first, Vector3D second)
  99.         {
  100.             double resultX = first.X + second.X;
  101.             double resultY = first.Y + second.Y;
  102.             double resultZ = first.Z + second.Z;
  103.             Vector3D result = new Vector3D(resultX, resultY, resultZ);
  104.  
  105.             return result;
  106.         }
  107.  
  108.         public static Vector3D operator -(Vector3D first, Vector3D second)
  109.         {
  110.             double resultX = first.X - second.X;
  111.             double resultY = first.Y - second.Y;
  112.             double resultZ = first.Z - second.Z;
  113.             Vector3D result = new Vector3D(resultX, resultY, resultZ);
  114.  
  115.             return result;
  116.         }
  117.  
  118.         public static double DotProduct(Vector3D first, Vector3D second)
  119.         {
  120.             double result = first.X * second.X + first.Y * second.Y + first.Z * second.Z;
  121.  
  122.             return result;
  123.         }
  124.  
  125.         public static Vector3D CrossProduct(Vector3D first, Vector3D second)
  126.         {
  127.             double resultX = first.Y * second.Z - first.Z * second.Y;
  128.             double resultY = first.Z * second.X - first.X * second.Z;
  129.             double resultZ = first.X * second.Y - first.Y * second.X;
  130.             Vector3D result = new Vector3D(resultX, resultY, resultZ);
  131.  
  132.             return result;
  133.         }
  134.  
  135.         public static bool SameSide(Vector3D p, Vector3D v1, Vector3D v2, Vector3D v3)
  136.         {
  137.             Vector3D cp1 = CrossProduct(v2-v1, p-v1);
  138.             Vector3D cp2 = CrossProduct(v2-v1, v3-v1);
  139.             bool result=DotProduct(cp1, cp2) >= 0;
  140.  
  141.             return result;
  142.         }
  143.  
  144.         public static bool PointTriangle(Vector3D p, Vector3D a, Vector3D b, Vector3D c)
  145.         {
  146.             bool result= SameSide(p, a, b, c) && SameSide(p, b, c, a) && SameSide(p, c, a, b);
  147.             return result;
  148.         }
  149.  
  150.         public override string ToString()
  151.         {
  152.             string result = string.Format("({0}, {1}, {2})", this.X, this.Y, this.Z);
  153.             return result;
  154.         }
  155.     }
  156. }
  157.  
  158. using System;
  159.  
  160. namespace T2.CheckPointVsTriangle
  161. {
  162.     class Point
  163.     {
  164.         public double X { get; set; }
  165.         public double Y { get; set; }
  166.         public double Z { get; set; }
  167.  
  168.         public Point(double x, double y, double z)
  169.         {
  170.             this.X = x;
  171.             this.Y = y;
  172.             this.Z = z;
  173.         }
  174.  
  175.         public override string ToString()
  176.         {
  177.             string result = string.Format("({0}, {1})", this.X, this.Y, this.Z);
  178.             return result;
  179.         }
  180.     }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment