What is clipping? Clip the line with end points (0,0) and (12,12) using Cohen Sutherland algorithm. The clipping rectangle is defined with bottom left edge as (1,1) and top right edge as (10,10
Answers
Answer:
Cohen-Sutherland algorithm divides a two-dimensional space into 9 regions and then efficiently determines the lines and portions of lines that are inside the given rectangular area.
Explanation:
In this algorithm, we are given 9 regions on the screen. Out of which one region is of the window and the rest 8 regions are around it given by 4 digit binary. The division of the regions are based on (x_max, y_max) and (x_min, y_min).
The central part is the viewing region or window, all the lines which lie within this region are completely visible. A region code is always assigned to the endpoints of the given line.
Program :
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: # to the left of rectangle
code |= LEFT
else if x > x_max: # to the right of rectangle
code |= RIGHT
if y < y_min:
code |= BOTTOM
else if y > y_max:
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
else if (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
else if code_out & BOTTOM:
x = x1 + (x2 - x1) * \
(y_min - y1) / (y2 - y1)
y = y_min
else if code_out & RIGHT:
y = y1 + (y2 - y1) * \
(x_max - x1) / (x2 - x1)
x = x_max
else if 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(5, 5, 7, 7)
cohenSutherlandClip(7, 9, 11, 4)
cohenSutherlandClip(1, 5, 4, 1)
For more related question : https://brainly.in/question/2439894
#SPJ1