Liang youdong-Barsky cropping Algorithm

Source: Internet
Author: User
Liang youdong-Barsky Cropping Algorithm

Cyrus and Beck use parameterized methods to propose more effective algorithms than Cohen-Sutherland. Later, Liang youdong and Barsky independently proposed a faster parameterized line segment pruning algorithm, also known as the liany-Barsky (LB) algorithm.

 

I. Liang youdong-Barsky cropping algorithm idea:

We know that a line segment whose two ends are p1 (x1, Y1) and P2 (X2, Y2) can be expressed in the form of a parameter equation:

 

 

X = X1 + U · (x2-x1) = X1 + U · △x

Y = Y1 + U · (y2-y1) = Y1 + U · △y

0 ≤ U ≤ 1

(1)

 

 

 

Formula, delta x = x2-x1, Delta y = y2-y1, parameter U in 0 ~ Value Range: 1. p (x, y) represents a point on the line segment. Its value is determined by the U parameter. According to the formula, when u is 0, the value is p1 (x1, Y1). When U is 1, the value is P2 (X2, Y2 ). If p (x, y) is located in the window determined by the coordinates (xmin, ymin) and (xmax, Ymax), the following formula is true:

 

Xmin ≤ X1 + U · △x ≤ xmax

Ymin ≤ Y1 + U · △y ≤ Ymax

(2)

 

 

 

These four inequalities can be expressed:

U · PK ≤ qk, k = 1, 2, 3, 4

(3)

 

 

P and q are defined:

 

P1 =-△x, Q1 = x1-xmin P2 = △x, q2 = xmax-x1 P3 =-△y, Q3 = y1-ymin P4 = △y, Q4 = ymax-y1

(4)

 

 

From the formula (4), we can know that for any straight line parallel to a window boundary, the PK = 0, and the K value correspond to the corresponding boundary (k = 1, 2, 3, 4 corresponds to the left, right, bottom, and upper boundary ). If the value of qk is less than 0, the line segment must be completely out of the boundary. If PK = 0 and qk is greater than or equal to 0, the line segment is parallel to a window boundary and within the window, as shown in the figure. Formula (4) also tells us:

1. When the PK is less than 0, the line segment extends from the external side of the cropping boundary extension line to the interior;

2. When the PK is greater than 0, the line segment extends from the inside of the cropping boundary extension line to the outside;

For example, when △x is greater than or equal to 0, for the left boundary P1 <0 (p1 =-△x), the line segment is from the exterior of the left boundary to the interior;

For the right boundary P2> 0 (P2 = △x), the line segment ranges from the interior of the right boundary to the exterior.

When △y is <0, for the lower boundary P3> 0 (P3 =-△y), the line segment is from the interior of the lower boundary to the exterior;

For the upper boundary P4 <0 (P4 = △y), the line segment is from the exterior of the upper boundary to the interior.

When the PK is less than 0, you can calculate the value of the parameter U, which corresponds to the intersection of the infinitely extended line and the extended window boundary K, that is:

 

U = qk/PK

(5)

 

 

For each line, the U1 and U2 parameters can be calculated. This value defines the line segment located in the window:

1. The value of U1 is determined by the rectangular boundary of the line segment from the outside to the inside (Pk <0). Calculate rk = qk/pk for these boundary, u1 is the maximum value between 0 and r values.

2. the U2 value is determined by the line segment from the inner to the rectangular boundary of the affair (PK> 0). Calculate rk = qk/pk for these boundary, u2 is the minimum value between 0 and r values.

3. If U1> u2, the line segment is completely out of the cropping window and should be discarded. Otherwise, the endpoint of the cropped line segment can be calculated by U1 and U2.

 

II. Implementation of the Barsky cropping algorithm:

1. initialize the intersection parameters of a line segment: U1 = 0, U2 = 1;

2. Calculate the p and q values of each cropping boundary;

3. Based on p and q, determine whether to discard the line segment or change the intersection parameter.

(1) When P <0, the parameter R is used to update U1; (U1 = max {u1 ,..., Rk })

(2) When P> 0, the parameter R is used to update U2. (U2 = min {U2 ,..., Rk })

(3) If U1 or U2 is updated to U1> u2, this line segment is discarded.

(4) When p = 0 and q <0, the line segment is discarded because it is parallel to the boundary and is out of the boundary. See.

4. After judging the four values of p and q, if the line segment is not discarded, the endpoint coordinates of the cropped line segment are determined by the values of the U1 and U2 parameters.

 

Iii. AlgorithmsCode

1. c ++

Bool _ rtprunelb (rtvector & Vector, rtrect rect) {rtvector DEST; bool flag = false; float U1 = 0, U2 = 1; int P [4], Q [4]; float R; P [0] = vector. SP. x-vector. ep. x; P [1] = vector. ep. x-vector. SP. x; P [2] = vector. SP. y-vector. ep. y; P [3] =-vector. SP. Y + vector. ep. y; Q [0] = vector. SP. x-rect. x; Q [1] = rect. X + rect. w-vector. SP. x; Q [2] = vector. SP. y-rect. y; Q [3] = rect. Y + rect. h-vector. SP. y; For (INT I = 0; I <4; I ++) {r = (float) Q [I]/(float) P [I]; if (P [I] <0) {u1 = max (U1, R); If (U1> u2) {flag = true ;}} if (P [I]> 0) {U2 = min (U2, R); If (U1> u2) {flag = true ;}} if (P [I] = 0 & P [I] <0) {flag = true ;}} if (FLAG) {return ;} DeST. SP. X = vector. SP. x-U1 * (vector. SP. x-vector. ep. x); DeST. SP. y = vector. SP. y-U1 * (vector. SP. y-vector. ep. y); DeST. ep. X = vector. SP. x-U2 * (vector. SP. x-vector. ep. x); DeST. ep. y = vector. SP. y-U2 * (vector. SP. y-vector. ep. y); vector = DEST ;}

 

2. Javascript

/*************************************** * ** Liang-Barsky ******************************** * ******** // ** verify whether cropping is required, calculate the slope @ return Boolean whether to cut */function cliptest (p, q) {var flag = true; var R; If (P <0.0) {r = Q/P; if (r> u2) {flag = false;} else if (r> U1) {u1 = r; flag = true ;}} else if (P> 0) {r = Q/P; If (r <U1) {flag = false;} else if (r <U2) {U2 = r; flag = true ;}} else if (q <0) {flag = false;} return flag;} var u1, U2; /** crop the line segment @ Param int wxl upper left corner X coordinate @ Param int WXR lower right corner X coordinate @ Param int wyt upper right corner y coordinate @ Param int WYB lower right corner y coordinate @ Param {x, y} start @ Param {x, y} Stop end @ return sring coordinate string, for example, '2014, 3, 5454, 2323,434 '*/function clipline (wxl, WXR, wyt, WYB, start, stop) {var dx, Dy; U1 = 0; U2 = 1; dx = stop. x-start. x; If (cliptest (-dx, start. x-wxl) {If (cliptest (dx, WXR-start. x) {DY = stop. y-start. y; If (cliptest (-dy, start. y-wyt) {If (cliptest (dy, WYB-start. y) {var arrcoords = []; arrcoords [0] = parseint (start. x) + parseint (U1 * dx); arrcoords [1] = parseint (start. y) + parseint (U1 * Dy); arrcoords [2] = parseint (start. x) + parseint (U2 * dx); arrcoords [3] = parseint (start. y) + parseint (U2 * Dy); Return arrcoords ;}}} return [];} /** crop the line according to the rectangle @ Param int wxl upper left corner X coordinate @ Param int WXR lower right corner X coordinate @ Param int wyt upper right corner y coordinate @ Param int WYB lower right corner y coordinate @ Param string coords line coordinate string (use, (separated) @ return array refers to the line segment in the rectangle, for example, ['123456', '19495', '19495, 2321, 100'] */function clippolylinebyrect (wxl, WXR, wyt, WYB, coords) {var arrcoords = coords. split (','); var cliplines = []; for (VAR I = 0; I <(arrcoords. length-2); I = I + 2) {var temp = clipline (wxl, WXR, wyt, WYB, {X: arrcoords [I], Y: arrcoords [I + 1]}, {X: arrcoords [I + 2], Y: arrcoords [I + 3]}); If (temp. length> 0) {If (cliplines. length> 0) {// first compare the last vertex with the current one. If they are the same, they belong to the same line and append the coordinates to the end, otherwise, store the new line var lastcoords = cliplines [cliplines. length-1]; var Re = new Regexp (temp [0] + ',' + temp [1] + '$', 'I'); If (Re. test (lastcoords) {cliplines [cliplines. length-1] = lastcoords + ',' + temp [2] + ',' + temp [3];} else {cliplines [cliplines. length] = temp. join (',') ;}} else {cliplines [cliplines. length] = temp. join (',') ;}}return cliplines;}/** crop the coordinate string of the Line @ Param string coords based on the current screen (separated) @ return multiple line segments after array reduction */function clippolylinebycurrentregion (coords) {var P1, P2; P1 = {X: getegisposxfromwin (0), Y: getegisposyfromwin (0 )}; p2 = {X: getegisposxfromwin (mapwidth), Y: getegisposyfromwin (mapheight)}; return clippolylinebyrect (p1.x, p2.x, p1.y, p2.y, coords );}

 

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.