Visibility Graph Algorithm
Visibility Graph Algorithm
I. ABSTRACT
II. INTRODUCTION
A. Visibility Graph
B. Runtime
D. Analysis of Correctness
The asymptotic runtime of Lee’s algorithm is analyzed
in the following. The algorithm has four for-loops as well We begin our proof of correctness of Lee’s O(n2 log n)
as the operations of the optimal sorting data structure. algorithm by defining the components of the algorithm.
An outer for-loop iterates once through n = |V | = 2|E|
points, finding the visibility tree for every point.
Definition 1. A visibility graph Gv = (V, Ev ) is the set
Within the outer for loop, each end point pair for of all vertices V in input graph G, and the set of edges
every |E| line segments are inserted into the optimal Ev which connects two vertices vi , vj ∈ V without inter-
sorting data structure A. Insert, delete, and find all secting any obstacles, for all vi , vj ∈ V . We assume that
take O(log n) time using a probabilistic structure such two endpoints i and j of the same line are also considered
as a skip list, or balance binary search tree, such as visible. We restrict our obstacle set to the |E| disjoint
an AVL tree. Thus, the insertion time for A takes line segments, in any direction.
2|E| log n = O(n log n).
Definition 2. The line sweep vector ~s is a vector with
its origin at some point c ∈ V that rotates starting from
Next, the sweep line edge list Es is initialized by
direction î a full 2π radians.
checking all |E| edges in G for intersection with ~s = î.
The edge list Es uses the same data structure as A, and Definition 3. A line segment li is an obstacle in the
thus all insertions take O(log n) time. In the worse case, 2 dimensional problem space defined between vertices vi
all |E| edges intersect ~s at some θi , so the total runtime and vi−1 .
for this step is O(|E| log |E|). There are twice as many
vertices as edges, and so because |E| < |V |, this runtime Definition 4. The set Es contains all li that intersect
is asymptotically overshadowed by the previous step and with ~s originating at point c, ordered in decreasing Eu-
can be ignored. clidean distance from c to li .
4
IV. RESULTS line ~s would visit and so it would be inserted into the
skip list Es with the distance d1 from its first endpoint
A. Implementation to c. Next, the scan line would visit the first endpoint
of l2 and it would add l2 to Es with distance d2 . Thus,
Es would have as its first ordered line segment l1 , and
The O(n2 log n) algorithm was implemented in
for its seconds l2 . But by definition 4, Es should have
C++ and visualized/animated using the open source,
as its first line segment the segment closest to c, and at
cross-platform CImg graphics library. With the graph-
scan line ~s0 the closest intersecting line segment is now
ics library we were able to visually verify geometric
actually l2 . As it is now clear, in our current example
results such as shown in Figure 3. The full source
the ordering of Es would be incorrect at location ~s0
code is appended at the end of this paper and is
unless there was some way to update the value of l1 to
available as an open source project online at https:
reflect its distance from c with respect to θi of ~s0 .
//github.com/davetcoleman/visibility_graph
C. Precision Errors
run time is both the worst- and average-case for this al-
To calculate the numerical time usage of this algo- gorithm because all points are always added to A and E
rithm, the source code was modified to automatically and all points are always visited to generate their indi-
generate a set of n line segments. To test the runtime vidual visibility tree.
with exponentially increasing problem space it was
instrumented to generate approximately n = 10x line
segments. However, to ensure a useful test set was
generated without intersection, each line segment was
constrained to a grid area. Within each line segment’s
grid, padding was added to allow more visibility between
grids. Additionally, 4 shapes were used inside the grids:
a horizontal, vertical, diagonal increasing and diagonal
decreasing line segment. Which of the 4 was chosen was
decided at random, such that every test was run on a
problem set with a high probability of being unique.
Because of the gridded nature of the problem space,
in reality only nx = (b(10n )1/2 c)2 line segments were
added. An example of an automatically generated
problem space is shown in Figure 5.
The results of the atomic operations measurements are Further visual results of the algorithm running for
shown in Figure 6. Our data showed performance that n = 100 line segments is shown in Figure 7 and for
was very tightly bound to a run time of O(n2 log n). This n = 1000 line segments in Figure 8.
7
V. CONCLUSIONS
[1] Burcin Cem Arabacioglu. Using fuzzy inference system [8] John Kitzinger and Computer Engineering. The visibil-
for architectural space analysis. Appl. Soft Comput., ity graph among polygonal obstacles: a comparison of
10(3):926–937, June 2010. algorithms, 2003.
[2] Herbert Edelsbrunner and Leonidas J. Guibas. Topolog- [9] Der-Tsai Lee. Proximity and reachability in the plane.
ically sweeping an arrangement. Journal of Computer PhD thesis, Champaign, IL, USA, 1978. AAI7913526.
and System Sciences, 38(1):165 – 194, 1989. [10] Jae-Ha Lee, Sung Yong Shin, and Kyung-Yong Chwa.
[3] Robert W. Floyd. Algorithm 97: Shortest path. Com- Visibility-based pursuit-evasion in a polygonal room with
mun. ACM, 5(6):345–, June 1962. a door. In Proceedings of the fifteenth annual symposium
[4] Subir Kumar Ghosh and David M. Mount. An output on Computational geometry, SCG ’99, pages 281–290,
sensitive algorithm for computing visibility graphs. In New York, NY, USA, 1999. ACM.
Proceedings of the 28th Annual Symposium on Founda- [11] Tomás Lozano-Pérez and Michael A. Wesley. An al-
tions of Computer Science, SFCS ’87, pages 11–19, Wash- gorithm for planning collision-free paths among polyhe-
ington, DC, USA, 1987. IEEE Computer Society. dral obstacles. Commun. ACM, 22(10):560–570, October
[5] P.E. Hart, N.J. Nilsson, and B. Raphael. A formal basis 1979.
for the heuristic determination of minimum cost paths. [12] William Pugh. Skip lists: a probabilistic alternative to
Systems Science and Cybernetics, IEEE Transactions on, balanced trees. Commun. ACM, 33(6):668–676, June
4(2):100 –107, july 1968. 1990.
[6] Donald B. Johnson. A note on dijkstra’s shortest path [13] Emo Welzl. Constructing the visibility graph for n-line
algorithm. J. ACM, 20(3):385–388, July 1973. segments in o(n2) time. Information Processing Letters,
[7] Sertac Karaman and Emilio Frazzoli. Sampling-based al- 20(4):167 – 171, 1985.
gorithms for optimal motion planning. Int. J. Rob. Res.,
30(7):846–894, June 2011.
Dave Coleman CSCI 5454 (Prof. Clauset 4:00 MW): Visibility Graph
Index:
vgraph.cpp
skiplist.h
line.h
line.cpp
point.h
point.cpp
geometry.h
plot.m
data.cvs
vgraph.cpp
#i n c l u d e ”CImg . h” // I n c l u d e CImg l i b r a r y h e a d e r . 1
#i n c l u d e <i o s t r e a m > 2
#i n c l u d e ” l i n e . h” 3
#i n c l u d e ” p o i n t . h” 4
#i n c l u d e ” s k i p l i s t . h” 5
#i n c l u d e <cmath> 6
7
u s i n g namespace c i m g l i b r a r y ; 8
u s i n g namespace s t d ; 9
10
const u n s i g n e d c h a r WHITE [ ] = { 2 5 5 , 2 5 5 , 255 } ; 11
const u n s i g n e d c h a r GREY [ ] = { 1 0 0 , 1 0 0 , 100 } ; 12
const u n s i g n e d c h a r BLACK [ ] = { 0 , 0 , 0 } ; 13
const u n s i g n e d c h a r RED [ ] = { 2 5 5 , 0 , 0 } ; 14
const u n s i g n e d c h a r GREEN [ ] = { 0 , 2 5 5 , 0 } ; 15
const u n s i g n e d c h a r BLUE [ ] = { 0 , 0 , 2 5 5 } ; 16
const int s c r e e n s i z e = 800; 17
18
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− 19
// P r o t o t y p e s 20
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− 21
v o i d vgraph ( d o u b l e o r d e r ) ; 22
d o u b l e v e c t o r s A n g l e ( i n t x , i n t y , i n t basex , i n t b a s e y ) ; 23
double d i s t a n c e ( Point ∗ a , Point ∗ b ) ; 24
25
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− 26
// Main p r o c e d u r e 27
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− 28
i n t main ( ) 29
{ 30
c o u t << e n d l << e n d l << ” V i s i b i l i t y Graph by Dave Coleman −−−−−−−−−−−−−−−−−−−− ” << e n 31
dl
<< e n d l ;
32
f o r ( d o u b l e o r d e r = 2 ; o r d e r < 3 ; o r d e r += 0 . 5 ) 33
{ 34
vgraph ( o r d e r ) ; 35
} 36
37
r e t u r n EXIT SUCCESS ; 38
} 39
40
v o i d vgraph ( d o u b l e o r d e r ) 41
{ 42
// V a r i a b l e s −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− 43
44
// Atomic o p e r a t i o n c o u n t e r 45
at om i c = 0 ; 46
Page 1 of 22
Dave Coleman CSCI 5454 (Prof. Clauset 4:00 MW): Visibility Graph
47
// G r a p h i c s : 48
bool v i s u a l = true ; 49
bool l i v e = true ; 50
51
CImg<u n s i g n e d char> img ( s c r e e n s i z e , s c r e e n s i z e , 1 , 3 , 2 0 ) ; 52
CImgDisplay d i s p ( img , ” V i s i b i l i t y Graph” ) ; // D i s p l a y t h e m o d i f i e d image on t h e 53
screen
54
// L i n e s e gm e nt s : 55
i n t s i z e = pow ( 1 0 . 0 , o r d e r ) ; 56
int row col = sqrt ( s i z e ) ; 57
int seg = row col ∗ row col ; 58
59
// C o o r d i n a t e s : 60
d o u b l e width = s c r e e n s i z e / r o w c o l ; // s i z e o f each g r i d box 61
d o u b l e margin = 0 . 1 ∗ width ; // padding i n s i d e each box 62
d o u b l e top , bottom , l e f t , r i g h t ; // c o o r d i n a t e s o f box with padding 63
64
// G e n e r a t e s p a c e f o r SEG number o f l i n e s 65
Line ∗ s e g s [ seg ] ; 66
67
// Track what i n d e x we a r e on 68
i n t index = 0 ; 69
70
// Now g e n e r a t e s e g l i n e s e g me n ts 71
f o r ( i n t x = 0 ; x < r o w c o l ; ++x ) 72
{ 73
f o r ( i n t y = 0 ; y < r o w c o l ; ++y ) 74
{ 75
top = y∗ width + margin ; 76
bottom = ( y+1)∗ width − margin ; 77
l e f t = x∗ width + margin ; 78
r i g h t = ( x+1)∗ width − margin ; 79
80
// C r e a t e l i n e segment i n box o f s i z e width ∗ width 81
// x1 , y1 , x2 , y2 82
s w i t c h ( rand ( ) % 4 ) 83
{ 84
c a s e 0 : // v e r t i c l e l i n e 85
s e g s [ i n d e x ] = new L i n e ( l e f t , top , l e f t , bottom ) ; 86
break ; 87
c a s e 1 : // h o r i z o n t a l l i n e 88
s e g s [ i n d e x ] = new L i n e ( l e f t , top , r i g h t , top ) ; 89
break ; 90
c a s e 2 : // d i a g o n a l l e f t t o r i g h t 91
s e g s [ i n d e x ] = new L i n e ( l e f t , top , r i g h t , bottom ) ; 92
break ; 93
case 3: 94
s e g s [ i n d e x ] = new L i n e ( l e f t , bottom , r i g h t , top ) ; 95
break ; 96
} 97
98
i n d e x ++; 99
} 100
} 101
102
// c o u t << ”SEGS ” << s e g << ” INDEX ” << i n d e x << e n d l ; 103
104
/∗ 105
Line s e g s [ ] = 106
{ 107
Line (280 ,300 ,330 ,120) , // 0 first 108
Line (450 ,150 ,280 ,330) , // 1 second 109
Line (400 ,150 ,401 ,190) , // 2 third , l a t e r 110
Line (400 ,400 ,450 ,200) , // 3 far right 111
Line (350 ,350 ,350 ,450) , // 4 112
Line (10 ,200 ,100 ,215) , // 5 113
Page 2 of 22
Dave Coleman CSCI 5454 (Prof. Clauset 4:00 MW): Visibility Graph
Page 3 of 22
Dave Coleman CSCI 5454 (Prof. Clauset 4:00 MW): Visibility Graph
182
// Add p o i n t e r s t o a l l p o i n t s back t o p a r e n t l i n e 183
l −>a−>p a r e n t L i n e = l ; 184
l −>b−>p a r e n t L i n e = l ; 185
186
// R e s e t v i s i t e d f l a g s 187
l −>v i s i t e d = f a l s e ; 188
l −>v i s i t e d S t a r t P o i n t = f a l s e ; 189
190
i f ( visual ) 191
img . d r a w l i n e ( l −>a−>x , l −>a−>y , l −>b−>x , l −>b−>y , WHITE) ; 192
193
i f ( ! ( i == c e n t e r i d && i s P o i n t A ) ) // p o i n t i s not l i n e A 194
{ 195
i f ( visual ) 196
img . d r a w c i r c l e ( l −>a−>x , l −>a−>y , 2 , WHITE) ; 197
198
// C a l c u l a t e t h e a n g l e from c e n t e r l i n e : 199
l −>a−>t h e t a = v e c t o r s A n g l e ( l −>a−>x , l −>a−>y , c e n t e r −>x , c e n t e r −>y ) ; 200
201
// S o r t t h e v e r t i c i e s : 202
a n g l e L i s t . add ( l −>a ) ; 203
204
// c o u t << ”Added A f o r l i n e ” << i << ” t h e t a ” << l −>a−>t h e t a << e n d l ; 205
// c o u t << ”POINT ” ; l −>a−>p r i n t ( ) ; c o u t << e n d l ; 206
} 207
208
i f ( ! ( i == c e n t e r i d && i s P o i n t A == f a l s e ) ) // p o i n t i s not l i n e B 209
{ 210
i f ( visual ) 211
img . d r a w c i r c l e ( l −>b−>x , l −>b−>y , 2 , WHITE) ; 212
213
// C a l c u l a t e t h e a n g l e from c e n t e r l i n e : 214
l −>b−>t h e t a = v e c t o r s A n g l e ( l −>b−>x , l −>b−>y , c e n t e r −>x , c e n t e r −>y ) ; 215
216
// S o r t t h e v e r t i c i e s : 217
a n g l e L i s t . add ( l −>b ) ; 218
// c o u t << ”Added B f o r l i n e ” << i << ” t h e t a ” << l −>b−>t h e t a << e n d l ; 219
220
// c o u t << ”POINT ” ; l −>b−>p r i n t ( ) ; c o u t << e n d l ; 221
} 222
223
// c o u t << e n d l ; 224
} 225
226
227
// Test S k i p L i s t 228
// c o u t << ” Angle L i s t − p o i n t s o r d e r e d CC from b a s e l i n e ” ; 229
// a n g l e L i s t . p r i n t A l l ( ) ; 230
231
232
// I n i t i a l i z e Edge L i s t Of L i n e s −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− 233
f o r ( i n t i = 0 ; i < s e g ; ++i ) 234
{ 235
++at om ic ; 236
237
l = s e g s [ i ] ; // g e t n e x t l i n e t o c h e c k 238
239
// c h e c k i f t h e c u r r e n t l i n e i s c o n n e c t e d t o t h e c e n t e r p o i n t 240
i f ( l −>i d == ( ( L i n e ∗ ) c e n t e r −>p a r e n t L i n e )−>i d ) 241
{ 242
// one c e n t e r ’ s l i n e 243
// c o u t << ”ONE CENTER ’ S LINE ! ! ! ” << e n d l ; 244
} 245
else 246
{ 247
// Check each l i n e and s e e i f i t c r o s s e s s c a n l i n e 248
double xi , y i ; 249
Page 4 of 22
Dave Coleman CSCI 5454 (Prof. Clauset 4:00 MW): Visibility Graph
250
l −>c e n t e r i n t e r c e p t ( x i , y i ) ; // t h e s e a r e r e f e r e n c e p a r a m e t e r s
251
// Now we know t h a t x i , y i i s on c e n t e r l i n e . 252
// Next we c h e c k i f X i s between a & b . We know a . x > b . x , t h u s : 253
i f ( l −>a−>x >= x i && l −>b−>x <= x i ) 254
{ 255
// c h e c k t h a t x i > c e n t e r −>x 256
i f ( x i >= c e n t e r −>x ) 257
{ 258
259
// I t d o e s i n t e r s e c t 260
e d g e L i s t . add ( l ) ; 261
262
// Mark a s opened , somewhere on l i n e 263
l −>v i s i t e d = t r u e ; 264
265
// V i s u a l i z e : 266
i f ( visual ) 267
img . d r a w l i n e ( l −>a−>x , l −>a−>y , l −>b−>x , l −>b−>y , GREEN) ; 268
} 269
} 270
} 271
} 272
273
if ( live ) 274
d i s p . d i s p l a y ( img ) ; 275
276
// c o u t << ” Edge L i s t : ” ; 277
// e d g e L i s t . p r i n t A l l ( ) ; 278
279
// Sweep −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− 280
281
// s l e e p ( 1 ) ; 282
// u s l e e p ( 5 0 0 ∗ 1 0 0 0 ) ; 283
f o r ( i n t i = 0 ; i < 2∗ s e g − 1 ; ++i ) 284
{ 285
++at om ic ; 286
287
// c o u t << ”\n\n\n −−−−−−−−−−−−−−− STARTING NEW SWEEP −−−−−−−−−−−−−−−−−− \n\n\n ” ; 288
289
// c o u t << ”SWEEP VERTEX ” << i << e n d l ; 290
// i f ( i > 0 ) 291
// b r e a k ; 292
293
// t a k e t h e f i r s t v e r t e x i n a n g u l a r o r d e r 294
p = a n g l e L i s t . pop ( ) ; 295
// c o u t << ” Sweep a t ” ; p−>p r i n t ( ) ; 296
297
// Update t h e c e n t e r l i n e t o t h e sweep l o c a t i o n and update m, b 298
c e n t e r l i n e −>b = p ; 299
c e n t e r l i n e −>u p d a t e C a l c s ( ) ; 300
301
// Update c e n t e r p o i n t t o c o n t a i n t h e t a between b a s e l i n e and 302
// c u r r e n t p o i n t , s o t h a t our l i n e f u n c t i o n can c a c h e 303
c e n t e r −>t h e t a = p−>t h e t a ; 304
305
// d e c i d e what t o do with i t 306
l = ( L i n e ∗ ) p−>p a r e n t L i n e ; // c a s t i t 307
// c o u t << ”\ t ” ; l −>p r i n t ( ) ; 308
309
// c h e c k i f t h e c u r r e n t l i n e i s c o n n e c t e d t o t h e c e n t e r p o i n t 310
i f ( l −>i d == ( ( L i n e ∗ ) c e n t e r −>p a r e n t L i n e )−>i d ) 311
{ 312
// one c e n t e r ’ s l i n e 313
// i g n o r e 314
} 315
e l s e i f ( l −>v i s i t e d ) // remove i t from e d g e L i s t 316
{ 317
Page 5 of 22
Dave Coleman CSCI 5454 (Prof. Clauset 4:00 MW): Visibility Graph
Page 6 of 22
Dave Coleman CSCI 5454 (Prof. Clauset 4:00 MW): Visibility Graph
// u s l e e p ( 1 ∗ 1 0 0 0 ) ; 385
d i s p . d i s p l a y ( img ) ; 386
} 387
// b r e a k ; 388
// img . f i l l ( 2 0 ) ; 389
// c o u t << o u t e r << e n d l ; 390
} 391
392
i f ( visual ) 393
{ 394
// Redraw o b s t a c l e l i n e s j u s t f o r f u n : 395
f o r ( i n t i = 0 ; i < s e g ; ++i ) 396
{ 397
l = segs [ i ] ; 398
399
img . d r a w l i n e ( l −>a−>x , l −>a−>y , l −>b−>x , l −>b−>y , WHITE) ; 400
img . d r a w c i r c l e ( l −>a−>x , l −>a−>y , 2 , WHITE) ; 401
img . d r a w c i r c l e ( l −>b−>x , l −>b−>y , 2 , WHITE) ; 402
} 403
d i s p . d i s p l a y ( img ) ; 404
405
406
img . s a v e ( ” r e s u l t . png ” ) ; // s a v e t h e image 407
} 408
409
c o u t << s e g << ” , ” << at om ic << e n d l ; 410
411
i f ( visual ) 412
{ 413
// Show window u n t i l u s e r i n p u t : 414
while ( ! disp . i s c l o s e d () ) { 415
i f ( d i s p . is keyESC ( ) ) 416
break ; 417
disp . wait ( ) ; 418
} 419
} 420
421
// Garabage c o l l e c t 422
// d e l e t e [ ] s e g s ; 423
// f r e e ( s e g s ) ; 424
} 425
426
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− 427
// C a l c u l a t e Angle Btw 2 V e c t o r s 428
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− 429
d o u b l e v e c t o r s A n g l e ( i n t x , i n t y , i n t basex , i n t b a s e y ) 430
{ 431
// Convert i n p u t p o i n t x & y t o be v e c t o r s r e l a t i v e t o b a s e p o i n t 432
d o u b l e x2 = d o u b l e ( x − b a s e x ) ; 433
d o u b l e y2 = d o u b l e ( y − b a s e y ) ; 434
435
// Hard code s c a n l i n e t o p o i n t r i g h t : 436
d o u b l e x1 = s q r t ( x2 ∗ x2 + y2 ∗ y2 ) ; // make i t with r a t i o ? 437
d o u b l e y1 = 0 . 0 ; 438
439
// c o u t << ” x1 : ” << x1 << ” − y1 : ” << y1 << e n d l ; 440
// c o u t << ” x2 : ” << x2 << ” − y2 : ” << y2 << e n d l ; 441
442
d o u b l e s t u f f = ( ( x1 ∗ x2 ) +(y1 ∗ y2 ) ) / ( s q r t ( x1 ∗ x1+y1 ∗ y1 ) ∗ s q r t ( x2 ∗ x2+y2 ∗ y2 ) ) ; 443
// c o u t << ” S t u f f : ” << s t u f f << e n d l ; 444
445
// C a l c u l a t e a n g l e : 446
double r e s u l t = acos ( s t u f f ) ; 447
// c o u t << ” R e s u l t : ” << r e s u l t << e n d l ; 448
449
// Now add PI i f below m id dl e l i n e : 450
i f ( y >= b a s e y ) 451
r e s u l t = 2∗M PI − r e s u l t ; 452
Page 7 of 22
Dave Coleman CSCI 5454 (Prof. Clauset 4:00 MW): Visibility Graph
453
// c o u t << ” R e s u l t : ” << r e s u l t ∗180/ M PI << ” d e g r e e s ” << e n d l ; 454
455
return r e s u l t ; 456
} 457
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− 458
// D i s t a n c e Btw 2 P o i n t s 459
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− 460
double d i s t a n c e ( Point ∗ a , Point ∗ b ) 461
{ 462
r e t u r n s q r t ( pow ( b−>x − a−>x , 2 . 0 ) + pow ( b−>y − a−>y , 2 . 0 ) ) ; 463
} 464
../visibility graph/vgraph.cpp
Page 8 of 22
Dave Coleman CSCI 5454 (Prof. Clauset 4:00 MW): Visibility Graph
skiplist.h
/∗ S k i p L i s t 1
CSCI 5454 A l g o r i t h m s 2
Dave Coleman | d a v i d . t . c o l em a n @ c o l o r a d o . edu 3
4
2/2/2012 5
6
Implementation o f Skip L i s t s 7
∗/ 8
9
#i n c l u d e <math . h> 10
#i n c l u d e <i o s t r e a m > 11
#i n c l u d e <c s t d l i b > 12
#i n c l u d e ” node . h” 13
//#i n c l u d e ” p o i n t . h” 14
u s i n g namespace s t d ; 15
16
// −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− 17
// S k i p L i s t C l a s s 18
// −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− 19
t e m p l a t e < c l a s s T> 20
class skiplist { 21
// used f o r t e s t i n g 22
public : 23
i n t maxLevel ; 24
25
private : 26
node<T> ∗ r o o t ; 27
28
// −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−29
// Get Random L e v e l 30
// −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−31
i n t getRandLevel ( ) 32
{ 33
i n t randResult = 1; 34
int level = 0; 35
while ( randResult ) 36
{ 37
38
r a n d R e s u l t = rand ( ) % 2 ; 39
i f ( randResult ) 40
{ 41
++l e v e l ; 42
} 43
44
i f ( l e v e l > maxLevel ) 45
{ 46
r a n d R e s u l t = 0 ; // t o end t h e w h i l e l o o p 47
} 48
} 49
return l e v e l ; 50
51
} 52
// −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−53
// C r e a t e New Node 54
// −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−55
node<T>∗ c r e a t e N o d e ( i n t l e v e l , i n t h e i g h t , T data ) 56
{ 57
// Check i f we a r e below l e v e l 0 58
i f ( l e v e l < 0) 59
{ 60
r e t u r n NULL; 61
} 62
e l s e // make a new node below 63
{ 64
node<T> ∗newNode = new node<T>() ; 65
newNode−>l e v e l = l e v e l ; 66
Page 9 of 22
Dave Coleman CSCI 5454 (Prof. Clauset 4:00 MW): Visibility Graph
newNode−>n e x t = NULL; 67
newNode−>below = c r e a t e N o d e ( l e v e l − 1 , h e i g h t , data ) ; 68
newNode−>h e i g h t = h e i g h t ; 69
newNode−>data = data ; 70
r e t u r n newNode ; 71
} 72
} 73
74
public : 75
76
// C o n s t r u c t o r : 77
s k i p l i s t () 78
{ 79
r o o t = NULL; 80
maxLevel = 0 ; 81
82
s r a n d ( time (NULL) ) ; // s e e d t h e random g e n e r a t o r 83
} 84
// −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−85
// ADD 86
// −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−87
v o i d add ( T data ) 88
{ 89
// c o u t << ”ADD: ” ; 90
// data . p r i n t ( ) ; 91
92
// S p e c i a l Cases −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− 93
94
i f ( ! r o o t ) // no r o o t has been e s t a b l i s h e d y e t 95
{ 96
r o o t = c r e a t e N o d e ( 0 , 0 , data ) ; 97
return ; 98
} 99
100
i f ( r o o t −>data−>v a l u e ( ) > data−>v a l u e ( ) ) // new v a l u e g o e s b e f o r e r o o t 101
{ 102
T temp data = r o o t −>data ; 103
node<T> ∗n = r o o t ; 104
105
f o r ( i n t l = maxLevel ; l >= 0 ; −−l ) 106
{ 107
at om i c += 1 ; 108
// change t h e r o o t t o t h e new v a l u e 109
n−>data = data ; 110
n = n−>below ; 111
} 112
data = temp data ; 113
} 114
115
// R e g u l a r i n s e r t a f t e r r o o t −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− 116
117
// Determine what l e v e l t h i s new node w i l l be a t 118
i n t l e v e l = getRandLevel ( ) ; 119
120
// I f new node i s a t whole new l e v e l , go ahead and update r o o t node t o be h i g h e r 121
i f ( l e v e l > maxLevel ) 122
{ 123
maxLevel ++; 124
node<T> ∗ newRoot = new node<T>() ; 125
newRoot−>data = r o o t −>data ; 126
newRoot−>n e x t = NULL; 127
newRoot−>below = r o o t ; 128
newRoot−>l e v e l = maxLevel ; 129
r o o t = newRoot ; 130
} 131
132
// C r e a t e t h e new node 133
node<T> ∗newNode = c r e a t e N o d e ( l e v e l , l e v e l , data ) ; 134
Page 10 of 22
Dave Coleman CSCI 5454 (Prof. Clauset 4:00 MW): Visibility Graph
135
// Now add t h e node t o t h e l i s t 136
node<T> ∗ i = r o o t ; 137
138
// Loop down t h r o u g h a l l l e v e l s 139
f o r ( i n t l = maxLevel ; l >= 0 ; −−l ) 140
{ 141
at om ic += 1 ; 142
// move f o r w a r d u n t i l we h i t a v a l u e g r e a t e r than o u r s 143
w h i l e ( i −>n e x t != NULL ) 144
{ 145
at om i c += 1 ; 146
i f ( i −>next−>data−>v a l u e ( ) > data−>v a l u e ( ) ) // i n s e r t b e f o r e i . n e x t 147
{ 148
break ; 149
} 150
i = i −>n e x t ; 151
} 152
153
// Check i f we s h o u l d add a p o i n t e r a t t h i s l e v e l 154
i f ( l <= l e v e l ) 155
{ 156
newNode−>n e x t = i −>n e x t ; 157
i −>n e x t = newNode ; 158
159
// Now move t h e new node p o i n t e r one l e v e l down : 160
newNode = newNode−>below ; 161
} 162
163
// Always move t h e i node p o i n t e r one l e v e l down : 164
i = i −>below ; 165
} 166
167
} 168
// −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−169
// Find 170
// −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−171
bool f i n d ( double x ) 172
{ 173
node<T> ∗ i = r o o t ; 174
175
// S p e c i a l c a s e : s k i p l i s t i s empty 176
i f ( ! root ) 177
{ 178
return f a l s e ; 179
} 180
181
// S p e c i a l c a s e : c h e c k r o o t 182
i f ( r o o t −>data−>v a l u e ( ) == x ) 183
{ 184
return true ; 185
} 186
187
f o r ( i n t l = maxLevel ; l >= 0 ; −−l ) 188
{ 189
at om ic += 1 ; 190
// move f o r w a r d u n t i l we h i t a v a l u e g r e a t e r than o u r s 191
w h i l e ( i −>n e x t != NULL ) 192
{ 193
at om i c += 1 ; 194
if ( i −>n e x t −>data−>v a l u e ( ) > x ) // x i s not found on t h i s l e v e l 195
{ 196
break ; 197
} 198
else if ( i −>next−>data−>v a l u e ( ) == x ) // b i n g o ! 199
{ 200
return true ; 201
} 202
Page 11 of 22
Dave Coleman CSCI 5454 (Prof. Clauset 4:00 MW): Visibility Graph
203
i = i −>n e x t ; 204
} 205
206
// Always move t h e i node<T> p o i n t e r one l e v e l down : 207
i = i −>below ; 208
} 209
210
return f a l s e ; 211
} 212
// −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−213
// REMOVE 214
// t h e i d i s t o c o n f i r m t h e c o r r e c t node , j u s t i n c a s e x i s not u n i q u e 215
// −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−216
b o o l remove ( d o u b l e x , i n t i d ) 217
{ 218
node<T> ∗ i = r o o t ; 219
220
// S p e c i a l c a s e : remove r o o t −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− 221
i f ( r o o t −>data−>v a l u e ( ) == x && r o o t −>data−>i d == i d ) 222
{ 223
// Get l e v e l 0 o f r o o t 224
f o r ( i n t l = r o o t −>l e v e l ; l > 0 ; −−l ) 225
{ 226
at om i c += 1 ; 227
// c o u t << ” L e v e l ” << l << e n d l ; 228
i = i −>below ; 229
} 230
231
// Check i f t h e r e a r e any more nodes 232
i f ( ! i −>n e x t ) // t h e s k i p l i s t i s empty 233
{ 234
r o o t = NULL; 235
maxLevel = 0 ; 236
237
return true ; 238
} 239
240
// Change v a l u e o f r o o t t o n e x t node 241
node<T> ∗n = r o o t ; 242
node<T> ∗ nextNode = i −>n e x t ; 243
244
f o r ( i n t l = maxLevel ; l >= 0 ; −−l ) 245
{ 246
at om i c += 1 ; 247
// change t h e r o o t t o t h e new v a l u e 248
n−>data = nextNode−>data ; 249
250
// update n e x t p o i n t e r i f t h e n e x t n e x t e x i s t s 251
i f ( n−>n e x t ) 252
{ 253
n−>n e x t = n−>next−>n e x t ; 254
} 255
256
// Move down t o n e x t l e v e l 257
n = n−>below ; 258
} 259
260
return true ; 261
} 262
263
// Normal c a s e : remove a f t e r r o o t −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− 264
b o o l found = f a l s e ; 265
266
f o r ( i n t l = maxLevel ; l >= 0 ; −−l ) 267
{ 268
at om ic += 1 ; 269
// move f o r w a r d u n t i l we h i t a v a l u e g r e a t e r than o u r s 270
Page 12 of 22
Dave Coleman CSCI 5454 (Prof. Clauset 4:00 MW): Visibility Graph
Page 13 of 22
Dave Coleman CSCI 5454 (Prof. Clauset 4:00 MW): Visibility Graph
return r e s u l t ; 339
} 340
341
// Change v a l u e o f r o o t t o n e x t node 342
node<T> ∗n = r o o t ; 343
node<T> ∗ nextNode = i −>n e x t ; 344
345
f o r ( i n t l = maxLevel ; l >= 0 ; −−l ) 346
{ 347
at om ic += 1 ; 348
// change t h e r o o t t o t h e new v a l u e 349
n−>data = nextNode−>data ; 350
351
// update n e x t p o i n t e r i f t h e n e x t n e x t e x i s t s 352
i f ( n−>n e x t ) 353
{ 354
n−>n e x t = n−>next−>n e x t ; 355
} 356
357
// Move down t o n e x t l e v e l 358
n = n−>below ; 359
360
} 361
362
return r e s u l t ; 363
} 364
// −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− 365
// I s Root 366
// −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−367
bool isRoot ( int id ) 368
{ 369
370
i f ( ! r o o t ) // t h e r e i s no r o o t ! 371
{ 372
s t d : : c o u t << ” t h e r e i s no r o o t ! ” << s t d : : e n d l ; 373
return f a l s e ; 374
} 375
r e t u r n ( r o o t −>data−>i d == i d ) ; 376
} 377
// −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−378
// PRINT ALL 379
// −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−380
void p r i n t A l l ( ) 381
{ 382
s t d : : c o u t << s t d : : e n d l << ”LIST −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−” << s t d : : e n d l ; 383
384
// S p e c i a l c a s e : s k i p l i s t i s empty 385
i f ( ! root ) 386
{ 387
s t d : : c o u t << ”−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−” << s t d : : e n d l ; 388
return ; 389
} 390
391
node<T> i = ∗ r o o t ; 392
393
// Get l e v e l 0 o f r o o t 394
f o r ( i n t l = r o o t −>l e v e l ; l > 0 ; −−l ) 395
{ 396
// c o u t << ” L e v e l ” << l << ” − ” ; 397
// i . data . p r i n t ( ) ; 398
// c o u t << e n d l ; 399
i = ∗ ( i . below ) ; 400
} 401
// s t d : : c o u t << ”we a r e on l e v e l ” << i . l e v e l << s t d : : e n d l ; 402
403
// Hack : update r o o t 0 l e v e l with maxLevel count , b e c a u s e we don ’ t update t h i s 404
// when g r o w i n g r o o t l e v e l s i z e 405
i . h e i g h t = maxLevel ; 406
Page 14 of 22
Dave Coleman CSCI 5454 (Prof. Clauset 4:00 MW): Visibility Graph
407
int counter = 0; 408
b o o l done = f a l s e ; 409
410
w h i l e ( ! done ) 411
{ 412
s t d : : c o u t << c o u n t e r ; 413
414
f o r ( i n t l = i . h e i g h t ; l >= 0 ; −−l ) 415
{ 416
s t d : : c o u t << ” | ” ; 417
} 418
s t d : : c o u t << ” ” << i . data−>v a l u e ( ) << ” − ” ; 419
i . data−>p r i n t ( ) ; 420
421
c o u n t e r ++; 422
423
i f ( i . next ) 424
{ 425
node<T> ∗ i i = i . n e x t ; 426
i = ∗ii ; 427
} 428
else 429
{ 430
done = t r u e ; 431
} 432
} 433
434
s t d : : c o u t << ”−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−” << s t d : : e n d l << s t d : : e n d l ; 435
} 436
}; 437
../visibility graph/skiplist.h
Page 15 of 22
Dave Coleman CSCI 5454 (Prof. Clauset 4:00 MW): Visibility Graph
node.h
#i n c l u d e ” l i n e . h” 1
2
// 3
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
// Node C l a s s 4
// 5
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
t e m p l a t e < c l a s s T> 6
c l a s s node { 7
public : 8
node ∗ below ; // node below i n tower 9
node ∗ n e x t ; // n e x t node i n s k i p l i s t 10
i n t l e v e l ; // l e v e l o f t h i s c u r r e n t node 11
i n t h e i g h t ; // f u l l number o f l e v e l s i n tower 12
T data ; 13
}; 14
../visibility graph/node.h
Page 16 of 22
Dave Coleman CSCI 5454 (Prof. Clauset 4:00 MW): Visibility Graph
line.h
#i f n d e f LINE H INCLUDED 1
#d e f i n e LINE H INCLUDED 2
3
#i n c l u d e <i o s t r e a m > 4
#i n c l u d e ” p o i n t . h” 5
#i n c l u d e ” geometry . h” 6
#i n c l u d e <cmath> 7
8
c l a s s L i n e : p u b l i c Geometry 9
{ 10
public : 11
Point ∗ a ; 12
Point ∗ b ; 13
b o o l v i s i t e d S t a r t P o i n t ; // has t h e b a s e / sweep l i n e c r o s s e d a t l e a s t one o f 14
// t h e v e r t i c i e s ? 15
bool v i s i t e d ; // has t h e sweep l i n e been on t h e l i n e ( a s in , maybe i t was i n i t on i t ) 16
17
int id ; 18
d o u b l e d i s t ; // d i s t a n c e from c e n t e r 19
d o u b l e t h e t a c a c h e ; // used f o r d e c i d i n g i f t h e d i s t c a c h e n e e d s t o be r e f r e s h e d 20
d o u b l e m; // s l o p e o f l i n e 21
d o u b l e y i n t e r c e p t ; // y−i n t e r c e p t o f l i n e 22
23
Line ( ) ; 24
L i n e ( i n t x1 , i n t y1 , i n t x2 , i n t y 2 ) ; 25
˜ Line ( ) ; 26
v i r t u a l void print ( ) ; 27
v i r t u a l double value ( ) ; 28
29
void updateCalcs ( ) ; 30
void distance ( ) ; 31
v o i d c e n t e r i n t e r c e p t ( d o u b l e &x i , d o u b l e &y i ) ; 32
}; 33
34
35
// This g l o b a l n e e d s t o be v i s i b l e t o c l a s s e s : 36
extern Point ∗ c e n t e r ; 37
extern Line ∗ c e n t e r l i n e ; 38
e x t e r n d o u b l e at om ic ; 39
40
#e n d i f 41
../visibility graph/line.h
line.cpp
#i n c l u d e ” l i n e . h” 1
2
Point ∗ c e n t e r ; 3
Line ∗ c e n t e r l i n e ; 4
d o u b l e at om ic ; 5
6
u s i n g namespace s t d ; 7
8
Line : : Line ( ) 9
{ 10
c o u t << ”You a r e c a l l i n g t h e f u n c t i o n wrong ” ; 11
exit (0) ; 12
} 13
L i n e : : L i n e ( i n t x1 , i n t y1 , i n t x2 , i n t y2 ) 14
{ 15
// Order a and b such t h a t a . x > b . x 16
i f ( x1 > x2 ) 17
{ 18
a = new P o i n t ( x1 , y1 ) ; 19
b = new P o i n t ( x2 , y2 ) ; 20
} 21
Page 17 of 22
Dave Coleman CSCI 5454 (Prof. Clauset 4:00 MW): Visibility Graph
else 22
{ 23
b = new P o i n t ( x1 , y1 ) ; 24
a = new P o i n t ( x2 , y2 ) ; 25
} 26
27
// Change ID 28
static int id counter = 0; 29
i d = i d c o u n t e r ++; 30
31
// Keep t r a c k o f i t s v i s i t e d h i s t o r y 32
visited = false ; 33
visitedStartPoint = false ; 34
35
// c o u t << ”LINE” << e n d l ; 36
updateCalcs ( ) ; 37
38
// c o u t << ”LINE” << e n d l ; 39
40
// Used f o r c h e c k i n g i f we need t o r e f r e s h our d i s t a n c e amount 41
t h e t a c a c h e = 3∗M PI ; // some a n g l e b i g g e r than 2PI , aka INF 42
// d i s t a n c e ( ) ; 43
44
// c o u t << ”END LINE \n” << e n d l ; 45
} 46
Line : : ˜ Line ( ) 47
{ 48
// d e l e t e a ; 49
// d e l e t e b ; 50
} 51
void Line : : p r i n t ( ) 52
{ 53
c o u t << ” L i n e : x1 : ” << a−>x << ” y1 : ” << a−>y << ” x2 : ” << b−>x 54
<< ” y2 : ” << b−>y << ” \ t ID : ” << i d << e n d l ; 55
} 56
double Line : : value ( ) 57
{ 58
// c a l c u l a t e d i s t a n c e from m i d p o i n t a t a g i v e n t h e t a , 59
// with r e s e p c t t o t h e b a s e l i n e 60
61
i f ( t h e t a c a c h e != c e n t e r −>t h e t a ) // c h e c k i f our ca ch ed v e r s i o n i s s t i l l f r e s h enough62
{ 63
// c o u t << ” R e c a l c u l a i n g d i s t a n c e f o r l i n e ” << i d << e n d l ; 64
distance () ; 65
} 66
67
return dist ; 68
} 69
void Line : : updateCalcs ( ) 70
{ 71
// Find S l o p e and y−i n t e r c e p t o f t h i s l i n e f o r f u t u r e d i s t a n c e c a l c u l a t i o n s 72
d o u b l e denom = ( b−>x − a−>x ) ; 73
i f ( denom == 0 ) 74
{ 75
// c o u t << ” This program d o e s not s u p p o r t p e r f e c t l y v e r t i c l e l i n e s . ” << e n d l ; 76
// e x i t ( 0 ) ; 77
78
// P e r t u r b : 79
// b−>x = b−>x + 1 ; 80
denom = 0 . 0 0 0 0 0 0 0 0 1 ; // ( b−>x − a−>x ) ; 81
} 82
m = ( b−>y − a−>y ) /denom ; 83
84
// c o u t << m << ” M ” << e n d l ; 85
86
y i n t e r c e p t = a−>y − m∗a−>x ; 87
// c o u t << y i n t e r c e p t << ” m ” << e n d l ; 88
} 89
Page 18 of 22
Dave Coleman CSCI 5454 (Prof. Clauset 4:00 MW): Visibility Graph
void Line : : d i s t a n c e ( ) 90
{ 91
// F i r s t f i n d t h e i n t e s e c t i o n o f t h i s l i n e and t h e sweep l i n e : 92
double xi ; 93
double yi ; 94
c e n t e r i n t e r c e p t ( xi , y i ) ; 95
96
// c o u t << ”The i n t e r c e p t i s x : ” << x i << ” y : ” << y i << e n d l ; 97
// c o u t << ”M: ” << m << ” b : ” << y i n t e r c e p t << e n d l ; 98
99
// Now f i n d t h e d i s t a n c e between t h e s e two l i n e s : 100
d i s t = s q r t ( pow ( c e n t e r −>x − x i , 2 . 0 ) + pow ( c e n t e r −>y − y i , 2 . 0 ) ) ; 101
102
// c o u t << ” D i s t a n c e : ” << d i s t << e n d l << e n d l ; 103
t h e t a c a c h e = c e n t e r −>t h e t a ; 104
} 105
106
v o i d L i n e : : c e n t e r i n t e r c e p t ( d o u b l e &x i , d o u b l e &y i ) 107
{ 108
x i = d o u b l e ( y i n t e r c e p t − c e n t e r l i n e −>y i n t e r c e p t ) / d o u b l e ( c e n t e r l i n e −>m − m ) ; 109
y i = m∗ x i + y i n t e r c e p t ; 110
} 111
../visibility graph/line.cpp
Page 19 of 22
Dave Coleman CSCI 5454 (Prof. Clauset 4:00 MW): Visibility Graph
point.h
#i f n d e f POINT H INCLUDED 1
#d e f i n e POINT H INCLUDED 2
3
#i n c l u d e <i o s t r e a m > 4
#i n c l u d e ” geometry . h” 5
6
c l a s s P o i n t : p u b l i c Geometry 7
{ 8
public : 9
int x ; 10
int y ; 11
void ∗ parentLine ; 12
i n t i d ; // f o r removing , comparing , e t c 13
d o u b l e t h e t a ; // a n g l u l a r amount from b a s e l i n e 14
15
Point ( ) ; 16
Point ( i n t x1 , i n t y1 ) ; 17
18
v i r t u a l void print ( ) ; 19
v i r t u a l double value ( ) ; 20
}; 21
22
#e n d i f 23
../visibility graph/point.h
point.cpp
#i n c l u d e ” p o i n t . h” 1
2
3
Point : : Point ( ) 4
{ 5
static int id counter = 0; 6
i d = i d c o u n t e r ++; 7
} 8
P o i n t : : P o i n t ( i n t x1 , i n t y 1 ) 9
{ 10
x = x1 ; 11
y = y1 ; 12
Point ( ) ; 13
} 14
void Point : : p r i n t ( ) 15
{ 16
s t d : : c o u t << ” P o i n t x : ” << x << ” y : ” << y << ” \ t ID : ” << i d << s t d : : e n d l ; 17
} 18
double Point : : value ( ) 19
{ 20
// t h i s i s t h e a n g u l a r d i s t a n c e from t h e b a s e l i n e 21
// f o r p o i n t . cpp , we j u s t c a c h e t h e i n i t i a l c a l c u l a t i o n 22
return theta ; 23
} 24
../visibility graph/point.cpp
Page 20 of 22
Dave Coleman CSCI 5454 (Prof. Clauset 4:00 MW): Visibility Graph
geometry.h
#i f n d e f GEOMETRY H INCLUDED 1
#d e f i n e GEOMETRY H INCLUDED 2
3
c l a s s Geometry 4
{ 5
public : 6
// i n t i d ; // f o r removing , comparing , e t c 7
8
v i r t u a l void print ( ) = 0 ; 9
v i r t u a l double value ( ) = 0 ; 10
11
}; 12
13
#e n d i f 14
../visibility graph/geometry.h
Page 21 of 22
Dave Coleman CSCI 5454 (Prof. Clauset 4:00 MW): Visibility Graph
Page 22 of 22