Skip to content

Instantly share code, notes, and snippets.

@kgabis
Last active December 28, 2015 23:19
Show Gist options
  • Save kgabis/7578604 to your computer and use it in GitHub Desktop.
Save kgabis/7578604 to your computer and use it in GitHub Desktop.
Colinear points tests.
//
// colinear.cpp
// AlgosLabs
//
// Created by Krzysztof Gabis on 20.11.2013.
// Copyright (c) 2013 Krzysztof Gabis. All rights reserved.
//
#include "colinear.h"
#include <math.h>
#include <vector>
#include <map>
#include <algorithm>
#include <math.h>
using std::vector;
using std::map;
struct vec2{
double x,y;
vec2 operator-(vec2 r) { return vec2(x-r.x, y-r.y); }
vec2 operator+(vec2 r) { return vec2(x+r.x, y+r.y); }
double operator%(vec2 r) { return x*r.x + y*r.y; }
vec2() {}
double operator^(vec2 r) { return x*r.y-y*r.x; }
double length() { return sqrt(x*x + y*y); }
vec2(double a,double b) { x=a; y=b; }
vec2 inverse() { return vec2(-x, -y); }
double theta() { return atan2(y, x); }
bool isInLowerHalf() { return y < 0 || (y == 0 && x < 0); }
};
void load_points(vector<vec2> *points) {
double x, y;
FILE *fp = fopen("colinear.csv", "r");
if (!fp) {
puts("File not found!");
return;
}
while(fscanf(fp, "%lf,%lf\n", &x, &y) != EOF) {
points->push_back(vec2(x, y));
}
printf("Points count: %lu\n", points->size());
}
typedef map<double, int>::value_type map_vt;
int find_max_colinear(vector<vec2> &points) {
int totalMax = 0;
char buf[10];
for (int i = 0; i < points.size(); i++) {
map<double, int> thetaCountMap;
vec2 &referencePoint = points[i];
for (int j = 0; j < points.size(); j++) {
if (i == j) {
continue;
}
vec2 point = points[j];
point = point - referencePoint;
if (point.isInLowerHalf()) {
point = point.inverse();
}
double theta = point.theta();
if (theta == -0) theta = 0;
double newTheta = 0;
sprintf(buf, "%.4lf", theta);
sscanf(buf, "%lf", &newTheta);
thetaCountMap[newTheta] = thetaCountMap[newTheta] + 1;
}
int max = std::max_element(thetaCountMap.begin(),
thetaCountMap.end(),
[](map_vt &a, map_vt& b){ return a.second < b.second; })->second;
totalMax = std::max(max, totalMax);
}
return totalMax + 1; // + 1 to account for point being tested
}
int main(int argc, const char **argv) {
vector<vec2> points;
load_points(&points);
int max = find_max_colinear(points);
printf("Max colinear: %d\n", max);
return 0;
}
11.397 13.000
39.498 44.932
43.759 49.774
0.018 0.069
26.400 30.048
60.239 68.501
65.563 74.551
52.615 59.837
27.822 31.664
65.432 74.401
96.881 110.139
51.490 58.559
84.765 96.371
56.919 64.728
71.903 81.755
12.474 14.224
81.569 92.739
86.434 98.267
96.212 109.378
1.169 1.378
76.549 87.034
33.026 37.578
86.978 98.884
86.148 97.942
83.588 95.033
89.310 101.534
12.498 14.251
65.184 74.120
57.827 65.759
63.375 72.064
89.534 101.789
2.060 2.390
93.736 106.564
93.561 106.365
31.730 36.105
22.178 25.251
11.548 13.172
29.209 33.241
67.926 77.235
41.437 47.135
16.312 18.585
52.144 59.302
46.126 66.620
31.087 45.044
39.634 57.305
27.692 40.173
9.253 13.719
6.684 10.033
34.954 50.592
92.535 133.202
67.051 96.641
47.312 68.322
18.597 27.125
16.656 24.340
30.810 44.647
74.755 107.693
89.110 128.289
43.334 62.615
88.525 127.449
52.084 75.167
56.437 89.919
56.113 89.405
74.239 118.192
65.642 104.539
79.595 126.699
83.115 132.290
9.835 15.905
45.154 71.998
66.511 105.918
54.450 86.763
83.091 132.252
68.314 108.782
11.090 17.897
71.835 114.374
69.957 111.392
11.407 18.401
45.078 71.879
0.193 0.591
69.011 109.889
84.910 135.140
2.158 3.712
70.984 113.022
29.139 46.564
11.549 18.627
90.318 143.730
53.493 85.243
78.620 125.150
12.220 19.693
99.678 158.595
42.283 74.621
43.771 77.231
57.142 100.687
44.788 79.015
46.161 81.423
27.208 48.176
79.437 139.796
19.063 33.889
3.103 5.890
50.586 89.185
54.981 96.895
82.440 145.064
82.119 144.501
16.248 28.950
1.070 2.325
17.137 30.510
68.298 120.256
86.769 152.659
46.236 81.555
68.526 120.656
24.445 43.329
41.613 63.220
49.492 75.131
52.880 80.254
29.025 44.190
91.121 138.067
84.389 127.889
53.045 102.299
43.362 83.732
63.117 121.612
21.065 40.977
24.744 48.032
63.107 121.591
4.424 9.069
1.972 4.367
51.073 98.518
10.496 20.712
92.623 178.189
2.695 5.754
68.853 132.611
54.083 104.288
32.186 62.302
53.723 103.598
14.651 28.679
52.871 101.965
31.364 60.725
38.638 74.673
46.521 48.501
51.138 53.294
68.822 71.649
86.031 89.510
21.456 22.486
79.283 82.506
26.931 28.168
5.748 6.182
75.734 78.822
55.299 57.613
5.855 6.293
83.403 86.782
71.964 74.910
6.746 7.218
74.123 77.150
23.699 24.814
5.480 5.904
70.486 73.375
90.649 94.303
42.096 57.262
87.779 118.919
47.570 64.650
33.034 45.031
38.827 52.850
11.265 15.651
76.917 104.258
20.441 28.036
71.003 96.277
89.471 119.546
78.137 104.509
37.774 50.960
99.212 132.469
16.725 23.035
3.518 5.512
99.189 132.438
12.958 18.037
24.557 33.425
38.888 52.438
34.055 46.026
13.042 18.148
91.706 122.511
75.513 101.028
98.723 131.820
91.683 99.034
50.260 54.395
28.124 30.541
89.523 96.707
59.942 64.829
47.133 51.026
52.165 56.449
6.381 7.111
49.041 53.082
33.671 36.518
64.812 70.078
55.291 59.817
52.756 57.086
44.766 48.475
54.183 58.624
83.089 89.774
52.997 57.345
76.723 82.913
81.090 87.619
36.015 39.045
3.340 3.833
26.673 28.977
90.424 97.678
32.609 35.374
84.252 91.026
85.040 91.876
9.808 10.804
71.701 77.501
62.921 68.039
94.770 102.361
44.234 47.901
76.347 82.508
43.037 46.612
47.342 51.251
41.952 45.443
7.473 8.287
95.461 103.106
20.118 21.914
76.488 82.660
42.029 45.526
19.373 21.111
72.115 77.948
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment