Friday 17 January 2014

Cohen-Sutherland Line Clipping Algorithm with C/C++

Theory
In this method, every line endpoint is assigned a four digit binary code(region code) that identifies the location of the point relative to the boundary.
b1 : left 
b2 : right 
b3 : below 
b4 : above
The value 1 indicates its relative position. If a point is within clipping rectangle then region code is 0000. So , 
If x – xwmin < 0 , b1 = 1 
If xwmax – x < 0 , b2 = 1
If y – ywmin < 0 , b3 = 1 
If ywmin – y < 0 , b4 = 1 
If the region codes of both end points are 0000 then we accept
the line.  Any  line that have one in the same bit position is rejected  i.e  If RA AND RB ≠ 0 ,line is completely outside . 
The lines which con not be identified as completely inside or outside a window by these tests are checked for intersection with the window boundary. Such lines may or may not cross into the window interior.
 
Source Code
Note: to run this code in your machine with Code::blocks IDE, add a link library libgdi32.a (it is usually inside MinGW\lib )  in linker setting.
?
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
#include <windows.h>
#include <cmath>
#define ROUND(a) ((int) (a + 0.5))
 
#define LEFT_EDGE 0x1
#define RIGHT_EDGE 0x2
#define BOTTOM_EDGE 0x4
#define TOP_EDGE 0x8
 
#define INSIDE(a) (!a)
#define REJECT(a,b) (a&b)
#define ACCEPT(a,b) (!(a|b))
/* set window handle */
 
static HWND sHwnd;
 
static COLORREF redColor=RGB(255,0,0);
static COLORREF blueColor=RGB(0,0,255);
static COLORREF greenColor=RGB(0,255,0);
 
typedef struct Point
{
    int x;
    int y;
}pt;
 
pt winMin = {100, 100};
pt winMax = {800, 400};
pt p1 = {200, 200};
pt p2 = {900, 300};
 
void SetWindowHandle(HWND hwnd){
    sHwnd=hwnd;
}
 
/* SetPixel */
void setPixel(int x,int y,COLORREF& color=redColor){
    if(sHwnd==NULL){
        MessageBox(NULL,"sHwnd was not initialized !","Error",MB_OK|MB_ICONERROR);
        exit(0);
    }
    HDC hdc=GetDC(sHwnd);
    SetPixel(hdc,x,y,color);
    ReleaseDC(sHwnd,hdc);
    return;
 
}
 
void LineBresenham (int xa, int ya, int xb, int yb){
    int dx = (xb - xa), dy = (yb - ya);
    int x, y, xEnd, yEnd;
    int p = 2 * abs(dy) - abs(dx);
    int twoDy = 2 * abs(dy), twoDyDx = 2 * (abs(dy) - abs(dx));
    int tFactor = dx*dy;
    if(xa > xb){
        x = xb;
        y = yb;
        xEnd = xa;
    }else{
        x = xa;
        y = ya;
        xEnd = xb;
    }
    setPixel(x, y);
    while(x < xEnd){
        x++;
        if(p < 0)
            p += twoDy;
        else{
            if(tFactor > 0) //for positive slope
                y++;
            else //for negative slope
                y--;
            p += twoDyDx;
        }
        setPixel(x, y);
    }
}
 
void drawBoundary(pt winMin, pt winMax){
    int x, y;
    x = winMin.x;
    for(y = winMin.y; y < winMax.y; y++){
        setPixel(x,y, blueColor);
    }
    y = winMin.y;
    for(x = winMin.x; x < winMax.x; x++){
        setPixel(x,y, blueColor);
    }
    x = winMax.x;
    for(y = winMin.y; y < winMax.y; y++){
        setPixel(x,y, blueColor);
    }
    y = winMax.y;
    for(x = winMin.x; x < winMax.x; x++){
        setPixel(x,y, blueColor);
    }
 
}
 
unsigned char encode(pt Point, pt winMin, pt winMax){
    unsigned char code = 0x00;
    if(Point.x < winMin.x)
        code = code | LEFT_EDGE;
    if(Point.x > winMax.x)
        code = code | RIGHT_EDGE;
    if(Point.y < winMin.y)
        code = code | BOTTOM_EDGE;
    if(Point.y > winMax.y)
        code = code | TOP_EDGE;
    return (code);
}
 
void swapPts(pt *p1, pt *p2){
    pt temp;
    temp = *p1;
    *p1 = *p2;
    *p2 = temp;
}
 
void swapCodes (unsigned char *c1, unsigned char *c2){
    unsigned char temp;
    temp = *c1;
    *c1 = *c2;
    *c2 = temp;
}
 
void clipLine (pt winMin, pt winMax, pt p1, pt p2){
    unsigned char code1, code2;
    int done = FALSE, draw = FALSE;
    float m;
 
    while(!done){
        code1 = encode (p1, winMin, winMax);
        code2 = encode (p2, winMin, winMax);
        if(ACCEPT(code1, code2)){
            done = TRUE;
            draw = TRUE;
        }
        else
            if(REJECT (code1, code2))
                done = TRUE;
            else{
                if (INSIDE(code1)){
                    swapPts(&p1, &p2);
                    swapCodes (&code1, &code2);
                }
 
                if(p2.x != p1.x)
                    m = (p2.y - p1.y) / (p2.x - p1.x);
                if(code1 & LEFT_EDGE){
                    p1.y += (winMin.x - p1.x) * m;
                    p1.x = winMin.x;
                }else
                    if(code1 & RIGHT_EDGE){
                        p1.y += (winMax.x - p1.x) * m;
                        p1.x = winMax.x;
                    }else
                        if(code1 & BOTTOM_EDGE){
                            if(p2.x != p1.x)
                                p1.x += (winMin.y - p1.y) / m;
                            p1.y = winMin.y;
                        }else
                            if(code1 & TOP_EDGE){
                                if(p2.x != p1.x)
                                    p1.x += (winMax.y - p1.y)/m;
                                p1.y = winMax.y;
                            }
 
            }
    }
    if(draw){
        LineBresenham(ROUND(p1.x), ROUND(p1.y), ROUND(p2.x), ROUND(p2.y));
    }
}
 
/* Window Procedure WndProc */
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam){
 
    switch(message){
        case WM_PAINT:
            SetWindowHandle(hwnd);
            drawBoundary(winMin, winMax);
            clipLine(winMin, winMax, p1, p2 );
            break;
        case WM_CLOSE: // FAIL THROUGH to call DefWindowProc
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
        default:
        break; // FAIL to call DefWindowProc //
    }
    return DefWindowProc(hwnd,message,wParam,lParam);
}
 
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int iCmdShow){
    static TCHAR szAppName[] = TEXT("Line Clipping");
    WNDCLASS wndclass;
    wndclass.style         = CS_HREDRAW|CS_VREDRAW ;
    wndclass.lpfnWndProc   = WndProc ;
    wndclass.cbClsExtra    = 0 ;
    wndclass.cbWndExtra    = 0 ;
    wndclass.hInstance     = hInstance ;
    wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
    wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
    wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
    wndclass.lpszMenuName  = NULL ;
    wndclass.lpszClassName = szAppName ;
 
    // Register the window //
    if(!RegisterClass(&wndclass)){
        MessageBox(NULL,"Registering the class failled","Error",MB_OK|MB_ICONERROR);
        exit(0);
    }
 
    // CreateWindow //
    HWND hwnd=CreateWindow(szAppName,"Cohen Sutherland Algorithm - Programming Techniques",
                WS_OVERLAPPEDWINDOW,
                 CW_USEDEFAULT,
                 CW_USEDEFAULT,
                 CW_USEDEFAULT,
                 CW_USEDEFAULT,
                 NULL,
                 NULL,
                 hInstance,
                 NULL);
    if(!hwnd){
        MessageBox(NULL,"Window Creation Failed!","Error",MB_OK);
        exit(0);
    }
    // ShowWindow and UpdateWindow //
    ShowWindow(hwnd,iCmdShow);
    UpdateWindow(hwnd);
 
    // Message Loop //
    MSG msg;
    while(GetMessage(&msg,NULL,0,0)){
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    /* return no error to the operating system */
    return 0;
}

Output

CohenSutherland


0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Affiliate Network Reviews