Click here to Skip to main content
15,400,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Comments
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.

1 solution

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))
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900