Sutherland-hotman Algorithm
The Sutherland-hodman algorithm is also called the edge-by-edge cropping method, which was proposed by sastherland (I. e. Sutherland) and hodman in 1974. This algorithm adopts the segmentation and edge-based cropping methods.
I. Basic Ideas:
Crop a polygon with an edge of the window at a time.
Consider the cropping line composed of an edge of the window and the extension line. The line divides the plane into two parts: visible side and invisible side. Points S and P at each side of a polygon. There are only four relationships between them and the position of the cropping line.
Case (1) output only 1 vertex P;
Case (2) Output 0 vertices;
Case (3) output line segment SP and cropping line 1 intersection I;
Case (4) output line segment SP and cropping line 1 intersection I and 1 end P
Ii. Algorithm Implementation:
1. Known: polygon vertex array SRC, number of vertices N,
Define the new polygon vertex array DeST.
2. Initial Value assignment: The variable flag is used for identification:
0 indicates inside, and 1 indicates outside.
3. process n edges of a polygon. Consider the following for the current vertex number: 0 ~ N-1.
For (I = 0; I <n; I ++)
{
If (is the current vertex I inside the boundary ?)
{
If (flag! = 0)/* is the previous point located outside? */
{
Flag = 0;/* set the flag to 0 as the first sign of the next cycle */
(DEST + J) = obtain the intersection point;/* place the intersection point DEST into the new polygon */
J ++;
}
(DEST + J) = (SRC + I);/* Add the current vertex SRCI to the new polygon */
J ++;
}
Else
{
If (flag = 0)/* is the previous point inside? */
{
Flag = 1;/* set the flag to 1 as the first sign of the next loop */
(DEST + J) = obtain the intersection point;/* place the intersection point DEST into the new polygon */
J ++;
}
}
S = (SRC + I);/* use the current point as the first point of the next loop */
}
Iii. algorithm features:
The Sutherland-hooreman polygon cropping algorithm is generic. The cropped polygon can be any convex polygon or concave polygon. The cropping window is not limited to a rectangle, but can be any convex polygon.
The preceding algorithm is used to crop a boundary of a polygon to a window. The algorithm program is called for each boundary of the window in sequence, use the result polygon of the previous cropping as the cropped polygon of the next cropping to obtain the complete polygon cropping program.
View plaincopy to clipboardprint?
- // Point on the side of a directed Line Segment
- /*
- Vector cross-Product Method
- It is a simple meter, and the test point is expressed as a point P. Assume that the window boundary direction is clockwise, as shown in. For any of the boundary vectors, we can see from starting point A to end B:
- If the tested point P is on the right (inside) of the boundary, the direction of the AB × AP is perpendicular to the X-Y plane and points to the screen, that is, the negative direction of the Z axis in the right coordinate system.
- In turn, if p is on the left of the boundary line (that is, the outer side), then the direction of AB × AP is perpendicular to the X-Y plane and points to the outside of the screen, that is, the positive direction of the Z axis in the right hand coordinate system.
- Set: point P (x, y), point A (XA, ya), point B (XB, Yb ),
- Vector AB = {(XB-XA), (Yb-Ya )},
- Vector ap = {(X-XA), (Y-Ya )},
- Then the direction of AB × AP can be determined by the following symbol:
- V = (XB-XA) · (Y-Ya)-(x-XA) · (Yb-Ya) (3-14)
- Therefore, when v ≤ 0, P is inside the boundary;
- When v> 0, P is outside the boundary.
- */
- Static int _ rtinside (rtvector vector, rtpoint point)
- {
- Return (vector. ep. x-vector. SP. x) * (point. y-vector. SP. y)-(vector. ep. y-vector. SP. y) * (point. x-vector. SP. X );
- }
- // The polygon point must be clockwise.
- Int rtprunepsh (rtpoint * SRC, int num, rtpoint ** DEST, int * total)
- {
- Int I = 0, j = 0, K =-1, flag = 0;
- Rtpoint start, stop; // The cropped polygon.
- Rtpoint sp, EP; // cropping window
- Rtpoint * temp = NULL;
- Temp = (rtpoint *) malloc (sizeof (rtpoint) * 3 * (* Total ));
- If (temp = NULL) Return-1;
- SP = * (SRC + num-1 );
- For (I = 0; I <num; I ++) // crop the window
- {
- Ep = * (SRC + I );
- Start = * (* DEST) + * Total-1 );
- Flag = _ rtinside (rtvector (SP, EP), start)> 0? 0: 1;
- For (j = 0; j <* Total; j ++) // The cropped polygon.
- {
- Stop = * (* DEST) + J );
- If (_ rtinside (rtvector (SP, EP), stop) <= 0) // whether the current vertex I is inside the boundary
- {
- If (flag = 0)/* is the previous point on the outside? */
- {
- Flag = 1;/* set the flag to 0 as the first sign of the next loop */
- K ++;
- Crtpoint <double> point;
- Crtpoint <int> ST (sp. X, sp. Y), ET (Ep. X, ep. y );
- Crtline <int> V1 (St, ET );
- St. setdata (start. X, start. y );
- Et. setdata (stop. X, stop. y );
- Crtline <int> V2 (St, ET );
- V2.intersect (V1, point );
- (Temp + k)-> X = point [0];/* place the intersection point into the new polygon */
- (Temp + k)-> Y = point [1];
- }
- K ++;
- * (Temp + k) = stop;/* place the current point pi into the new polygon */
- }
- Else
- {
- If (0! = Flag)/* is the previous point inside? */
- {
- Flag = 0;/* set the flag to 1 as the first sign of the next loop */
- K ++;
- Crtpoint <double> point;
- Crtpoint <int> ST (sp. X, sp. Y), ET (Ep. X, ep. y );
- Crtline <int> V1 (St, ET );
- St. setdata (start. X, start. y );
- Et. setdata (stop. X, stop. y );
- Crtline <int> V2 (St, ET );
- V2.intersect (V1, point );
- (Temp + k)-> X = point [0];/* place the intersection point into the new polygon */
- (Temp + k)-> Y = point [1];
- }
- }
- Start = stop;/* use the current vertex as the first point of the next loop */
- }
- SP = EP;
- * Total = k + 1;
- Memcpy (* DEST, temp, sizeof (rtpoint) * (* Total ));
- K =-1;
- }
- Return 0;
- }