import matplotlib.pyplot as plt
## Cordinates of the clipping window
X_MIN = 30
X_MAX = 100
Y_MIN = 30
Y_MAX = 100
## Initialize 4 region bit
left = 1
right = 2
bottom = 4
top = 8
class line:
def __init__(self):
self.visibility = 0
def input(self):
self.xstart = int(input("Enter x1: "))
self.ystart = int(input("Enter y1: "))
self.xend = int(input("Enter x2: "))
self.yend = int(input("Enter y2: "))
def getRegCode(x,y):
code = 0
if(x > X_MAX):
code |= right
elif(x < X_MIN):
code |= left
elif(y < Y_MIN):
code |= bottom
elif (y >Y_MAX):
code |= top
return code
def evaluate_intersaction(L,out):
slope = (L.yend - L.ystart) / (L.xend - L.xstart)
if (out & left):
xn = X_MIN
yn = L.ystart + (slope)*(XMIN - L.xstart)
elif(out & right):
xn = X_MAX
yn = L.ystart + (slope)*(XMAX - L.xstart)
elif(out & bottom):
yn = Y_MIN
xn = L.xstart + (1/slope)*(Y_MIN - L.xstart)
elif(out & top):
yn = Y_MAX
xn = L.xstart + (1/slope)*(Y_MAX - L.xstart)
new = (xn,yn)
return new
def decide_clipping(L: line):
e1 = getRegCode(L.xstart,ystart)
e2 = getRegCode(L.xend,L.yend)
if e1 | e2 == 0:
L.visibility = 0
print("Trivially accepted: Line is fully inside of the window")
elif (e1 & e2 != 0):
L.visibility = 0
print("Trivially rejected: Line is fully outside of the window")
else:
if(e1 != 0):
outside = e1
else:
outside = e2
new_pt = evaluate_intersaction(L,outside)
return new_pt
line1 = line()
line1.input()
print(line1.xstart,line1.ystart,line1.xend,line1.yend)
plt.plot(
[X_MIN,X_MIN,X_MAX,X_MAX,X_MIN],
[Y_MIN,Y_MAX,Y_MAX,Y_MIN,Y_MIN]
)
plt.plot([line1.xstart,line1.xend] , [line1.ystart , line1.yend])
plt.show()4 views