This section simply introduces the principle of Liang Youdong-barsky clipping algorithm, only the conclusion and no process, read http://blog.csdn.net/daisy__ben/article/details/51941608 This article, probably have a new understanding.
"
The necessary and sufficient conditions for the existence of a common part (visible part) of the p1p2 and the Blue clipping window w1w2 (between the blue lines) are x1,x2,w1,w2 for the point p1p2w1w2, respectively:
Max (min (x1,x2), Min (w1,w2)) ≤min (Max (X1,X2), Max (W1,W2))
that is, the small person in the <= right end of the so-called left end.The points on the segment meet Y=x1+u (X2-X1) = x1 + u* x. Which 0<=u<=1. U1 determines the left point of the segment within the clipping area, and U2 determines the point to the right in the cropping area. Left point yl = x1 + u1 * x, right point yr = x1 + U2 * x, and must meet 0 <= U1 <= U2 <= 1. At this point, the solution of U1 and U2 is the purpose of the Liang Youdong-barsky segment clipping algorithm, (8.18) (8.19) for the formula in the book (8.18), the equation, that is, the intersection of the line segment and 4 boundaries U value. "
R is not equal to 0, for the above four inequalities, when RK < 0 o'clock, U >= qk/rk, when rk>0 u <= qk/rk, then point P can be located within the cropping window. Similarly, if p already falls within the clipping window, you must be greater than or equal to the maximum value of the UK for all rk<0, and less than the UK minimum value corresponding to all rk>0. The code therefore has the function Glint cliptest (glfloat p, glfloat q, Glfloat * U1, Glfloat * U2). When p<0, to obtain the corresponding U, if the u>u2, then discard; if U<u2 and u>u1, then U1=u. If p>0, if the u<u1 is discarded, if u<u2, then u2=u. The incoming p,q are determined by the formula (8.19), respectively. This is calculated up to 4 times to obtain U1 and U2.
1#include <GLUT/GLUT.h>2#include <iostream>3#include"lineliangbarsk.h"4#include"Linebres.h"5 6Glint Cliptest (glfloat p, glfloat q, Glfloat * U1, Glfloat *U2)7 {8 glfloat R;9Glint returnvalue =true;Ten One if(P <0.0) A { -R = Q/p; - if(R > *U2) the { -ReturnValue =false; - } - Else + { - if(R > *U1) + { A*U1 =R; at } - } - } - Else - { - if(P >0.0) in { -R = Q/p; to if(R < *U1) + { -ReturnValue =false; the } * Else $ {Panax Notoginseng if(R < *U2) - { the*U2 =R; + } A } the } + Else - { $ if(Q <0.0) $ { -ReturnValue =false; - } the } - }Wuyi returnreturnvalue; the } - Wu voidLineclipliangbarsk (wcpt2d winmin, wcpt2d Winmax, wcpt2d p1, wcpt2d p2) - { AboutGlfloat u1 =0.0, U2 =1.0, dx = P2.getx ()-p1.getx (), DY; $ - if(Cliptest (-DX, P1.getx ()-Winmin.getx (), &U1, &U2)) - { - if(Cliptest (DX, Winmax.getx ()-P1.getx (), &U1, &U2)) A { +DY = p2.gety ()-p1.gety (); the if(Cliptest (-dy, P1.gety ()-winmin.gety (), &U1, &U2)) - { $ if(Cliptest (Dy, winmax.gety ()-p1.gety (), &U1, &U2)) the { the if(U2 <1.0) the { theP2.setcoords (P1.getx () + U2 * dx, p1.gety () + U2 *dy); - } in if(U1 >0.0) the { theP1.setcoords (P1.getx () + u1 * dx, p1.gety () + u1 *dy); About } the Linebres (Round (P1.getx ()), round (P1.gety ()), round ( p2.getx ()), round (P2.gety ())); theStd::cout <<"Liangbarsk:"<< U1 <<","<< U2 <<Std::endl; theStd::cout <<"Liangbarsk:"<< P1.getx () <<","<< p1.gety () <<","<< P2.getx () <<","<< p2.gety () <<Std::endl; + } - } the }Bayi } the}
View Code
Https://github.com/p0e0o0p0l0e0/Computer_Graphics.git
c05938b3e669c1a04f86a54a69b5e2bb3066bd4e
Reference: http://blog.csdn.net/daisy__ben/article/details/51941608
[Graphic Science] Chapter 8.7.2 Liang Youdong-barsky segment clipping algorithm