Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace T2.CheckPointVsTriangle
- {
- class PointInTriangle
- {
- static void Main()
- {
- Console.WriteLine("Given 3 points A, B and C, forming triangle, and a point P");
- Console.WriteLine("Check if the point P is in the triangle or not\n");
- // triangle points
- Point a = new Point(-3.45, -2.12);
- Point b = new Point(6.82, 1.9);
- Point c = new Point(3.1, 10.4);
- // points to check
- // Point p = new Point(-4.8, 9.2);
- Point p = new Point(3.2, 4.82);
- // find outermost coordinates of the triangle
- double minX = Math.Min(Math.Min(a.X,b.X),c.X);
- double maxX = Math.Max(Math.Max(a.X,b.X),c.X);
- double minY = Math.Min(Math.Min(a.Y,b.Y),c.Y);
- double maxY = Math.Max(Math.Max(a.Y,b.Y),c.Y);
- // check if the point is in the triangle or not
- bool inTriangle = minX < p.X && p.X < maxX && minY < p.Y && p.Y < maxY;
- Console.Write("Is the point {0} inside the triangle\n\n{1} {2} {3} ? \t", p, a, b, c);
- Console.WriteLine(inTriangle);
- Console.WriteLine();
- }
- }
- }
- using System;
- namespace T2.CheckPointVsTriangle
- {
- class Point
- {
- public double X { get; set; }
- public double Y { get; set; }
- public Point(double x, double y)
- {
- this.X = x;
- this.Y = y;
- }
- public override string ToString()
- {
- string result = string.Format("({0}, {1})", this.X, this.Y);
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment