Z: Line Clipping

posted Apr 7, 2012, 5:00 AM by Neil Mathew   [ updated Apr 7, 2012, 5:02 AM ]


The Program I've done on this is rather complex and time consuming. 
Not ideal for an exam with one hour to complete. 

So, made this simple one:

PROUD OF THIS CODE REALLY.
Although it is limited to one line (can be changed if needed), it is short and rather direct.
Also, overcame two problems I faced with Line Clipping.

1.
By making the coordinates DOUBLE, accurate clippings seen.

2.
 By putting slope in equation rather than a separate variable, overcame the crashing 
whenever slope matched one of the axis and caused a division by zero error.

3.
Made use of Call by reference, being default for arrays and enforced it for PINSIDE()


CODE


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include<iostream.h>
#include<graphics.h>
#include<conio.h>
 
/* In Line Clipping:
 
INPUT()
Enter 1st pt, 2nd pt. (Keep window fixed.)
 
CHECK()
check each point and MAP 4 3 2 1 CODE
 
AND()
return if inside or outside
 
CLIP() & PINSIDE()
Y=MX+C
 
DRAW()
just the lines and window
 
*/
 
double x1,y1,x2,y2;
double Xmin,Ymin,Xmax,Ymax;
int CODE1[4];
int CODE2[4];
 
void INPUT()
{
cout<<"\n Enter First Point: ";
cin>>x1>>y1;
 
cout<<"\n Enter Second Point: ";
cin>>x2>>y2;
 
Xmin=100;
Ymin=100;
Xmax=300;
Ymax=300;
}
 
void DRAW()
{
 
setcolor(CYAN);
rectangle(Xmin,Ymin,Xmax,Ymax);
 
setcolor(WHITE);
line(x1,y1,x2,y2);
 
}
 
 
void CHECK(int x,int y, int CODE[4])
{                       //arrays call by reference
 
if(y > Ymax)
CODE[3]=1;
else
CODE[3]=0;
 
if(y < Ymin)
CODE[2]=1;
else
CODE[2]=0;
 
if(x > Xmax)
CODE[1]=1;
else
CODE[1]=0;
 
if(x < Xmin)
CODE[0]=1;
else
CODE[0]=0;
}
 
int AND(int code1[4], int code2[4])
{
 
for(int i=0; i<4; i++)
{
if( code1[i] == 1 && code2[i] == 1 )
return 1;
}
 
return 0;
}
 
 
 
void PINSIDE(double &x, double &y)
{
 
 
if( x < Xmin )
{
y=y + (  ((y2-y1)/(x2-x1)) * (Xmin-x) );
x=Xmin;
}
 
if( x > Xmax )
{
y=y + (  ((y2-y1)/(x2-x1)) * (Xmax-x) );
x=Xmax;
}
 
if( y > Ymax )
{
x=x + ( (x2-x1)/(y2-y1) * (Ymax-y) );
y=Ymax;
}
 
 
if( y < Ymin )
{
x=x + ( (x2-x1)/(y2-y1) * (Ymin-y) );
y=Ymin;
}
 
 
}
 
 
 
void CLIP()
{
 
CHECK(x1,y1,CODE1);
CHECK(x2,y2,CODE2);
 
int flag=AND(CODE1,CODE2);
 
if( flag==1 )
{
cout<<" COMPLETELY OUTSIDE ";
x1=0;
x2=0;
y1=0;
y2=0;
 
DRAW();
}
else if( flag==0 )
{
 
if( CODE1[0]==1 || CODE1[1]==1 || CODE1[2]==1 || CODE1[3]==1 )
{
cout<<" PARTIALLY INSIDE ";
PINSIDE(x1,y1);
}
 
if( CODE2[0]==1 || CODE2[1]==1 || CODE2[2]==1 || CODE2[3]==1 )
{
cout<<" PARTIALLY INSIDE ";
PINSIDE(x2,y2);
}
 
if( CODE1[0]==0 && CODE1[1]==0 && CODE1[2]==0 && CODE1[3]==0
    &&  CODE2[0]==0 && CODE2[1]==0 && CODE2[2]==0 && CODE2[3]==0 )
{
cout<<" COMPLETELY INSIDE";
}
 
DRAW();
}
 
}
 
 
 
void main()
{
INPUT();
 
int gdriver=DETECT, gmode;
initgraph(&gdriver,&gmode,"C:/TC/BGI");
 
DRAW();
getch();
 
cleardevice();
CLIP();
 
DRAW();
getch();
 
closegraph();
}
 
 
 





Z: Transformations

posted Apr 7, 2012, 1:27 AM by Neil Mathew   [ updated Apr 7, 2012, 3:23 AM ]


All the programs follow a basic layout, except for the transformation.
And the only change in the transformation is the matrix and it's input variables.

One thing that bothered me is the order since ma'am taught us formulas using Column Major.

However, for shapes with sides greater than the number 3, one tends to get confused with the order.
However, to avoid getting confused, I've used Row Major and changed the order 
but retained the formulas as given in the notebook





ALL ROUND PROGRAM:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
 
#include<math.h> //for rotate
 
/* Every Such Program are divided into these parts (Functions):
 
1. Common Variables = P P2 n T , Tx or Sx or Angle = GLOBAL
 
2. Input the Required Values = INPUT()
 
[ Changing from Translation Scaling, etc EDIT only 1, 2 ]
 
3. Transform = change()
 
4. Draw the Lines Before and After Transformation = Draw()
 
5. Matrix Show  = ShowM() ShowT()
               (IMPORTANT B/C you find your mistakes here.)
                MATRICES. MAKE SURE ALL YOUR CELLS ARE FILLED!!!!
 
 
------Extras:
 
>>ROTATION requires matrices & angle TO BE DOUBLE (floating type)
                others can be int. (exc Reflect which depends on Rotate.
 
>>ROTATION requires conversion of inputted DEGREE to RADIAN
                180 deg = pi rad
                1 deg = pi / 180 * 1 rad
                x deg = pi / 180 * x rad
 
>>ROTATION requires Math.h headerfile
 
>> If Ma'am asks you to do multiple transformations.
        Add the extra bits of INPUT into Change()
        and duplicate Change to its types
*/
 
double P[3][10], P2[3][10], n, T[3][3];
 
int Tx,  Ty; //For Translation
int Sx,  Sy; //For Scaling
double angle;   //For Rotation
double m,b;
int Fx, Fy; //Fixed point
 
 
void INPUT()
{
cout<<"\n Enter no of sides of Shape: ";
cin>>n;
               //i for each point
cout<<"\n Enter the coordinates of Triangle";
for(int i=0; i<n; i++)
{
cout<<"\n"<<i+1<<" (x,y) = ";
cin>>P[0][i];
cin>>P[1][i];
 
P[2][i]=1; //LAST ROW
}
 
/*TRANSLATION:
cout<<"\n To Translate:";
cout<<"\n Enter Tx Ty ";
cin>>Tx>>Ty;
 
T[0][0]=1; T[0][1]=0; T[0][2]=Tx;
T[1][0]=0; T[1][1]=1; T[1][2]=Ty;
T[2][0]=0; T[2][1]=0; T[2][2]=1; */
 
 
 
 
/*SCALING
cout<<"\n To Scale:";
cout<<"\n Enter Sx Sy ";
cin>>Sx>>Sy;
 
T[0][0]=Sx; T[0][1]=0; T[0][2]=0;
T[1][0]=0; T[1][1]=Sy; T[1][2]=0;
T[2][0]=0; T[2][1]=0; T[2][2]=1; */
 
 
 
 
 
 
/*ROTATION
cout<<"\n To Rotate:";
cout<<"\n Enter angle: ";
cin>>angle;
 
//<<<<<<<<<<<<<<CONVERT DEGREE TO RADIAN + call MATH.h
angle = ( ( (22.0/7.0)/180.0 ) * angle );
 
cout<<( cos( angle ) );
 
T[0][0]=cos (angle);    T[0][1]= - sin (angle); T[0][2]=0;
T[1][0]=sin (angle);    T[1][1]= cos (angle);   T[1][2]=0;
T[2][0]=0;              T[2][1]= 0;             T[2][2]=1;
*/
 
 
 
//REFLECTION:
cout<<"\n To Reflect:";
cout<<"\n Enter wrt eq (Y= m X + b) value of m & b: ";
cin>>m>>b;
 
cout<<( cos( angle ) );
 
T[0][0]=(1-m*m)/(1+m*m); T[0][1]= 2*m/ (1+m*m);         T[0][2]= - (2*m*b)/(1+m*m);
T[1][0]=2*m/(1+m*m);     T[1][1]= (m*m-1)/(1+m*m);      T[1][2]= (2*b)/(1+m*m);
T[2][0]=0;               T[2][1]= 0;                    T[2][2]= 1;
 
 
/*^^^^^^^^^^^^^^^^^^^^^^^^^ WRT POINT
 
cout<<"\n Enter the fixed points: (x,y)";
cin>>Fx>>Fy; */
 
 
 
 
/*SCALING wrt POINT
cout<<"\n To Scale:";
cout<<"\n Enter Sx Sy ";
cin>>Sx>>Sy;
 
T[0][0]=Sx; T[0][1]=0; T[0][2]=-Fx*Sx + Fx;
T[1][0]=0; T[1][1]=Sy; T[1][2]=-Fy*Sy + Fy;
T[2][0]=0; T[2][1]=0; T[2][2]=1; */
 
 
 
 
 
 
 
/*ROTATION wrt POINT
cout<<"\n To Rotate:";
cout<<"\n Enter angle: ";
cin>>angle;
 
//<<<<<<<<<<<<<<CONVERT DEGREE TO RADIAN + call MATH.h
angle = ( ( (22.0/7.0)/180.0 ) * angle );
 
cout<<( cos( angle ) );
 
T[0][0]=cos (angle);    T[0][1]= - sin (angle); T[0][2]=-(Fx*T[0][0]+Fy*T[0][1])+Fx;
T[1][0]=sin (angle);    T[1][1]= cos (angle);   T[1][2]=-(Fx*T[1][0]+Fy*T[1][1])+Fy;
T[2][0]=0;              T[2][1]= 0;             T[2][2]=1;
*/
 
}
 
void Change()
{
            /*here: i for Rows (x,y,1)
                    j for Columns (each point)
                    k for multiplication of matrices (common) */
 
for(int i=0; i<3; i++)
{
for(int j=0; j<n; j++)
{
P2[i][j]=0;
 
for(int k=0; k<3; k++)
{
P2[i][j] += T[i][k] * P[k][j];
}
 
}
}
 
}
 
void ShowM(double A[3][10])
{
 
for(int i=0; i<3; i++)
{
for(int j=0; j<n; j++)
{
cout<<" "<<A[i][j];
}
cout<<"\n";
}
}
 
void ShowT()
{
 
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
cout<<" "<<T[i][j];
}
cout<<"\n";
}
}
 
void Draw( double A[3][10] )
{
 
//MID POINT
int X= getmaxx()*0.5;
int Y= getmaxy()*0.5;
 
        //i for each point
for(int i=0; i< ( n - 1 ); i++)
{
line(X + A[0][i],Y + A[1][i], X + A[0][i+1], Y+ A[1][i+1]);
}
line(X + A[0][i], Y + A[1][i], X + A[0][0],Y + A[1][0]);
 
 
//below so axis is not green, or blue
setcolor(DARKGRAY);
//DRAW AXIS:
line(0,                 getmaxy()*0.5,
     getmaxx(),         getmaxy()*0.5);
 
line(getmaxx()*0.5,     0,
     getmaxx()*0.5,     getmaxy());
}
 
 
 
void main()
{
int gdriver=DETECT,gmode;
 
//INPUT & INITIALIZATION:::::::::::::::::::::::::::::::::::
INPUT();
 
//THE TRANSFORM::::::::::::::::::::::::::::::::::::::::::::::
Change();
 
//SHOW MATRICES::::::::::::::::::::::::::::::::::::::::::::::
 
cout<<"\n Show Matrices:";
 
cout<<"\n\nORIGINAL:\n";
ShowM(P);
 
cout<<"\nTRANSFORMATION MAT:\n";
ShowT();
 
cout<<"\n\nTRANSFORMED:\n";
ShowM(P2);
 
cout<<"\n Draw? ";
getch();
 
//DRAW::::::::::::::::::::::::::::::::::::::::::::::::::::::::
initgraph(&gdriver, &gmode, "C:/TC/BGI");
cleardevice();
 
//Before
setcolor(GREEN);
Draw(P);
 
//After
setcolor(CYAN);
Draw(P2);
 
getch();
closegraph();
}
 
 
 
 

Q13: WAP to perform reflection wrt to a line

posted Mar 12, 2012, 11:51 AM by Neil Mathew


SOURCE CODE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<stdio.h>
 
 
 
int n;
double P[3][10];
double P2[3][10];
 
double S[3][3];
 
float m; int b;
 
 
 
void INPUT()
{
int ch;
cout<<"\n For Polygon where No of edges (>=3) = ";
cin>>n;
 
cout<<"\n Enter the coordinates of "<<n<<" sided Polygon: \n";
 
for(int i=0; i<n; i++)
{
cout<<" "<<i+1<<" Point (x,y) : ";
cin>>P[0][i]>>P[1][i];
P[2][i]=1;
}
 
cout<<"\n Enter the equation of line ( Y = m X + b) : ";
char ch1;
 
cin>>ch1;
cin>>ch1;
cin>>m;
cin>>ch1;
cin>>ch1;
cin>>b;
 
//cout<<"\n Slope = "<<m<<"\n Y Intercept = "<<b;
}
 
 
 
 
void ReflectPoint()
{
 
// S[3][3] : Reflect Transform MATRIX
// P[3][n] : 3 x n MATRIX
// P2[3][n] : Final MATRIX
 
//Reflect Transform Matrix:
//1st - making the identity Matrix
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
        if(i==j)     //DIAGONAL
        S[i][j] = 1;
        else
        S[i][j] = 0;
}
}
//2nd - making changes
S[0][0] = (1-m*m) / (m*m+1) ;
S[0][1] = (2*m) / (m*m+1) ;
S[0][2] = (-2*m*b) / (m*m+1) ;
S[1][0] = (2*m) / (m*m+1) ;
S[1][1] = (m*m-1) / (m*m+1) ;
S[1][2] =  (2*b) / (m*m+1);
 
 
// Matrix Multiplication
// S(3x3)  x  P(3xN)
 
for(i=0; i<3; i++)
{
for(int j=0; j<n; j++)
{
        P2[i][j]=0;
for(int k=0; k<3; k++)
{
P2[i][j]+=S[i][k]*P[k][j];
}
}
}
 
}
 
void Display_Matrix(double p[3][10])
{
 
for(int i=0; i<3; i++)
{
cout<<"\n |";
for(int j=0; j<n; j++)
cout<<"  "<<p[i][j];
cout<<"  |";
}
 
}
 
void DRAW(double p[3][10])
{
 
for(int i=0; i<n-1; i++)
{
line(getmaxx()*0.5+p[0][i], getmaxy()*0.5+p[1][i], 
               getmaxx()*0.5+p[0][i+1], getmaxy()*0.5+p[1][i+1]);
}
line(getmaxx()*0.5+p[0][n-1], getmaxy()*0.5+p[1][n-1], 
                    getmaxx()*0.5+p[0][0], getmaxy()*0.5+p[1][0]);
 
}
 
 
void main()
{
INPUT();
Display_Matrix(P);
cout<<endl;
 
ReflectPoint();
 
for(int i=0; i<3; i++)
{
cout<<"\n |";
for(int j=0; j<3; j++)
cout<<" "<<S[i][j];
cout<<" |";
}
cout<<endl;
 
Display_Matrix(P2);
 
cout<<"\n\n Press Enter to see Graphically? ";
getch();
 
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"C:/TC/BGI");
 
 
setcolor(DARKGRAY);
//X AXIS
line(getmaxx()*0.5, 0, getmaxx()*0.5, getmaxy());
//Y AXIS
line(0, getmaxy()*0.5, getmaxx(), getmaxy()*0.5);
 
 
setcolor(CYAN);
DRAW(P);
setcolor(GREEN);
DRAW(P2);
 
getch();
closegraph();
 
}


OUTPUT:



Q12: WAP to perform rotation wrt to a point.

posted Mar 4, 2012, 8:23 AM by Neil Mathew   [ updated Mar 6, 2012, 7:36 AM ]



SOURCE CODE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
 
int n;
double P[3][10];
double P2[3][10];
 
double S[3][3];
double angle;
int h,k;
 
double deg2rad(double deg)
{
   double rad= ((22.0/7.0)/180.0)*deg;
   return rad;
 
}
 
 
void INPUT()
{
int ch;
cout<<"\n For Polygon where No of edges (>3) = ";
cin>>n;
 
cout<<"\n Enter the coordinates of "<<n<<" sided Polygon: \n";
 
for(int i=0; i<n; i++)
{
cout<<" "<<i+1<<" Point (x,y) : ";
cin>>P[0][i]>>P[1][i];
P[2][i]=1;
}
 
cout<<"\n Enter the Angle: "; cin>>angle;
 
cout<<"\n Scaling W.R.T which point (1/2/3...) : ";
cin>>ch;
 
if(ch!=0)
{
h=P[0][ch-1];
k=P[1][ch-1];
}
}
 
 
void ROTATEpoint()
{
                   double x=angle;
// S[3][3] : Scaling MATRIX
// P[3][n] : 3 x n MATRIX
// P2[3][n] : Final MATRIX
 
//ROTATE Matrix:
//1st - making the identity Matrix
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
        if(i==j)     //DIAGONAL
        S[i][j] = 1;
        else
        S[i][j] = 0;
}
}
//2nd - making changes
S[0][0] = cos (  deg2rad(x) );
S[0][1] = - sin( deg2rad(x) );
S[1][0] = sin ( deg2rad(x) );
S[1][1] = cos ( deg2rad(x) );
 
S[0][2] = -h*cos(deg2rad(x)) + k*sin ( deg2rad(x) ) + h;
S[1][2] = -h*sin(deg2rad(x)) - k*cos ( deg2rad(x) ) + k;
 
 
 
// Matrix Multiplication
// S(3x3)  x  P(3xN)
 
for(i=0; i<3; i++)
{
for(int j=0; j<n; j++)
{
        P2[i][j]=0;
for(int k=0; k<3; k++)
{
P2[i][j]+=S[i][k]*P[k][j];
}
}
}
 
}
 
void Display_Matrix(double p[3][10])
{
 
for(int i=0; i<3; i++)
{
cout<<"\n |";
for(int j=0; j<n; j++)
cout<<"  "<<p[i][j];
cout<<"  |";
}
 
}
 
void DRAW(double p[3][10])
{
 
for(int i=0; i<n-1; i++)
{
line(getmaxx()*0.5+p[0][i], getmaxy()*0.5+p[1][i], 
               getmaxx()*0.5+p[0][i+1], getmaxy()*0.5+p[1][i+1]);
} line(getmaxx()*0.5+p[0][n-1], getmaxy()*0.5+p[1][n-1],                     getmaxx()*0.5+p[0][0], getmaxy()*0.5+p[1][0]);   }     void main() { INPUT(); Display_Matrix(P); cout<<endl;   ROTATEpoint();   for(int i=0; i<3; i++) { cout<<"\n |"; for(int j=0; j<3; j++) cout<<" "<<S[i][j]; cout<<" |"; } cout<<endl;   Display_Matrix(P2);   cout<<"\n\n Press Enter to see Graphically? "; getch();   int gdriver=DETECT,gmode; initgraph(&gdriver,&gmode,"C:/TC/BGI");     setcolor(DARKGRAY); //X AXIS line(getmaxx()*0.5, 0, getmaxx()*0.5, getmaxy()); //Y AXIS line(0, getmaxy()*0.5, getmaxx(), getmaxy()*0.5);     setcolor(CYAN); DRAW(P); setcolor(GREEN); DRAW(P2);   getch(); closegraph();   }



OUTPUT:




(NOT w.r.t. a point)


The images below are inaccurate. My Degree to Radian formula was faulty when I took these. The above program should yield more proper angles where the rotation takes place ANTI CLOCKWISE unlike the clockwise ones below.






Q11: WAP to perform Scaling wrt a Point.

posted Feb 26, 2012, 7:58 AM by Neil Mathew   [ updated Feb 26, 2012, 8:05 AM ]


SOURCE CODE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
 
int n;
int P[3][10];
int P2[3][10];
 
int S[3][3];
int Sx,Sy;
int h,k;
 
void INPUT()
{
int ch;
cout<<"\n For Polygon where No of edges (>3) = ";
cin>>n;
 
cout<<"\n Enter the coordinates of "<<n<<" sided Polygon: \n";
 
for(int i=0; i<n; i++)
{
cout<<" "<<i+1<<" Point (x,y) : ";
cin>>P[0][i]>>P[1][i];
P[2][i]=1;
}
 
cout<<"\n Enter the Scaling Factors:";
cout<<"\n for X axis: "; cin>>Sx;
cout<<" for Y axis: "; cin>>Sy;
 
cout<<"\n Scaling W.R.T which point (1/2/3...) : ";
cin>>ch;
 
h=P[0][ch-1];
k=P[1][ch-1];
}
 
 
void ScalingPoint()
{
 
// S[3][3] : Scaling MATRIX
// P[3][n] : 3 x n MATRIX
// P2[3][n] : Final MATRIX
 
//Scaling Matrix:
//1st - making the identity Matrix
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
        if(i==j)     //DIAGONAL
        S[i][j] = 1;
        else
        S[i][j] = 0;
}
}
//2nd - making changes
S[0][0] = Sx;
S[1][1] = Sy;
S[0][2] = -(h*Sx)+h;
S[1][2] = -(k*Sy)+k;
 
 
 
// Matrix Multiplication
// S(3x3)  x  P(3xN)
 
for(i=0; i<3; i++)
{
for(int j=0; j<n; j++)
{
        P2[i][j]=0;
for(int k=0; k<3; k++)
{
P2[i][j]+=S[i][k]*P[k][j];
}
}
}
 
}
 
void Display_Matrix(int p[3][10])
{
 
for(int i=0; i<3; i++)
{
cout<<"\n |";
for(int j=0; j<n; j++)
cout<<"  "<<p[i][j];
cout<<"  |";
}
 
}
 
void DRAW(int p[3][10])
{
for(int i=0; i<n-1; i++)
{
line(p[0][i], p[1][i], p[0][i+1], p[1][i+1]);
}
line(p[0][n-1], p[1][n-1], p[0][0], p[1][0]);
 
}
 
 
void main()
{
INPUT();
Display_Matrix(P);
cout<<endl;
 
ScalingPoint();
for(int i=0; i<3; i++)
{
cout<<"\n |";
for(int j=0; j<3; j++)
cout<<" "<<S[i][j];
cout<<" |";
}
cout<<endl;
 
Display_Matrix(P2);
 
cout<<"\n\n Press Enter to see Graphically? ";
getch();
 
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"C:/TC/BGI");
 
setcolor(CYAN);
DRAW(P);
setcolor(GREEN);
DRAW(P2);
 
getch();
closegraph();
 
}


OUTPUT:



P8: WAP... MY ALL IN ONE CLIPPING.

posted Feb 23, 2012, 8:21 AM by Neil Mathew

Note:
  • Using the floodfill() graphics function, I realized, just like the bucket tool in Paint, I could wipe out everything that was outside the clipping window. 

  • The floodfill() works by replacing everything with the setfillstyle color until it comes across the 'boundary color' which I've used as the colour of my clipping window.

  • Drawbacks:
    The background, shapes to be clipped and the clipping window must be of different colours.


SOURCE CODE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include<graphics.h>
#include<iostream.h>
#include<conio.h>
 
 
int n=11; //no of points;
 
int P[]={
        200, 50,
        150, 150,
        20,  200,
        150, 250,
        100, 400,
        200, 350,
        300, 400,
        250, 250,
        380, 200,
        250, 150,
        200, 50
        };
 
//Clipping Window
int Xmin=100;
int Ymin=100;
int Xmax=300;
int Ymax=300;
 
void ShowUP()
{
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"C:/TC/BGI");
 
//DEFAULT COLOR SETTINGS:
setbkcolor(BLACK);
setfillstyle(SOLID_FILL,WHITE);
setcolor(WHITE);
 
//DRAW UNCLIPPED:
fillpoly(11,P);
setcolor(CYAN);
rectangle(Xmin,Ymin,Xmax,Ymax);
 
cout<<"\n Press Enter to start Clipping... ";
getch();
 
//FILL EVERYTHIHNG BUT CYAN BORDERS BLACK
setfillstyle(SOLID_FILL,BLACK);
floodfill(0,0,CYAN);
 
cout<<"\n Press Enter to Exit... ";
getch();
closegraph();
}
 
void main()
{
ShowUP();
}
 
 


OUTPUT:





P9: WAP to... (Redone, Better Way)

posted Feb 23, 2012, 7:59 AM by Neil Mathew   [ updated Feb 23, 2012, 8:03 AM ]

Note: This Scaling method can be used for all polygons.


SOURCE CODE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
 
int n;
int P[3][10];
int P2[3][10];
 
int S[3][3];
int Sx,Sy;
 
void INPUT()
{
 
cout<<"\n For Polygon where No of edges (>3) = ";
cin>>n;
 
cout<<"\n Enter the coordinates of "<<n<<" sided Polygon: \n";
 
for(int i=0; i<n; i++)
{
cout<<" "<<i+1<<" Point (x,y) : ";
cin>>P[0][i]>>P[1][i];
P[2][i]=1;
}
 
cout<<"\n Enter the Scaling Factors:";
cout<<"\n for X axis: "; cin>>Sx;
cout<<" for Y axis: "; cin>>Sy;
}
 
 
void Scaling()
{
 
// S[3][3] : Scaling MATRIX
// P[3][n] : 3 x n MATRIX
// P2[3][n] : Final MATRIX
 
//Scaling Matrix:
//1st - making the identity Matrix
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
        if(i==j)     //DIAGONAL
        S[i][j] = 1;
        else
        S[i][j] = 0;
}
}
//2nd - making changes
S[0][0] = Sx;
S[1][1] = Sy;
 
 
// Matrix Multiplication
// S(3x3)  x  P(3xN)
 
for(i=0; i<3; i++)
{
for(int j=0; j<n; j++)
{
        P2[i][j]=0;
for(int k=0; k<3; k++)
{
P2[i][j]+=S[i][k]*P[k][j];
}
}
}
 
}
 
void Display_Matrix(int p[3][10])
{
 
for(int i=0; i<3; i++)
{
cout<<"\n |";
for(int j=0; j<n; j++)
cout<<"  "<<p[i][j];
cout<<"  |";
}
 
}
 
void DRAW(int p[3][10])
{
for(int i=0; i<n-1; i++)
{
line(p[0][i], p[1][i], p[0][i+1], p[1][i+1]);
}
line(p[0][n-1], p[1][n-1], p[0][0], p[1][0]);
 
}
 
 
void main()
{
INPUT();
Display_Matrix(P);
cout<<endl;
 
Scaling();
for(int i=0; i<3; i++)
{
cout<<"\n |";
for(int j=0; j<3; j++)
cout<<" "<<S[i][j];
cout<<" |";
}
cout<<endl;
 
Display_Matrix(P2);
 
cout<<"\n\n Press Enter to see Graphically? ";
getch();
 
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"C:/TC/BGI");
 
setcolor(CYAN);
DRAW(P);
setcolor(GREEN);
DRAW(P2);
 
getch();
closegraph();
 
}


OUTPUT:





Q10: WAP to perform translation

posted Feb 23, 2012, 7:50 AM by Neil Mathew   [ updated Feb 23, 2012, 8:11 AM ]


SOURCE CODE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
 
int n;
int P[3][10];
int P2[3][10];
 
int T[3][3];
int tx,ty;
 
void INPUT()
{
 
cout<<"\n For Polygon where No of edges (>3) = ";
cin>>n;
 
cout<<"\n Enter the coordinates of "<<n<<" sided Polygon: \n";
 
for(int i=0; i<n; i++)
{
cout<<" "<<i+1<<" Point (x,y) : ";
cin>>P[0][i]>>P[1][i];
P[2][i]=1;
}
 
cout<<"\n Enter the Translation Factors:";
cout<<"\n for X axis: "; cin>>tx;
cout<<" for Y axis: "; cin>>ty;
}
 
 
void Translate()
{
 
// T[3][3] : Translation MATRIX
// P[3][n] : 3 x n MATRIX
// P2[3][n] : Final MATRIX
 
//Transaction Matrix:
//1st - making the identity Matrix
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
        if(i==j)     //DIAGONAL
        T[i][j] = 1;
        else
        T[i][j] = 0;
}
}
//2nd - making changes
T[0][2] = tx;
T[1][2] = ty;
 
 
// Matrix Multiplication
// T(3x3)  x  P(3xN)
 
for(i=0; i<3; i++)
{
for(int j=0; j<n; j++)
{
        P2[i][j]=0;
for(int k=0; k<3; k++)
{
P2[i][j]+=T[i][k]*P[k][j];
}
}
}
 
}
 
void Display_Matrix(int p[3][10])
{
 
for(int i=0; i<3; i++)
{
cout<<"\n |";
for(int j=0; j<n; j++)
cout<<"  "<<p[i][j];
cout<<"  |";
}
 
}
 
void DRAW(int p[3][10])
{
for(int i=0; i<n-1; i++)
{
line(p[0][i], p[1][i], p[0][i+1], p[1][i+1]);
}
line(p[0][n-1], p[1][n-1], p[0][0], p[1][0]);
 
}
 
 
void main()
{
INPUT();
Display_Matrix(P);
cout<<endl;
 
Translate();
for(int i=0; i<3; i++)
{
cout<<"\n |";
for(int j=0; j<3; j++)
cout<<" "<<T[i][j];
cout<<" |";
}
cout<<endl;
 
Display_Matrix(P2);
 
cout<<"\n\n Press Enter to see Graphically? ";
getch();
 
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"C:/TC/BGI");
 
setcolor(CYAN);
DRAW(P);
setcolor(GREEN);
DRAW(P2);
 
getch();
closegraph();
 
}


OUTPUT:







P9: WAP to perform scaling of a triangle.

posted Feb 23, 2012, 6:24 AM by Neil Mathew   [ updated Feb 23, 2012, 8:03 AM ]

Note:

  • To make it compatible with the drawpoly function, I converted the 2D matrices holding the coordinates of the points to a 1D matrix which is acceptable by the graphics function. 

  • Next time, use line function. The coding becomes simpler, and easier to understand.

SOURCE CODE:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include<graphics.h>
#include<iostream.h>
#include<conio.h>
 
int n=4;     //Variables for drawpoly() function
int p[8];
int d[8];
 
int Sx,Sy;   //Scaling factors.
 
 
 
void Input()
{
cout<<"\n Enter the scaling factor of x:";
cin>>Sx;
cout<<" Enter the scaling factor of y:";
cin>>Sy;
 
cout<<"\n Using default triangle values \n  
                            | A(0,-20), B(20,-20), C(20, 20) |";
cout<<"\n\n  Click to Continue... ";
getch();
cleardevice();
 
p[0]=0;
p[1]=-30;
p[2]=-30;
p[3]=+30;
p[4]=+30;
p[5]=+30;
p[6]=0;
p[7]=-30;
 
}
 
 
void Draw(int Xmid, int Ymid)
{
 
for(int i=0; i<8; i++)
{
        if( i%2==0 )
        d[i]=Xmid+p[i];
        else
        d[i]=Ymid+p[i];
}
 
drawpoly(n,d);
getch();
}
.
void Scale()
{
int i,j,k;
 
// Matrix with Triangle Coordinates
int xy1[3][3] = { p[0], p[1], 1,
                  p[2], p[3], 1,
                  p[4], p[5], 1,
                };
 
// Matrix with Scaling Factors
int SI[3][3] = { Sx, 0, 0,
                 0, Sy, 0,
                 0,  0, 1
               };
 
// Matrix with Coordinates after Scaling.
int XY1[3][3];
 
//Matrix multiplication: xy1 x SI = XY1
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
        XY1[i][j]=0;
for(k=0; k<3; k++)
{
        XY1[i][j]+=xy1[i][k]*SI[k][j];
} } }
 
// Converting Final Matrix into Single Array
k=0;
for(i=0; i<3; i++)
{
for(j=0; j<2; j++)   // Till 2 because '1' not needed.
{
p[k++]=XY1[i][j];
} 
}
p[k++]=XY1[0][0];
p[k]=XY1[0][1];
}
 
void main()
{
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"C:/TC/BGI");
Input();
 
setcolor(CYAN);
outtextxy(10,getmaxy()*0.25," BEFORE SCALING: ");
Draw(getmaxx()*0.25,getmaxy()*0.5 );
 
Scale();
 
setcolor(GREEN);
outtextxy(getmaxx()*0.5,getmaxy()*0.25," AFTER SCALING: ");
Draw(getmaxx()*0.75, getmaxy()*0.5);
 
closegraph();
}



OUTPUT:





P8: WAP to perform Line CLipping

posted Jan 25, 2012, 3:03 AM by Neil Mathew   [ updated Nov 3, 2012, 8:41 PM ]


What I learnt:
  • This algo fails where there is a vertical Line ( x=c ). I've made provisions for the Slope to be 1, rather than having the program crash due to division by zero. Even by specifying it as 1, the clipping is All Wrong. I get some strange diagonal within the clipping window when I should be getting a straight vertical line. 

    > Realized that making slope = 1 was a bad decision. It seemed rational at the time, but I see now that zero was needed. When the intersection is made on a Y axis based side, according to the algo, the difference between Y and Yaxis is multiplied by inverse of slope, in this case, zero.
    (Don't calculate Slope separately, write the expression as part of the statement. 
    Problem Solved)

  • This program implements Array Deletion, Tertiary operators and a manipulated Palindrome algo. This is there only because I chose to represent the 4-bit indicators as a long datatype rather than an int array. This was only possible because the 4-bit indicator is of the order 4321.


    Each Point indicated by four bit code: [ 4 3 2 1 ]
    (where the 4,3,2,1 will be either 0 or 1)



Correction:
(Later I realize I'm wrong. I've messed with convention.
What is explained as 4 3 2 1 should have been 1 2 3 4 .
That is, 1 if extends over top edge, 2 if it goes over bottom edge, etc)

SOURCE CODE:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include<graphics.h>
#include<iostream.h>
#include<conio.h>
 
int n; //No of Lines
 
int ClipWindow[2][2]; //Clipping Window Coordinates
/* For 2 vertices A and C, each having x and y coordinates*/
 
int Line[20][4];  //Line Coordinates
/* The 20 represents total number of lines possible
 4 represents the 4 coordinates required to draw ONE line */
 
void Draw()
{
clrscr();
 
int gdriver=DETECT,gmode;
initgraph(&gdriver, &gmode, "C:/TC/BGI");
 
setbkcolor(BLACK);
setcolor(WHITE);
for(int i=0; i<n; i++)
line(Line[i][0],Line[i][1],Line[i][2],Line[i][3]);
 
setcolor(CYAN);
rectangle(ClipWindow[0][0],ClipWindow[0][1]
                ,ClipWindow[1][0],ClipWindow[1][1]);
 
outtextxy(10,10, "Press any key to continue");
getch(); closegraph();
}
 
void UnClipped_Input()
{
cout<<"\n Enter the number of lines to draw : ";
cin>>n; cout<<endl;
 
for(int i=0; i<n; i++)
{
cout<<" LINE #"<<(i+1);
cout<<"\n Enter the Initial position (x,y) : ";
cin>>Line[i][0];
cin>>Line[i][1];
cout<<" Enter the Final position (x,y)   : ";
cin>>Line[i][2];
cin>>Line[i][3];
cout<<"\n";
}
cout<<"\n >> Enter the coordinates of the Clipping Window:";
cout<<"\n >> Vertex A (x,y) : ";
cin>>ClipWindow[0][0];
cin>>ClipWindow[0][1];
cout<<" >> Vertex C (x,y) : ";
cin>>ClipWindow[1][0];
cin>>ClipWindow[1][1];
}
long CheckBit(long x,long y)
{
long b=5; /* The 5 here is any random non zero number. 
This ensures 0000 doesn't become 0 */
int Xmax= 
ClipWindow[0][0] > ClipWindow[1][0] 
? ClipWindow[0][0] : ClipWindow[1][0];
 
int Xmin= 
ClipWindow[0][0] < ClipWindow[1][0] 
? ClipWindow[0][0] : ClipWindow[1][0];
int Ymax= 
ClipWindow[0][1] > ClipWindow[1][1] 
? ClipWindow[0][1] : ClipWindow[1][1];
 
int Ymin= 
ClipWindow[0][1] < ClipWindow[1][1] 
? ClipWindow[0][1] : ClipWindow[1][1];
 
//Case 4:
if(x < Xmin)
b=b*10 + 1;
else
b=b*10 + 0;
 
//Case 3:
if(x > Xmax)
b=b*10 + 1;
else
b=b*10 + 0;
 
//Case 2:
if(y < Ymin)
b=b*10 + 1;
else
b=b*10 + 0;
 
//Case 1:
if(y > Ymax)
b=b*10 + 1;
else
b=b*10 + 0;
 
return b;
}
 
long AND(long a, long b)
{
//Using manipulated Palindrome Algo
long c;
int digitA;
int digitB;
int digitC[4];
 
for(int i=3; i>=0; i--)
{
digitA=a%10;
digitB=b%10;
 
if( digitA==1 && digitB==1)
digitC[i]=1;
else
digitC[i]=0;
 
a/=10;
b/=10;
}
c=5;
for(i=0; i<4; i++)
c=(long) c*10+digitC[i];
 
return c;
}                       //END of AND function
 
void Clip()
{
int Xmax= 
ClipWindow[0][0] > ClipWindow[1][0] 
? ClipWindow[0][0] : ClipWindow[1][0];
 
int Xmin= 
ClipWindow[0][0] < ClipWindow[1][0] 
? ClipWindow[0][0] : ClipWindow[1][0];
 
int Ymax= 
ClipWindow[0][1] > ClipWindow[1][1] 
? ClipWindow[0][1] : ClipWindow[1][1];
 
int Ymin= 
ClipWindow[0][1] < ClipWindow[1][1] 
? ClipWindow[0][1] : ClipWindow[1][1];
 
long b1, b2, b1ANDb2;
int i,j,k;
int digit_b1, digit_b2;
double Slope;
 
for(i=0; i<n; i++)
{  //FOR EACH LINE:
 
b1=CheckBit(Line[i][0], Line[i][1]);
b2=CheckBit(Line[i][2], Line[i][3]);
b1ANDb2=AND(b1,b2);
 
if( b1ANDb2 != 50000 ) //OUTSIDE
{
//Remove OUTSIDE Line Coordinates from Array
 
for(int k=i; k<n-1; k++)
{
Line[k][0]=Line[k+1][0];
Line[k][1]=Line[k+1][1];
Line[k][2]=Line[k+1][2];
Line[k][3]=Line[k+1][3];
}
n--;
i--;
 
}
 
else  //INSIDE
{
 
        //Completely Inside
        if( b1==50000 && b2==50000 )
        {
        //No Change to be made to Line Coordinates.
        }
 
 
        //Partially Inside Cases
        else if( b1!=50000 || b2!=50000 )
        {
 
        if( (Line[i][2] - Line[i][0]) == 0)
        Slope= 1.0;
        else
        Slope= 
        (double) ((Line[i][3]-Line[i][1])/(Line[i][2]-Line[i][0]));
 
 
         for(j=1; j<=4; j++)
         {
         digit_b1=b1%10;
         digit_b2=b2%10;
 
         switch(j)
         {
 
         case 1:
 
         if(digit_b1==1)
         {
         Line[i][0]= Line[i][0] + ( (Ymax - Line[i][1]) / Slope );
         Line[i][1]=Ymax;
         }
         else if(digit_b2==1)
         {
         Line[i][2]= Line[i][2] + ( (Ymax - Line[i][3] ) / Slope );
         Line[i][3]=Ymax;
         }
         break;
 
         case 2:
 
         if(digit_b1==1)
         {
         Line[i][0]= Line[i][0] + ( (Ymin - Line[i][1] ) / Slope );
         Line[i][1]=Ymin;
         }
         else if(digit_b2==1)
         {
         Line[i][2]= Line[i][2] + ( (Ymin - Line[i][3] ) / Slope );
         Line[i][3]=Ymin;
         }
         break;
 
         case 3:
 
         if(digit_b1==1)
         {
         Line[i][1]= Line[i][1] + ( ( Xmax - Line[i][0] ) * Slope) ;
         Line[i][0]=Xmax;
         }
         else if(digit_b2==1)
         {
         Line[i][3]= Line[i][3] + ( (Xmax - Line[i][2] ) * Slope) ;
         Line[i][2]=Xmax;
         }
         break;
 
         case 4:
 
         if(digit_b1==1)
         {
         Line[i][1]= Line[i][1] + ( (Xmin - Line[i][0] ) * Slope) ;
         Line[i][0]=Xmin;
         }
         else if(digit_b2==1)
         {
         Line[i][3]= Line[i][3] + ( (Xmin - Line[i][2] ) * Slope) ;
         Line[i][2]=Xmin;
         }
         break;
 
         } //End of Case
 
 
         b1/=10;
         b2/=10;
 
      } //End of inner For Loop
 
 
      b1=CheckBit(Line[i][0],Line[i][1]);
      b2=CheckBit(Line[i][2],Line[i][3]);
      b1ANDb2=AND(b1,b2);
 
 
      if( b1!=50000 || b2!=50000 )
      { i--; continue; }
 
 
} //End of Inner If-Else-if
} //End of Outer If-Else-if
} //End of Outer For Loop
}
 
 
void main()
{
clrscr();
 
UnClipped_Input();
Draw();
 
Clip();
Draw();
}


OUTPUT:







1-10 of 21