0%

Liang-Barsky Algorithm

Liang-Barsky

参数方程

两点式:

xx1yy1=x2x1y2y1

换下位置,并设其等于 t

xx1x2x1=yy1y2y1=t,t[0,1]

其中 (x1,y1) 为左下边界, (x2,y2) 为右上边界, (x,y) 位于其间。

则参数方程可表示为:

x=x1+t(x2x1)y=y1+t(y2y1)

裁剪窗口不等式

xwminx1+t(x2x1)xwmaxywminy1+t(y2y1)ywmax

( xw,yw 为边界)

不等式可表示为:

tpkqk

其中, k=1,2,3,4 分别表示左右下上四边界。

pq 的定义为:

p1=(x2x1),q1=x1xwmin()p2=(x2x1),q2=xwmaxx1()p3=(y2y1),q3=y1ywmin()p4=(y2y1),q4=ywmaxy1()

判定

条件 线的位置
pk=0 平行于裁剪边界
pk=0qk<0 完全在边界外
pk=0qk0 在边界内
pk<0 线从外到内
pk>0 线从内到外

参数 t1,t2 可以判定线的某部分位于裁剪矩形内,当:

pk<0,maximum(0,qkpk)pk>0,minimum(1,qkpk)

t1>t2, 则线完全在窗口外,可舍弃;否则,裁剪线的止点可由参数 t 决定。

Algorithm

  1. Set tmin=0,tmax=1.
  2. Calculate the values of t (t(left), t(right), t(top), t(bottom)),
    (a) If t<tmin ignore that and move to the next edge.
    (b) else separate the t values as entering or exiting values using the inner product.
    (c) If t is entering value, set tmin=t; if t is existing value, set tmax=t.
  3. If tmin<tmax, draw a line from (x1+tmin(x2x1),y1+tmin(y2y1)) to (x1+tmax(x2x1),y1+tmax(y2y1))
  4. If the line crosses over the window, (x1+tmin(x2x1),y1+tmin(y2y1)) and (x1+tmax(x2x1),y1+tmax(y2y1)) are the intersection point of line and edge.

Reference

https://www.geeksforgeeks.org/liang-barsky-algorithm/

Powered By Valine
v1.5.2