JustPaste
HomeCategoriesAboutDonateContactTerms of UsePrivacy Policy
JustPaste

Free online notepad — write and share instantly

Navigate

  • Home
  • Timeline
  • Categories

Info

  • About
  • Donate
  • Contact

Legal

  • Terms of Use
  • Privacy Policy

© 2026 JustPaste.app. All rights reserved.

Made with ♥ by JustPaste

Prog | JustPaste.app
10 days ago8 views
👨‍💻Programming

Prog

#include<graphics.h>
#include<stdio.h>
#include<conio.h>

#define XMIN 100
#define YMIN 100
#define XMAX 400
#define YMAX 300

#define INSIDE 0
#define LEFT   1
#define RIGHT  2
#define BOTTOM 4
#define TOP    8

int computeCode(float x, float y)
{
    int code = INSIDE;

    if(x < XMIN) code |= LEFT;
    else if(x > XMAX) code |= RIGHT;

    if(y < YMIN) code |= TOP;      /* screen y grows downward */
    else if(y > YMAX) code |= BOTTOM;

    return code;
}

void cohenSutherland(float x1, float y1, float x2, float y2)
{
    int code1 = computeCode(x1, y1);
    int code2 = computeCode(x2, y2);
    int accept = 0;

    while(1)
    {
        if((code1 == 0) && (code2 == 0))
        {
            accept = 1;
            break;
        }
        else if(code1 & code2)
        {
            break;   /* trivially rejected */
        }
        else
        {
            float x, y;
            int codeOut = code1 ? code1 : code2;

            if(codeOut & TOP)
            {
                x = x1 + (x2 - x1) * (YMIN - y1) / (y2 - y1);
                y = YMIN;
            }
            else if(codeOut & BOTTOM)
            {
                x = x1 + (x2 - x1) * (YMAX - y1) / (y2 - y1);
                y = YMAX;
            }
            else if(codeOut & RIGHT)
            {
                y = y1 + (y2 - y1) * (XMAX - x1) / (x2 - x1);
                x = XMAX;
            }
            else /* LEFT */
            {
                y = y1 + (y2 - y1) * (XMIN - x1) / (x2 - x1);
                x = XMIN;
            }

            if(codeOut == code1)
            {
                x1 = x; y1 = y;
                code1 = computeCode(x1, y1);
            }
            else
            {
                x2 = x; y2 = y;
                code2 = computeCode(x2, y2);
            }
        }
    }

    if(accept)
    {
        setcolor(RED);
        rectangle(XMIN, YMIN, XMAX, YMAX);   /* clipping window */
        line((int)x1, (int)y1, (int)x2, (int)y2);
    }
    else
    {
        printf("\nLine completely outside clipping window.\n");
    }
}

void main()
{
    int gd = DETECT, gm;
    float x1, y1, x2, y2;

    printf("Enter x1 y1 x2 y2 of line: ");
    scanf("%f %f %f %f", &x1, &y1, &x2, &y2);

    initgraph(&gd, &gm, "c:\\turboc3\\bgi");

    setcolor(YELLOW);
    rectangle(XMIN, YMIN, XMAX, YMAX);
    setcolor(WHITE);
    line((int)x1, (int)y1, (int)x2, (int)y2);
    getch();

    cleardevice();
    cohenSutherland(x1, y1, x2, y2);

    getch();
    closegraph();
}
← Back to timeline