1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
const double INF = 1000000.00; //in case line is in the form of x = number
class Point{ //declare class for point
public:
double x, y;
void create(double x1, double y1){
x = x1;
y = y1;
}
void setOrigin(){
x = 0;
y = 0;
}
};
class Triangle{ //declare class for Triangle
public:
Point a, b;
void create(Point a1, Point b1){
a = a1;
b = b1;
}
};
class Equation{ //declare class of equation
public:
double slope;
double intercept; //intercept point, meaning b in y=ax + b
void create(double slope1, double intercept1){
slope = slope1;
intercept = intercept1;
}
double returnY(double x){
double y = slope*x + intercept;
}
};
double findSlope (Point a, Point b){ //find slope in a line of a and b
double slope;
double Dy = a.y - b.y;
double Dx = a.x - b.x;
if (Dx==0){
slope = INF;
}
else{
slope = Dy / Dx;
}
return slope;
}
double findIntercept (Point a, Point b){ //find Intercept (b in y=ax+b)
double slope = findSlope (a, b);
double intercept = a.y - slope*a.x;
return intercept;
}
Equation findEquation(Point a, Point b){ //find equation of a line
Equation result;
double slope = findSlope(a, b);
double intercept = findIntercept (a, b);
result.create(slope, intercept);
return result;
}
Point findMeetPoint(Equation a, Equation b){ //find the intercept point between to lines
Point result;
double x = (a.intercept - b.intercept) / (b.slope - a.slope);
double y = a.returnY(x);
result.create(x, y);
}
bool isInTriangle(Triangle current, Point check){ //finally check if it's in the triangle or not
bool is = false;
Point origin;
origin.setOrigin();
double slopeEdge = findSlope(current.a, origin);
double slopeCheck = findSlope(check, origin);
double slopeBase = findSlope(current.a, origin);
Equation thirdLine = findEquation(current.a, current.b);
Equation checkLine = findEquation(check, origin);
Point meet = findMeetPoint(checkLine, thirdLine);
if (slopeEdge>slopeCheck && slopeBase<slopeCheck && findMeetPoint(findEquation(check, origin), findEquation(current.a,current.b).y>check.y) {
is = true;
}
return is;
}
int main(){ //read input and print answer
int x, y;
Point check, a, b;
Triangle ab;
cin>>x>>y;
check.create(x, y);
cin>>x>>y;
a.create(x, y);
cin>>x>>y;
b.create(x, y);
ab.create(a, b);
if (isInTriangle(ab, check)){
cout<<"Inside";
}
else{
cout<<"Outside";
}
}
|