C++ Programming Blog

 
 
 
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<graphics.h>
#define ROUND(a) ((int)(a+0.5))
#define n 4

#define LEFT_EDGE 0x1
#define RIGHT_EDGE 0x2
#define BOTTOM_EDGE 0x4
#define TOP_EDGE 0x8

#define INSIDE(a)  (!a)
#define REJECT(a,b) (a&b)
#define ACCEPT(a,b) (!(a|b))

typedef struct wcpt2
{
    int x,y;
}wcpt2;

typedef struct dcpt
{
    int x,y;
}dcpt;

void main()
{
    int gd=DETECT,gm;
    int left,top,right,bottom;
    int x1,x2,y1,y2;
    int maxx, maxy;
       /* our polygon array */
    int poly[10];
    void clipline(dcpt,dcpt,wcpt2,wcpt2);
    clrscr();

    initgraph(&gd,&gm,\"c:\\\\tc30\\\\bgi\");
    maxx = getmaxx()/4;
    maxy = getmaxy()/4;

    poly[0] = 20;        /* 1st vertex */
    poly[1] = maxy / 2;

    poly[2] = maxx - 10; /* 2nd */
    poly[3] = 10;

    poly[4] = maxx - 50; /* 3rd */
    poly[5] = maxy - 20;

    poly[6] = maxx / 2;  /* 4th */
    poly[7] = maxy / 2;

/*   drawpoly doesn\'t automatically close
   the polygon, so we close it.
*/
    poly[8] = poly[0];
    poly[9] = poly[1];

   /* draw the polygon */
    drawpoly(5, poly);

    rectangle(20,25,80,125);
    wcpt2 pt1,pt2;
    dcpt winmin,winmax;

    winmin.x=20;
    winmin.y=25;
    winmax.x=80;
    winmax.y=125;

    pt1.x=20;
    pt1.y=maxy/2;
    pt2.x=maxx-10;
    pt2.y=10;

//    clipline(winmin,winmax,pt1,pt2);

    int i=0;
    for(int index=0;index<n;index++)
    {
        if(index==n-1)
        {
            pt1.x=poly[i];
            pt1.y=poly[i+1];
            i=0;
            pt2.x=poly[i];
            pt2.y=poly[i+1];
            clipline(winmin,winmax,pt1,pt2);
        }
        else
        {
            pt1.x=poly[i];
            pt1.y=poly[i+1];
            pt2.x=poly[i+2];
            pt2.y=poly[i+3];
            clipline(winmin,winmax,pt1,pt2);
        }
        i+=2;
    }
    pt1.x=poly[i];
    pt1.y=poly[i+1];
    clipline(winmin,winmax,pt1,pt2);
    getch();
}


unsigned char encode(wcpt2 pt,dcpt winmin,dcpt winmax)
{
    unsigned char code=0x00;
    if(pt.x < winmin.x)
        code=code | LEFT_EDGE;
    if(pt.x > winmax.x)
        code=code | RIGHT_EDGE;
    if(pt.y < winmin.y)
        code=code | TOP_EDGE;
    if(pt.y > winmax.y)
        code=code | BOTTOM_EDGE;
    return code;
}


void swappts(wcpt2 *p1,wcpt2 *p2)
{
    wcpt2 tmp;
    tmp = *p1;
    *p1 = *p2;
    *p2 = tmp;
}


void swapcode(unsigned char *c1,unsigned char *c2)
{
    unsigned char tmp;
    tmp = *c1;
    *c1 = *c2;
    *c2 = tmp;
}


void clipline(dcpt winmin,dcpt winmax,wcpt2 p1,wcpt2 p2)
{
    unsigned char encode(wcpt2,dcpt,dcpt);
    unsigned char code1,code2;
    int done = 0 , draw = 0;
    float m;
    void swapcode(unsigned char *c1,unsigned char *c2);
    void swappts(wcpt2 *p1,wcpt2 *p2);

    while(!done)
    {
        code1 = encode(p1,winmin,winmax);
        code2 = encode(p2,winmin,winmax);
        if(ACCEPT(code1,code2))
        {
            draw = 1;
            done = 1;
        }
        else if(REJECT(code1,code2))
            done = 1;
        else if(INSIDE(code1))
        {
                swappts(&p1,&p2);
                swapcode(&code1,&code2);
        }
        if(code1 & LEFT_EDGE)
        {
                p1.y += (winmin.x - p1.x) *  (p2.y - p1.y) / (p2.x - p1.x);
                p1.x = winmin.x;
        }
        else if(code1 & RIGHT_EDGE)
        {
                p1.y += (winmax.x - p1.x) *  (p2.y - p1.y) / (p2.x - p1.x);
                p1.x = winmax.x;
        }
        else if(code1 & TOP_EDGE)
        {
            if(p2.x != p1.x)
                p1.x += (winmin.y - p1.y) *  (p2.x - p1.x) / (p2.y - p1.y);
                p1.y = winmin.y;
        }
        else if(code1 & BOTTOM_EDGE)
        {
            if(p2.x != p1.x)
                p1.x += (winmax.y - p1.y) *  (p2.x - p1.x) / (p2.y - p1.y);
                p1.y = winmax.y;
        }
    }
    if(draw)
    {
    setcolor(5);
    line(p1.x,p1.y,p2.x,p2.y);
    }
}

    Related Post:
  1. To parse a string using Operator Precedence parsing

  2. Comment line in c

  3. Program to construct Newtons Forward Difference Interpolation Formula from the given distinct equally spaced data points

  4. Program that provides an example of binary files

  5. Program to evaluate series

  6. Program to estimate the value of Fourth Derivative of the function at the given points from the given data using Central Difference Formula of order 2

  7. Program of vector class that perform different operations on vector

  8. Program that places n equally spaced points on the circumference of a circle of radius r,and then join each point to every other point

  9. Program that provides an example of destructors

  10. Program to estimate the Integral value of the function at the given points from the given data using Simpsons 1/3 Rule

  11. Program to fill a Polygon using Scan Line Polygon Fill Algorithm

  12. Program to illustrate the implementation of Scaling Transformation along Arbitrary Direction

  13. Program to show the 3D Shearing Transformation along y-axis

  14. Program that reads two numbers from user, computes and display their sum.

  15. Program to perform shell sort

  16. Program to fill a Circle using Scan-Line Circle Fill Algorithm using Polar Coordinates

  17. Program to illustrate the Bubble Sorting of arrays

  18. Program to illustrate the implementation of Stack as an Arithmetic Expression Evaluater

  19. Program to convert an Infix Expression into a Postfix Expression using Linked List as a Stack

  20. Program to illustrate supplying of values to pointer variables

 
 
Didn't find what you were looking for? Find more on Program of Sutherland Hodgemann Algorithm for Polygon clipping