Encoding and cropping Algorithm

Source: Internet
Author: User

Encoding and cropping Algorithm
Draw a window with any color, draw a line segment with a color, and crop the line segment using the encoding cropping algorithm. The cropping process must be demonstrated. The image before cropping should be drawn. When any button is pressed, the cropping result should be drawn. Use TC to open the source program:
# Include <stdio. h>
# Include <graphics. h>
# Define LEFT 1
# Define RIGHT 2
# Define BOTTOM 4
# Define TOP 8
# Define FALSE 0
# Define TRUE 1
Void Line_Clipping (x1, y1, x2, y2, xw_xmin, yw_ymin, xw_xmax, yw_ymax)
Float x1, y1, x2, y2, xw_xmin, yw_ymin, xw_xmax, yw_ymax;
{
Int draw, done;
Int code1, code2, code;
Float x, y;
Draw = FALSE; done = FALSE;
Code1 = get_code (x1, y1, xw_xmin, yw_ymin, xw_xmax, yw_ymax );
Code2 = get_code (x2, y2, xw_xmin, yw_ymin, xw_xmax, yw_ymax );
While (! Done)
{
If (code1 = 0 & code2 = 0)
{
Draw = TRUE; done = TRUE;
}
Else if (code1 & code2! = 0)
{
Done = TRUE;
} Else
{
If (code1! = 0)
Code = code1;
Else
Code = code2;
If (code & TOP )! = 0)
{Y = yw_ymax;
X = x1 + (y-y1) * (x2-x1)/(y2-y1 );

}
Else if (code & BOTTOM )! = 0)
{
Y = yw_ymin;
X = x1 + (y-y1) * (x2-x1)/(y2-y1 );
}
Else if (code & RIGHT )! = 0)
{
X = xw_xmax;
Y = y1 + (x-x1) * (y2-y1)/(x2-x1 );
}
Else if (code & LEFT )! = 0)
{
X = xw_xmin;
Y = y1 + (x-x1) * (y2-y1)/(x2-x1 );
}
If (code = code1)
{
X1 = x;
Y1 = y;
Code1 = get_code (x1, y1, xw_xmin, yw_ymin, xw_xmax, yw_ymax );
}
Else
{
X2 = x;
Y2 = y;
Code2 = get_code (x2, y2, xw_xmin, yw_ymin, xw_xmax, yw_ymax );
}
}
If (draw)
{
Setcolor (8 );
Line (x1, y1, x2, y2 );
}
}
}
Int get_code (x, y, xw_xmin, yw_ymin, xw_xmax, yw_ymax)
Float x, y, xw_xmin, yw_ymin, xw_xmax, yw_ymax;
{
Int code;
Code = 0;
If (y> yw_ymax)
Code | = TOP;
Else if (y <yw_ymin)
Code | = BOTTOM;
If (x> xw_xmax)
Code | = RIGHT;
Else if (x <xw_xmin)
Code | = LEFT;
Return code;
}
Void main ()
{
Float x1 = 76, y1 = 40, x2 = 60, y2 = 80, xw_xmin = 50, xw_xmax = 80, yw_ymin = 50, yw_ymax = 70;
Int gdriver = DETECT, gmode;
Initgraph (& gdriver, & gmode, "C: \ TURBOC2 ");
Setcolor (5 );
Line (x1, y1, x2, y2 );
Rectangle (xw_xmin, yw_ymin, xw_xmax, yw_ymax );
Getch ();
Cleardevice ();
Setcolor (8 );
Rectangle (xw_xmin, yw_ymin, xw_xmax, yw_ymax );
Line_Clipping (x1, y1, x2, y2, xw_xmin, yw_ymin, xw_xmax, yw_ymax );
Getch ();
Closegraph ();
}


From pzhsunxu's column

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.