-
Notifications
You must be signed in to change notification settings - Fork 1
/
IsolatedSegments209.cpp
128 lines (124 loc) · 3.07 KB
/
IsolatedSegments209.cpp
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <iostream>
#include <cstdlib>
#include <math.h>
#include <stack>
#include <iomanip>
#include <algorithm>
//#include <fstream>
using namespace std;
class Point {
public:
int x, y;
Point(int a, int b);
Point();
};
Point::Point(int a, int b) {
x = a;
y = b;
}
Point::Point() {
x = y = 0;
}
Point startPoint[100];
Point endPoint[100];
Point smallest;
int smallestIndex;
int determinant(Point p1, Point p2) {
return (p1.x*p2.y - p2.x*p1.y);
}
int AntiClockWise(Point p1, Point p2, Point p3) {
int val = determinant(p1, p2) + determinant(p2, p3) + determinant(p3, p1);
if (val > 0)
return 1;
else if (val < 0)
return -1;
else
return 0;
}
double distance(Point p1, Point p2) {
return sqrt(pow(p2.x - p1.x, 2) + pow(p2.y - p1.y, 2));
}
bool onSegment(Point p, Point q, Point r){//checks if point q lies on line pr
if (q.x <= max(p.x, r.x) && q.x >= min(p.x, r.x) &&
q.y <= max(p.y, r.y) && q.y >= min(p.y, r.y))
return true;
return false;
}
int main() {
int n;
int ctr = 1;
cin >> n;
//ofstream myfile;
//myfile.open("output.txt");
bool flag[100];
for (int z = 0; z < n; z++) {
int m;
cin >> m;
fill(begin(flag), end(flag), false);
for (int i = 0; i < m; i++) {
cin >> startPoint[i].x >> startPoint[i].y >> endPoint[i].x >> endPoint[i].y;
}
int ctr = 0;
for (int i = 0; i < m; i++) {
for (int j = i + 1; j < m; j++) {
int ctr = AntiClockWise(startPoint[i], endPoint[i], startPoint[j]);
int ctr1 = AntiClockWise(startPoint[i], endPoint[i], endPoint[j]);
int ctr2 = AntiClockWise(startPoint[j], endPoint[j], startPoint[i]);
int ctr3 = AntiClockWise(startPoint[j], endPoint[j], endPoint[i]);
if (ctr!=ctr1 && ctr2!=ctr3 ) {
flag[i] = true;
flag[j] = true;
}
if (ctr == 0) {//on the same line. startPoint[i],endPoint[i],startPoint[j] are coolinear
//check if the startpoint[j] lies in between
Point a = startPoint[i];
Point b = endPoint[i];
Point c = startPoint[j];
if (onSegment(a,c,b)) {
flag[i] = true;
flag[j] = true;
continue;
}
}
if (ctr1 == 0) {//startPoint[i],endPoint[i],endPoint[j]
double lineDist = distance(startPoint[i], endPoint[i]);
Point a = startPoint[i];
Point b = endPoint[i];
Point c = endPoint[j];
double dotproduct = (c.x - a.x) * (b.x - a.x) + (c.y - a.y)*(b.y - a.y);
if (onSegment(a, c, b)) {
flag[i] = true;
flag[j] = true;
continue;
}
}
if (ctr2 == 0) {//startPoint[j],endPoint[j],startPoint[i]
//check if the startpoint[i] lies in between
Point a = startPoint[j];
Point b = endPoint[j];
Point c = startPoint[i];
if (onSegment(a, c, b)) {
flag[i] = true;
flag[j] = true;
continue;
}
}
if (ctr3 == 0) {//startPoint[j],endPoint[j],endPoint[i]
Point a = startPoint[j];
Point b = endPoint[j];
Point c = endPoint[i];
if (onSegment(a, c, b)) {
flag[i] = true;
flag[j] = true;
continue;
}
}
}
if (!flag[i])
ctr++;
}
cout << ctr << endl;
//myfile << ctr << endl;
}
return 0;
}