@@ -21,13 +21,12 @@ const height = window.innerHeight;
2121const resolution = 20 ;
2222const r = 15 ;
2323
24- const points = new Array ( 4 ) . fill ( 0 ) . map ( ( _ , i ) => {
25- return {
26- x : round ( Math . random ( ) * ( width - r * 2 ) , resolution ) ,
27- y : round ( Math . random ( ) * ( height - r * 2 ) , resolution ) ,
28- index : i ,
29- } ;
30- } ) ;
24+ const points = [
25+ { x : 280 , y : 260 , index : 0 } ,
26+ { x : 580 , y : 120 , index : 1 } ,
27+ { x : 380 , y : 130 , index : 2 } ,
28+ { x : 580 , y : 240 , index : 3 } ,
29+ ] ;
3130
3231const drag = d3Drag < SVGCircleElement , Point > ( ) . on ( 'drag' , dragged ) ;
3332const svg = d3Select ( 'body' )
@@ -113,6 +112,14 @@ function dragged(
113112
114113 d3Select ( this ) . attr ( 'cx' , d . x ) . attr ( 'cy' , d . y ) ;
115114
115+ highlightIntersections ( ) ;
116+ }
117+
118+ function round ( p : number , n : number ) {
119+ return p % n < n / 2 ? p - ( p % n ) : p + n - ( p % n ) ;
120+ }
121+
122+ function highlightIntersections ( ) {
116123 const isect = findIntersection (
117124 points [ 0 ] . x ,
118125 points [ 0 ] . y ,
@@ -134,13 +141,23 @@ function dragged(
134141 intersectionPoints . classed ( 'hidden' , true ) ;
135142 } else if ( isect === 1 ) {
136143 // 1 intersection point
144+ intersectionPoints . classed ( 'hidden' , true ) ;
137145 intersectionPoints . filter ( ( _ , i ) => i === 0 ) . classed ( 'hidden' , false ) ;
138146 } else if ( isect === 2 ) {
139147 // 2 intersection points
140148 intersectionPoints . classed ( 'hidden' , false ) ;
141149 }
150+ document . getElementById ( 'output' ) ! . innerHTML =
151+ isect === 0
152+ ? ''
153+ : isect === 1
154+ ? `${ formatPoint ( intersection [ 0 ] ) } `
155+ : `${ formatPoint ( intersection [ 0 ] ) } ,\n${ formatPoint ( intersection [ 1 ] ) } ` ;
156+ console . log ( 'Intersection: ' , isect ) ;
142157}
143158
144- function round ( p : number , n : number ) {
145- return p % n < n / 2 ? p - ( p % n ) : p + n - ( p % n ) ;
159+ function formatPoint ( [ x , y ] : [ number , number ] ) {
160+ return ` ${ x . toFixed ( 2 ) } , ${ y . toFixed ( 2 ) } ` ;
146161}
162+
163+ highlightIntersections ( ) ;
0 commit comments