1 Check if the Rectangle is Inside the Circle
A rectangle is inside a circle if all four corners of the rectangle lie inside the circle. Mathematically, a
point (x,y) is inside a circle centered at (Xc, Yc) with radius r if:
(x − Xc )2 + (y − Yc )2 <= r2
1 # include < graphics .h >
2 # include < math .h >
3 # include < stdio .h >
4
5 void c h e c k R e c t a n g l e I n s i d e C i r c l e ( int x , int y , int width , int height , int circleX ,
int circleY , int radius ) {
6 int x1 = x , y1 = y ;
7 int x2 = x + width , y2 = y ;
8 int x3 = x , y3 = y + height ;
9 int x4 = x + width , y4 = y + height ;
10
11 int inside =
12 ( pow ( x1 - circleX , 2) + pow ( y1 - circleY , 2) <= pow ( radius , 2) ) &&
13 ( pow ( x2 - circleX , 2) + pow ( y2 - circleY , 2) <= pow ( radius , 2) ) &&
14 ( pow ( x3 - circleX , 2) + pow ( y3 - circleY , 2) <= pow ( radius , 2) ) &&
15 ( pow ( x4 - circleX , 2) + pow ( y4 - circleY , 2) <= pow ( radius , 2) ) ;
16
17 if ( inside ) {
18 printf ( " The rectangle is inside the circle .\ n " ) ;
19 } else {
20 printf ( " The rectangle is NOT inside the circle .\ n " ) ;
21 }
22 }
23
24 int main () {
25 int gd = DETECT , gm ;
26 initgraph (& gd , & gm , ( char *) " " ) ;
27
28 int circleX = 200 , circleY = 200 , radius = 100;
29 int rectX = 150 , rectY = 150 , width = 50 , height = 50;
30
31 // Draw Circle
32 circle ( circleX , circleY , radius ) ;
33
34 // Draw Rectangle
35 rectangle ( rectX , rectY , rectX + width , rectY + height ) ;
36
37 c h e c k R e c t a n g l e I n s i d e C i r c l e ( rectX , rectY , width , height , circleX , circleY ,
radius ) ;
38
39 getch () ;
40 closegraph () ;
41 return 0;
42 }
Listing 1: Check if the Rectangle is Inside the Circle
2 Compute Region Codes and Clip Using Cohen-Sutherland
Algorithm
Each point is assigned a 4-bit region code based on its position relative to the clipping window.
1 # include < graphics .h >
2 # include < stdio .h >
1
3
4 # define INSIDE 0 // 0000
5 # define LEFT 1 // 0001
6 # define RIGHT 2 // 0010
7 # define BOTTOM 4 // 0100
8 # define TOP 8 // 1000
9
10 int Xmin = 2 , Xmax = 5 , Ymin = 1 , Ymax = 6;
11
12 // Function to compute region codes
13 int computeCode ( int x , int y ) {
14 int code = INSIDE ;
15 if ( x < Xmin ) code |= LEFT ;
16 if ( x > Xmax ) code |= RIGHT ;
17 if ( y < Ymin ) code |= BOTTOM ;
18 if ( y > Ymax ) code |= TOP ;
19 return code ;
20 }
21
22 // Cohen - Sutherland Line Clipping Algorithm
23 void c oh e n Su t h er l a nd C l ip ( int x1 , int y1 , int x2 , int y2 ) {
24 int code1 = computeCode ( x1 , y1 ) ;
25 int code2 = computeCode ( x2 , y2 ) ;
26 int accept = 0;
27
28 while (1) {
29 if (( code1 == 0) && ( code2 == 0) ) {
30 accept = 1; break ;
31 } else if ( code1 & code2 ) {
32 break ;
33 } else {
34 int x , y ;
35 int outcode = code1 ? code1 : code2 ;
36
37 if ( outcode & TOP ) {
38 x = x1 + ( x2 - x1 ) * ( Ymax - y1 ) / ( y2 - y1 ) ;
39 y = Ymax ;
40 } else if ( outcode & BOTTOM ) {
41 x = x1 + ( x2 - x1 ) * ( Ymin - y1 ) / ( y2 - y1 ) ;
42 y = Ymin ;
43 } else if ( outcode & RIGHT ) {
44 y = y1 + ( y2 - y1 ) * ( Xmax - x1 ) / ( x2 - x1 ) ;
45 x = Xmax ;
46 } else if ( outcode & LEFT ) {
47 y = y1 + ( y2 - y1 ) * ( Xmin - x1 ) / ( x2 - x1 ) ;
48 x = Xmin ;
49 }
50
51 if ( outcode == code1 ) {
52 x1 = x ; y1 = y ; code1 = computeCode ( x1 , y1 ) ;
53 } else {
54 x2 = x ; y2 = y ; code2 = computeCode ( x2 , y2 ) ;
55 }
56 }
57 }
58
59 if ( accept ) {
60 setcolor ( GREEN ) ;
61 line ( x1 * 50 , y1 * 50 , x2 * 50 , y2 * 50) ;
62 }
63 }
64
65 int main () {
66 int gd = DETECT , gm ;
2
67 initgraph (& gd , & gm , ( char *) " " ) ;
68
69 int Ax = 6 , Ay = 2 , Bx = 3 , By = 8 , Cx = 1 , Cy = 2 , Dx = 3 , Dy = 4;
70
71 printf ( " Region Codes :\ n " ) ;
72 printf ( " A (% d ,% d ) -> % d \ n " , Ax , Ay , computeCode ( Ax , Ay ) ) ;
73 printf ( " B (% d ,% d ) -> % d \ n " , Bx , By , computeCode ( Bx , By ) ) ;
74 printf ( " C (% d ,% d ) -> % d \ n " , Cx , Cy , computeCode ( Cx , Cy ) ) ;
75 printf ( " D (% d ,% d ) -> % d \ n " , Dx , Dy , computeCode ( Dx , Dy ) ) ;
76
77 setcolor ( RED ) ;
78 line ( Ax * 50 , Ay * 50 , Bx * 50 , By * 50) ;
79 line ( Cx * 50 , Cy * 50 , Dx * 50 , Dy * 50) ;
80
81 rectangle ( Xmin * 50 , Ymin * 50 , Xmax * 50 , Ymax * 50) ;
82
83 c oh e n Su t h er l a nd C l i p ( Ax , Ay , Bx , By ) ;
84 c oh e n Su t h er l a nd C l i p ( Cx , Cy , Dx , Dy ) ;
85
86 getch () ;
87 closegraph () ;
88 return 0;
89 }
Listing 2: Compute Region Codes and Clip Using Cohen-Sutherland Algorithm
3 Thick Line using Bresenham’s Algorithm
1 # include < graphics .h >
2 # include < iostream >
3 using namespace std ;
4
5 void thickBresenham ( int x1 , int y1 , int x2 , int y2 , int thickness ) {
6 int dx = abs ( x2 - x1 ) , dy = abs ( y2 - y1 ) ;
7 int p = 2 * dy - dx ;
8 int x , y , stepY ;
9
10 if ( x1 > x2 ) swap ( x1 , x2 ) , swap ( y1 , y2 ) ;
11 y = y1 ;
12 stepY = ( y2 > y1 ) ? 1 : -1;
13
14 for ( int i = 0; i < thickness ; i ++) {
15 int tempY = y1 - i ;
16 x = x1 ;
17 int tempP = p ;
18
19 for ( int j = 0; j <= dx ; j ++) {
20 putpixel (x , tempY , WHITE ) ;
21 x ++;
22
23 if ( tempP < 0) {
24 tempP += 2 * dy ;
25 } else {
26 tempY += stepY ;
27 tempP += 2 * ( dy - dx ) ;
28 }
29 }
30 }
31 }
32
33 int main () {
34 int gd = DETECT , gm ;
3
35 initgraph (& gd , & gm , " " ) ;
36
37 thickBresenham (100 , 200 , 400 , 300 , 5) ;
38
39 getch () ;
40 closegraph () ;
41 return 0;
42 }
Listing 3: Thick Line using Bresenham’s Algorithm
4 Dashed Line using DDA Algorithm
1 # include < graphics .h >
2 # include < bits / stdc ++. h >
3 using namespace std ;
4
5 void dashedDDA ( int x1 , int y1 , int x2 , int y2 ) {
6 int dx = x2 - x1 , dy = y2 - y1 ;
7 int steps = max ( abs ( dx ) , abs ( dy ) ) ;
8
9 float xInc = dx / ( float ) steps , yInc = dy / ( float ) steps ;
10 float x = x1 , y = y1 ;
11
12 for ( int i = 0; i <= steps ; i ++) {
13 if ( i % 10 < 5) // Dash pattern (5 pixels on , 5 off )
14 putpixel ( round ( x ) , round ( y ) , WHITE ) ;
15
16 x += xInc ;
17 y += yInc ;
18 }
19 }
20
21 int main () {
22 int gd = DETECT , gm ;
23 initgraph (& gd , & gm , " " ) ;
24
25 dashedDDA (100 , 200 , 400 , 300) ;
26
27 getch () ;
28 closegraph () ;
29 return 0;
30 }
Listing 4: Dashed Line using DDA Algorithm
5 Anti Aliasing Using DDA Algorithm
1 # include < graphics .h >
2 # include < cmath >
3 # include < iostream >
4 using namespace std ;
5
6 void antiAliasedLine ( int x1 , int y1 , int x2 , int y2 ) {
7 int dx = x2 - x1 , dy = y2 - y1 ;
8 int steps = max ( abs ( dx ) , abs ( dy ) ) ;
9
10 float xInc = dx / ( float ) steps , yInc = dy / ( float ) steps ;
11 float x = x1 , y = y1 ;
12
4
13 for ( int i = 0; i <= steps ; i ++) {
14 int intensity = 255 - (255 * fabs ( y - round ( y ) ) ) ; // Anti - alias effect
15 putpixel ( round ( x ) , round ( y ) , COLOR ( intensity , intensity , intensity ) ) ;
16
17 x += xInc ;
18 y += yInc ;
19 }
20 }
21
22 int main () {
23 int gd = DETECT , gm ;
24 initgraph (& gd , & gm , " " ) ;
25
26 antiAliasedLine (100 , 200 , 400 , 300) ;
27
28 getch () ;
29 closegraph () ;
30 return 0;
31 }
Listing 5: Anti Aliasing Using DDA Algorithm