11// Defining Boundaries of the clipping window
22let xmin = 100 ,
3- xmax = 500 ,
4- ymin = 200 ,
5- ymax = 400 ;
3+ xmax = 500 ,
4+ ymin = 200 ,
5+ ymax = 400 ;
66
77function liangbarskyClipping ( x1 , y1 , x2 , y2 ) {
88
9- if ( x1 > x2 ) {
10- let temp = x2 ;
11- x2 = x1 ;
12- x1 = temp ;
13- temp = y2 ;
14- y2 = y1 ;
15- y1 = temp ;
9+ if ( x1 > x2 ) {
10+ let temp = x2 ;
11+ x2 = x1 ;
12+ x1 = temp ;
13+ temp = y2 ;
14+ y2 = y1 ;
15+ y1 = temp ;
16+ }
17+
18+
19+ console . log ( "Original coords: " +
20+ x1 + " " + y1 + " " + x2 + " " + y2 ) ;
21+
22+ let p = new Array ( ) ,
23+ q = new Array ( ) ,
24+ t = new Array ( ) ;
25+
26+ let dx = x2 - x1 ,
27+ dy = y2 - y1 ,
28+ tmin = 0 ,
29+ tmax = 1 ;
30+
31+ p [ 0 ] = - ( x2 - x1 ) ;
32+ p [ 1 ] = ( x2 - x1 ) ;
33+ p [ 2 ] = - ( y2 - y1 ) ;
34+ p [ 3 ] = ( y2 - y1 ) ;
35+ q [ 0 ] = x1 - xmin ;
36+ q [ 1 ] = xmax - x1 ;
37+ q [ 2 ] = y1 - ymin ;
38+ q [ 3 ] = ymax - y1 ;
39+
40+ for ( let i = 0 ; i < 4 ; i ++ ) {
41+ if ( p [ i ] < 0 ) {
42+ tmin = max ( tmin , q [ i ] / p [ i ] ) ;
43+ } else if ( p [ i ] > 0 ) {
44+ tmax = min ( tmax , q [ i ] / p [ i ] ) ;
45+ } else {
46+ if ( q [ i ] < 0 ) {
47+ tmax = 0 ;
48+ tmin = 1 ;
49+ break ;
50+ }
1651 }
52+ }
53+
54+ //cout<<tmin<<" "<<tmax<<endl;
55+ if ( tmin < tmax ) {
56+ stroke ( 255 , 0 , 0 ) ;
57+ console . log ( "Final Coords: " + x1 + tmin * dx + " " + y1 + tmin * dy + " " + x1 + tmax * dx + " " + y1 + tmax * dy )
58+ line ( x1 + tmin * dx , y1 + tmin * dy , x1 + tmax * dx , y1 + tmax * dy ) ;
59+ } else {
60+ console . log ( "Line is outside the boudary" ) ;
61+ }
62+ }
1763
64+ function setup ( ) {
65+ createCanvas ( 600 , 600 ) ;
1866
19- console . log ( "Original coords: " +
20- x1 + " " + y1 + " " + x2 + " " + y2 ) ;
21-
22- let p1 = - ( x2 - x1 ) ,
23- p2 = - p1 ,
24- p3 = - ( y2 - y1 ) ,
25- p4 = - p3 ;
26-
27- let q1 = x1 - xmin ,
28- q2 = xmax - x1 ,
29- q3 = y1 - ymin ,
30- q4 = ymax - y1 ;
31-
32- let posarray = new Array ( ) ;
33- let negarray = new Array ( ) ;
34-
35- posarray . push ( 1 ) ;
36- negarray . push ( 0 ) ;
37-
38- if ( ( p1 == 0 && q1 < 0 ) || ( p3 == 0 && q3 < 0 ) ) {
39- return ;
40- }
41-
42- if ( p1 != 0 ) {
43- let r1 = q1 / p1 ,
44- r2 = q2 / p2 ;
45- if ( p1 < 0 ) {
46- negarray . push ( r1 ) ;
47- posarray . push ( r2 ) ;
48- } else {
49- negarray . push ( r2 ) ;
50- posarray . push ( r1 ) ;
51- }
52- }
53- if ( p3 != 0 ) {
54- let r3 = q3 / p3 ,
55- r4 = q4 / p4 ;
56- if ( p3 < 0 ) {
57- negarray . push ( r3 ) ;
58- posarray . push ( r4 ) ;
59- } else {
60- negarray . push ( r4 ) ;
61- posarray . push ( r3 ) ;
62- }
63- }
64-
65- let rn1 = max ( negarray ) ,
66- rn2 = min ( posarray ) ;
67-
68- if ( rn1 > rn2 ) {
69- return ;
70- }
71-
72- let xn1 = x1 + p2 * rn1 ,
73- yn1 = y1 + p4 * rn1 ,
74- xn2 = x2 + p2 * rn2 ,
75- yn2 = y2 + p4 * rn2 ;
76-
77-
78- console . log ( "Clipped coords: " +
79- xn1 + " " + yn1 + " " + xn2 + " " + yn2 ) ;
8067
81- stroke ( 0 , 255 , 255 ) ;
82- line ( xn1 , yn1 , xn2 , yn2 ) ;
8368
84- return ;
8569}
8670
87- function setup ( ) {
88- createCanvas ( 600 , 600 ) ;
89- background ( 21 ) ;
90-
91- rectMode ( CENTER ) ;
71+ function draw ( ) {
72+ // put drawing code here
73+ background ( 21 ) ;
74+
75+ rectMode ( CENTER ) ;
76+ stroke ( 255 ) ;
77+ strokeWeight ( 2 ) ;
78+ noFill ( ) ;
79+ rect ( 300 , 300 , 400 , 200 ) ;
80+ strokeWeight ( 0.5 ) ;
81+
82+ for ( let i = 0 ; i < 5 ; i ++ ) {
83+ let x1 = random ( 0 , width ) ,
84+ y1 = random ( 0 , height ) ,
85+ x2 = random ( 0 , width ) ,
86+ y2 = random ( 0 , height ) ;
9287 stroke ( 255 ) ;
93- strokeWeight ( 2 ) ;
94- noFill ( ) ;
95- rect ( 300 , 300 , 400 , 200 ) ;
96-
97- for ( let i = 0 ; i < 1 ; i ++ ) {
98- let x1 = random ( 0 , width ) ,
99- y1 = random ( 0 , height ) ,
100- x2 = random ( 0 , width ) ,
101- y2 = random ( 0 , height ) ;
102- stroke ( 255 ) ;
103- line ( x1 , y1 , x2 , y2 ) ;
104-
105- liangbarskyClipping ( x1 , y1 , x2 , y2 ) ;
106- }
88+ line ( x1 , y1 , x2 , y2 ) ;
89+
90+ liangbarskyClipping ( x1 , y1 , x2 , y2 ) ;
91+ }
92+
93+ frameRate ( 0.5 ) ;
10794
108- }
10995
110- function draw ( ) {
111- // put drawing code here
11296}
0 commit comments