two-dimensional graphic visual and cropping
one. Objective:
1. Understand and consolidate the meaning of line cutting.
2. Master Cohen-sutherland Line Cutting method.
two. Requirements:
1. Draw a simpler geometry on the screen.
2. The Cohen-sutherland algorithm is used to cut line segments and display the cropping results in the display window.
3. Using Sutherland-hodgema algorithm (Polygon reduction algorithm principle) to achieve polygon cropping, and crop results displayed in the display window.
three. Principle and key algorithm:
1. Cohen-sutherland algorithm (coding algorithm)
Principle of line algorithm:
For each line segment P1P2 is divided into three different cases:
(1) If the P1P2 is completely inside the window, the segment p1p2 is displayed.
(2) If the P1P2 is obviously outside the window, the segment is discarded.
(3) If the line segment does not meet (1) or (2) conditions, at the intersection of the line segment divided into two segments. One paragraph is completely outside the window and can be discarded. Then repeat the above treatment for another paragraph.
For quick judgment, the following coding method is used: The two-dimensional plane is divided into 9 regions by the line of the four edge of the window (right), each region is given a four-bit encoding: CTCBCRCL (up and down right); the endpoints of the line are given the region code in their area to identify where the endpoint is relative to the trimmed rectangle boundary.
You encode the meaning:
On: If Y>ymax,ct=1,else, 0;
Under: If Y<ymax,cb=1,else, 0;
Right: if x>xmax,cr=1,else, 0;
Left: if x<xmax,cl=1,else, 0;
The bit and operation of the area code of the two endpoints of a segment shows whether the two endpoints are in the upper, lower, left and right of the viewport;
‐ if the two-point encoding is 0000, the line is in the window.
‐ if the two-point encoding phase is not 0000, the line is outside the window.
‐ if the two-point encoding is not all 0000, but the phase is 0000, the line part is visible, and the intersection of the line and the window needs to be computed to determine which part is visible.
2.sutherland-hodgema algorithm (Polygon reduction algorithm principle):
By cutting the single edge or surface, the cutting strategy of polygon is realized: the clipping of the polygon about the rectangular window is decomposed into a polygon about the line of the four edges of the window. Crop the polygon at once with one edge of the window. pipelined process (bottom left): The front result is the input behind. Also known as Edge clipping algorithm.
Each output of the algorithm (including the intermediate result) is the vertex table of a polygon, and all vertices are located on the visible side of the corresponding window trimmed edges or faces. Because each edge of a polygon needs to be compared to a trimmed edge or a surface, you only need to discuss the possible positional relationships between a single edge and an individual trimmed edge or surface. Assuming that S,p is the two adjacent vertex of the polygon, and S is the starting point for the edge, and P is the end of the edge, there are only 4 possible relationships between the variable SP and the trimmed edge or surface.
Divide the plane into two parts: one part contains the window, becomes the visible side, and the other part is called the Invisible side. The two ends of the polygon are considered in the order of S,p. Their position with the clipping line is only 4.
(1) S,p are on the visible side, the P-point (2) s,p is not on the visible side
(3) S is visible, p is invisible (4) s is not visible, p is visible
Visible from the above, each time the edge of the polygon and trimmed edge or surface comparison, output one or two vertices, there may be no output point. If the SP side is fully visible, the output p point is not required to output the start S, because the vertices are processed sequentially, and S is exported as the end point of the previous side. If the SP side is completely invisible, there is no output. If the SP side portion is visible, the SP side may enter or leave the visible side of the trimmed edge or surface. If the SP side leaves the visible side of the trimmed edge or surface, the output SP is trimmed or a personal point. If the SP side enters the visible side of the trimmed edge or surface, the output is two points, one for the SP and the cropping edge or surface, and one for the P point.
For the first vertex of a polygon, you only need to determine its visibility. If visible, the output is as the starting point S, otherwise there is no output, but it is saved as s for subsequent point processing.
For the last edge PnP1, it is handled by marking the first vertex as F, so that the last edge is PNF and can be treated the same as the other side.
Implementation method:
Set two tables:
Input vertex table: the vertex p1-pm for storing the cropped polygon.
Output vertex table: used to store the vertex q1-qn of the cropping process and the results.
The vertices in the input vertex table are ordered in a certain order, which can be used in clockwise or counterclockwise direction. The is trimmed relative to the edges of the cropping window by the order of the vertex table.
3.cohen-sutherland Line clipping core algorithm:
Algorithm Description:
1. While for each window edge or face do begin
2). If P1 the visible side of the window then output P1
3). For I=1 to n do begin
4). If pi is on the visible side of the window then
5). The If pi+1 then output pi+1 6 on the visible side of the window edge
. Else computes the intersection point and outputs intersection
7). else if pi+1 on the visible side of the window, then compute the intersection point and output the intersection point, outputting pi+1 end
8). End of Algorithm<span style= "font-family: XXFarEastFont-Arial Backgroun D-color:rgb (255, 255, 255); > </span>
Four Key code:
1. Encode the region:
int Ccg_wuping2dtransview::p code (int *x, int *y)
{
int code = 0;
ccg_wuping2dtransdoc* PDoc = GetDocument ();
if (*x < PDoc->m_wndrectangle[0])
Code = Code | Left;
else if (*x > PDoc->m_wndrectangle[1])
Code = Code | Right;
if (*y < PDoc->m_wndrectangle[3])
Code = Code | BOTTOM;
else if (*y > PDoc->m_wndrectangle[2])
Code = Code | Top;
return code;
}
2.cohen-sutherland Line cropping:
int ccg_wuping2dtransview::clipline (int *x1, int *y1, int *x2, int *y2) {int tempx, tempy;
ccg_wuping2dtransdoc* PDoc = GetDocument ();
int Pcode1, Pcode2, Pcode;
Pcode1 = Pcode (x1, y1);
Pcode2 = Pcode (x2, y2);
if (Pcode1 = = 0 && Pcode2 = = 0) {return 1;
while (pcode1!= 0 | | | Pcode2!= 0) {if (Pcode1 & Pcode2)!= 0) {return 0;
} Pcode = Pcode1;
if (Pcode1 = = 0) Pcode = pcode2;
if ((Left & Pcode)!= 0) {tempx = PDoc->m_wndrectangle[0];
Tempy = *y1 + (*y2-*y1) * (PDoc->m_wndrectangle[0]-*x1)/(*X2-*x1);
else if ((Right & Pcode)!= 0) {tempx = PDoc->m_wndrectangle[1];
Tempy = *y1 + (*y2-*y1) * (PDoc->m_wndrectangle[1]-*x1)/(*X2-*x1);
else if ((Top & Pcode)!= 0) {tempy = PDoc->m_wndrectangle[2];
TEMPX = *x1 + (*X2-*x1) * (PDoc->m_wndrectangle[2]-*y1)/(*y2-*y1);
else if ((BOTTOM & Pcode)!= 0) {tempy = PDoc->m_wndrectangle[3];
TEMPX = *x1 + (*X2-*x1) * (PDoc->m_wndrectangle[3]-*y1)/(*y2-*y1);
} if (Pcode = = pcode1) {*x1 = tempx;
*y1 = Tempy;
Pcode1 = Pcode (x1, y1);
else if (Pcode = = pcode2) {*x2 = tempx;
*y2 = Tempy;
Pcode2 = Pcode (x2, y2);
} return 1;
}
3. Determine whether each edge of the polygon is visible: (The following functions are used to achieve polygon cropping)
int Ccg_wuping2dtransview::p visible (int x, int y, int i)
{
int visible = 0;
ccg_wuping2dtransdoc* PDoc = GetDocument ();
switch (i)
{case
0:
if (x >= pDoc->m_wndrectangle[0])
visible = 1;
break;
Case 1:
if (x <= pDoc->m_wndrectangle[1])
visible =1;
break;
Case 2:
if (y <= pDoc->m_wndrectangle[2])
visible = 1;
break;
Case 3:
if (y >= pDoc->m_wndrectangle[3])
visible = 1;
break;
return visible;
}
4. To determine whether or not to make a request:
int ccg_wuping2dtransview::linecross (int x1, int y1, int x2, int y2, int i)
{
int visible1,visible2;
Visible1 = pvisible (x1, y1, i);
Visible2 = pvisible (x2, y2, i);
if (visible1!= visible2) return
1;
else return
0;
}
5. Find the intersection of the polygon edge and the cropping window:
void Ccg_wuping2dtransview::intersect (int Sx, int Sy, int Px, int Py, int i, int *ix, int *iy)/Find intersection
{
ccg_ wuping2dtransdoc* PDoc = GetDocument ();
switch (i)
{case
0://left
*ix = pdoc->m_wndrectangle[0];
*iy = (float) (py-sy)/(PX-SX) * (Pdoc->m_wndrectangle[0]-Sx) + Sy;
break;
Case 1://Right
*ix = pdoc->m_wndrectangle[1];
*iy = (float) (py-sy)/(PX-SX) * (Pdoc->m_wndrectangle[1]-Sx) + Sy;
break;
Case 2://top
*iy = pdoc->m_wndrectangle[2];
*ix= (float) (PX-SX)/(Py-sy) * (pdoc->m_wndrectangle[2)-Sy) + Sx;
break;
Case 3://Bottom
*iy = pdoc->m_wndrectangle[3];
*ix = (float) (PX-SX)/(Py-sy) * (Pdoc->m_wndrectangle[3]-Sy) + Sx;
break;
}
}
6. Storage Polygon Output vertex:
void Ccg_wuping2dtransview::output (int x, int y, int *nout, int *tempx, int *tempy)
{
tempx[*nout] = x;
Tempy[*nout] = y;
(*nout) + +;
}
Five Results:
1. Line cutting:
2. Polygon cropping:
3. Cropping window Mobile cropping: