0
0

# Computer graphics program in Python graphics.py and C++ graphics

## Python

``````#Experiment 7.3 - Cohen-Sutherland Line Clipping
INSIDE = 0  # 0000
LEFT = 1    # 0001
RIGHT = 2   # 0010
BOTTOM = 4  # 0100
TOP = 8     # 1000

x_max = 10.0
y_max = 8.0
x_min = 4.0
y_min = 4.0

def computeCode(x, y):
code = INSIDE
if x < x_min:      # left of rectangle
code |= LEFT
elif x > x_max:    # right of rectangle
code |= RIGHT
if y < y_min:      # below
code |= BOTTOM
elif y > y_max:    # above
code |= TOP
return code

def cohenSutherlandClip(x1, y1, x2, y2):
code1 = computeCode(x1, y1)
code2 = computeCode(x2, y2)
accept = False
while True:

if code1 == 0 and code2 == 0:
accept = True
break

elif (code1 & code2) != 0:
break

else:

x = 1.0
y = 1.0
if code1 != 0:
code_out = code1
else:
code_out = code2

if code_out & TOP:

x = x1 + (x2 - x1) * (y_max - y1) / (y2 - y1)
y = y_max
elif code_out & BOTTOM:

x = x1 + (x2 - x1) * (y_min - y1) / (y2 - y1)
y = y_min
elif code_out & RIGHT:

y = y1 + (y2 - y1) * (x_max - x1) / (x2 - x1)
x = x_max
elif code_out & LEFT:

y = y1 + (y2 - y1) * (x_min - x1) / (x2 - x1)
x = x_min

if code_out == code1:
x1 = x
y1 = y
code1 = computeCode(x1, y1)
else:
x2 = x
y2 = y
code2 = computeCode(x2, y2)
if accept:
print ("Line accepted from %.2f, %.2f to %.2f, %.2f" % (x1, y1, x2, y2))

else:
print("Line rejected")

cohenSutherlandClip(3, 5, 5, 7)
cohenSutherlandClip(9, 9, 10, 4)
cohenSutherlandClip(1, 5, 4, 1)
``````

## C++

``````#include <bits/stdc++.h>
#include <graphics.h>
using namespace std;
int xmin, xmax, ymin, ymax;
class lines {
public:
int x1, y1, x2, y2;
lines() { x1 = y1 = x2 = y2 = 0; }
void set(int a, int b, int c, int d) {
x1 = a;
y1 = b;
x2 = c;
y2 = d;
}
};

int sign(int x) {
if (x > 0)
return 1;
else
return 0;
}

void clip(lines mylines) {
int bits[4], bite[4], i, var;
setcolor(RED);
bits[0] = sign(xmin - mylines.x1);
bite[0] = sign(xmin - mylines.x2);
bits[1] = sign(mylines.x1 - xmax);
bite[1] = sign(mylines.x2 - xmax);
bits[2] = sign(ymin - mylines.y1);
bite[2] = sign(ymin - mylines.y2);
bits[3] = sign(mylines.y1 - ymax);
bite[3] = sign(mylines.y2 - ymax);

string initial = "", end = "", temp = "";

for (i = 0; i < 4; i++) {
if (bits[i] == 0)
initial += '0';
else
initial += '1';
}
for (i = 0; i < 4; i++) {
if (bite[i] == 0)
end += '0';
else
end += '1';
}

float m = (mylines.y2 - mylines.y1) / (float)(mylines.x2 - mylines.x1);
float c = mylines.y1 - m * mylines.x1;

if (initial == end && end == "0000") {
line(mylines.x1, mylines.y1, mylines.x2, mylines.y2);
return;
}

else {
for (i = 0; i < 4; i++) {
int val = (bits[i] & bite[i]);
if (val == 0)
temp += '0';
else
temp += '1';
}

if (temp != "0000") return;

for (i = 0; i < 4; i++) {
if (bits[i] == bite[i]) continue;

if (i == 0 && bits[i] == 1) {
var = round(m * xmin + c);
mylines.y1 = var;
mylines.x1 = xmin;
}

if (i == 0 && bite[i] == 1) {
var = round(m * xmin + c);
mylines.y2 = var;
mylines.x2 = xmin;
}

if (i == 1 && bits[i] == 1) {
var = round(m * xmax + c);
mylines.y1 = var;
mylines.x1 = xmax;
}

if (i == 1 && bite[i] == 1) {
var = round(m * xmax + c);
mylines.y2 = var;
mylines.x2 = xmax;
}
if (i == 2 && bits[i] == 1) {
var = round((float)(ymin - c) / m);
mylines.y1 = ymin;
mylines.x1 = var;
}
if (i == 2 && bite[i] == 1) {
var = round((float)(ymin - c) / m);
mylines.y2 = ymin;
mylines.x2 = var;
}
if (i == 3 && bits[i] == 1) {
var = round((float)(ymax - c) / m);
mylines.y1 = ymax;
mylines.x1 = var;
}
if (i == 3 && bite[i] == 1) {
var = round((float)(ymax - c) / m);
mylines.y2 = ymax;
mylines.x2 = var;
}
bits[0] = sign(xmin - mylines.x1);
bite[0] = sign(xmin - mylines.x2);
bits[1] = sign(mylines.x1 - xmax);
bite[1] = sign(mylines.x2 - xmax);
bits[2] = sign(ymin - mylines.y1);
bite[2] = sign(ymin - mylines.y2);
bits[3] = sign(mylines.y1 - ymax);
bite[3] = sign(mylines.y2 - ymax);
}
initial = "", end = "";
for (i = 0; i < 4; i++) {
if (bits[i] == 0)
initial += '0';
else
initial += '1';
}
for (i = 0; i < 4; i++) {
if (bite[i] == 0)
end += '0';
else
end += '1';
}
if (initial == end && end == "0000") {
line(mylines.x1, mylines.y1, mylines.x2, mylines.y2);
return;
} else
return;
}
}

int main() {
xmin = 40;
xmax = 100;
ymin = 40;
ymax = 80;

int gd = DETECT, gm;
initgraph(&gd, &gm, NULL);

rectangle(xmin, ymin, xmax, ymax);

lines mylines[4];

mylines[0].set(30, 65, 55, 30);
mylines[1].set(60, 20, 100, 90);
mylines[2].set(60, 100, 80, 70);
mylines[3].set(85, 50, 120, 75);

for (int i = 0; i < 4; i++) {
line(mylines[i].x1, mylines[i].y1, mylines[i].x2, mylines[i].y2);
delay(1000);
}

for (int i = 0; i < 4; i++) {
clip(mylines[i]);
delay(1000);
}
delay(4000);
getch();

closegraph();
return 0;
}
``````
SHARE
Hey! I am one of the 100,000 engineering students in India, with a step forward to support and provide resources to 99,999 other students.