15,400,381 members
See more:
This is block of Sutherland line clipping. I am trying to add a condition here which says "No need of Clipping" if line is inside viewport. But when i am adding this condition its not getting executed. Even if line is inside viewport the message is not showing.

What I have tried:

```void cohenSutherland(double x1, double y1,
double x2, double y2)
{
int code1 = compute(x1, y1);
int code2 = compute(x2, y2);
bool accept = false;
while (true)
{
if ((code1 == 0) && (code2 == 0))
{
accept = true;
break;
}
else if (code1 & code2)
{
break;
}
else
{
int code_out;
double x, y;
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 = compute(x1, y1);
}
else
{
x2 = x;
y2 = y;
code2 = compute(x2, y2);
}
}
}
if (accept)
{
cout <<"Line accepted from " << x1 << ", "
<< y1 << " to "<< x2 << ", " << y2 << endl;
ClearDev();
drawBoundary();
draw(x1,y1,x2,y2);
}
else
cout << "Line rejected" << endl;
}```
Posted
Updated 3-Jan-22 21:14pm
v2
Rick York 4-Jan-22 2:59am

You should include the code for the compute function also along with the definitions of the TOP, BOTTOM, LEFT, and RIGHT values.

## Solution 1

You should use the debugger to see what is going on. Maybe you are leaving your code at an unexpected point, like the two breaks at first.
I dont know whether your must trigger the execution from outside when flipping the if.

Why arent you control the loop with
C++
`while( !(code1 & code2))`