Sutherland-Hockman algorithm (polygon cropping)

Source: Internet
Author: User

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?
  1. // Point on the side of a directed Line Segment
  2. /*
  3. Vector cross-Product Method
  4. 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:
  5. 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.
  6. 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.
  7. Set: point P (x, y), point A (XA, ya), point B (XB, Yb ),
  8. Vector AB = {(XB-XA), (Yb-Ya )},
  9. Vector ap = {(X-XA), (Y-Ya )},
  10. Then the direction of AB × AP can be determined by the following symbol:
  11. V = (XB-XA) · (Y-Ya)-(x-XA) · (Yb-Ya) (3-14)
  12. Therefore, when v ≤ 0, P is inside the boundary;
  13. When v> 0, P is outside the boundary.
  14. */
  15. Static int _ rtinside (rtvector vector, rtpoint point)
  16. {
  17. Return (vector. ep. x-vector. SP. x) * (point. y-vector. SP. y)-(vector. ep. y-vector. SP. y) * (point. x-vector. SP. X );
  18. }
  19. // The polygon point must be clockwise.
  20. Int rtprunepsh (rtpoint * SRC, int num, rtpoint ** DEST, int * total)
  21. {
  22. Int I = 0, j = 0, K =-1, flag = 0;
  23. Rtpoint start, stop; // The cropped polygon.
  24. Rtpoint sp, EP; // cropping window
  25. Rtpoint * temp = NULL;
  26. Temp = (rtpoint *) malloc (sizeof (rtpoint) * 3 * (* Total ));
  27. If (temp = NULL) Return-1;
  28. SP = * (SRC + num-1 );
  29. For (I = 0; I <num; I ++) // crop the window
  30. {
  31. Ep = * (SRC + I );
  32. Start = * (* DEST) + * Total-1 );
  33. Flag = _ rtinside (rtvector (SP, EP), start)> 0? 0: 1;
  34. For (j = 0; j <* Total; j ++) // The cropped polygon.
  35. {
  36. Stop = * (* DEST) + J );
  37. If (_ rtinside (rtvector (SP, EP), stop) <= 0) // whether the current vertex I is inside the boundary
  38. {
  39. If (flag = 0)/* is the previous point on the outside? */
  40. {
  41. Flag = 1;/* set the flag to 0 as the first sign of the next loop */
  42. K ++;
  43. Crtpoint <double> point;
  44. Crtpoint <int> ST (sp. X, sp. Y), ET (Ep. X, ep. y );
  45. Crtline <int> V1 (St, ET );
  46. St. setdata (start. X, start. y );
  47. Et. setdata (stop. X, stop. y );
  48. Crtline <int> V2 (St, ET );
  49. V2.intersect (V1, point );
  50. (Temp + k)-> X = point [0];/* place the intersection point into the new polygon */
  51. (Temp + k)-> Y = point [1];
  52. }
  53. K ++;
  54. * (Temp + k) = stop;/* place the current point pi into the new polygon */
  55. }
  56. Else
  57. {
  58. If (0! = Flag)/* is the previous point inside? */
  59. {
  60. Flag = 0;/* set the flag to 1 as the first sign of the next loop */
  61. K ++;
  62. Crtpoint <double> point;
  63. Crtpoint <int> ST (sp. X, sp. Y), ET (Ep. X, ep. y );
  64. Crtline <int> V1 (St, ET );
  65. St. setdata (start. X, start. y );
  66. Et. setdata (stop. X, stop. y );
  67. Crtline <int> V2 (St, ET );
  68. V2.intersect (V1, point );
  69. (Temp + k)-> X = point [0];/* place the intersection point into the new polygon */
  70. (Temp + k)-> Y = point [1];
  71. }
  72. }
  73. Start = stop;/* use the current vertex as the first point of the next loop */
  74. }
  75. SP = EP;
  76. * Total = k + 1;
  77. Memcpy (* DEST, temp, sizeof (rtpoint) * (* Total ));
  78. K =-1;
  79. }
  80. Return 0;
  81. }

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.