-
Notifications
You must be signed in to change notification settings - Fork 0
/
line clip
165 lines (151 loc) · 3.65 KB
/
line clip
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
/******************************************************************************
Online C Compiler.
Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <stdio.h>
int xmin,xmax,ymin,ymax;
struct lineclipping{
float x,y;
int outcode[4];
}p1,p2;
void calculate_outcode(struct lineclipping *p){
for(int i=0;i<4;i++){
p->outcode[i]=0;
}
if(p->x<xmin){
p->outcode[3]=1;
}
if(p->x>xmax){
p->outcode[2]=1;
}
if(p->y<ymin){
p->outcode[1]=1;
}
if(p->y>ymax){
p->outcode[0]=1;
}
}
void calculate(){
float m;
m=(float)(p2.y-p1.y)/(p2.x-p1.x);
int flag=-1;
float x1,y1;
float x2,y2;
for(int i=0;i<4;i++){
if(p1.outcode[i]==1){
flag=i;
}
}
if (flag==-1){
printf("\nno intersection for p1");
}
else{
if(flag==3){
x1=xmin;
y1=p1.y+m*(x1-p1.x);
printf("\npoint of intersection x and y are:%f\t%f",x1,y1);
}
else if(flag==2){
x1=xmax;
y1=p1.y+m*(x1-p1.x);
printf("\npoint of intersection x and y are:%f\t%f",x1,y1);
}
else if(flag==1){
y1=ymin;
x1=p1.x+(y1-p1.y)/m;
printf("\npoint of intersection x and y are:%f\t%f",x1,y1);
}
else if(flag==0){
y1=ymax;
x1=p1.x+(y1-p1.y)/m;
printf("\npoint of intersection x and y are:%f\t%f",x1,y1);
}
}
//for p2
flag=-1;
for(int i=0;i<4;i++){
if(p2.outcode[i]==1){
flag=i;
}
}
if (flag==-1){
printf("no intersection for p2");
}
else{
if(flag==3){
x2=xmin;
y2=p1.y+m*(x2-p1.x);
printf("\npoint of intersection x and y are:%f\t%f",x2,y2);
}
else if(flag==2){
x2=xmax;
y2=p1.y+m*(x2-p1.x);
printf("\npoint of intersection x and y are:%f\t%f",x2,y2);
}
else if(flag==1){
y2=ymin;
x2=p1.x+(y2 - p1.y)/m;
printf("\npoint of intersection x and y are:%f\t%f",x2,y2);
}
else if(flag==0){
y2=ymax;
x2=p1.x+(y2-p1.y)/m;
printf("\npoint of intersection x and y are:%f\t%f",x2,y2);
}
}
}
void find(){
int flag=0;
int outcode1[4];
for(int i=0;i<4;i++){
outcode1[i]=p1.outcode[i]&&p2.outcode[i];
if(outcode1[i]==1){
flag=1;
break;
}
}
if(flag==1){
printf("\ncompletely rejected");
}
else{
printf("\nIt is partially accepted");
calculate();
}
}
void check(){
int flag;
for(int i=0;i<4;i++){
if(p1.outcode[i]==0&&p2.outcode[i]==0){
flag=0;
}
else{
flag=1;
break;
}
}
if(flag==0){
printf("Accepted");
}
else{
printf("\npartially accepted or rejected");
find();
}
}
int main()
{
printf("Enter the coordinates of pixel p1:");
scanf("%f%f",&p1.x,&p1.y);
printf("Enter the coordinates of pixel p2:");
scanf("%f%f",&p2.x,&p2.y);
printf("Enter Xmax ,Ymax:");
scanf("%d%d",&xmax,&ymax);
printf("Enter Xmin ,Ymin:");
scanf("%d%d",&xmin,&ymin);
calculate_outcode(&p1);
calculate_outcode(&p2);
printf("outcode of p1:\t%d\t%d\t%d\t%d",p1.outcode[0],p1.outcode[1],p1.outcode[2],p1.outcode[3]);
printf("\noutcode of p2:\t%d\t%d\t%d\t%d",p2.outcode[0],p2.outcode[1],p2.outcode[2],p2.outcode[3]);
check();
return 0;
}