梁友栋-柏世奇算法

✍ dations ◷ 2025-10-30 09:18:48 #计算机图形学,算法

梁友栋—柏世奇算法(以梁友栋和柏世奇(英语:Brian A. Barsky)的名字命名)是计算机图形学中的一个线段裁剪算法。梁友栋—柏世奇算法使用直线的参数方程和不等式组来描述线段和裁剪窗口的交集。求解出的交集将被用于获知线的哪些部分是应当绘制在屏幕上的。这一算法比科恩-苏泽兰算法(Cohen-Sutherland algorithm)要更加高效,梁友栋—柏世奇算法的基本思想是:在计算线段与裁剪窗交集之前做尽可能多的判断。

考虑直线的参数方程:

点在裁剪窗内,若

其可用4个不等式表达:

其中

计算最终线段:

// Liang--Barsky line-clipping algorithm#include<iostream>#include<graphics.h>#include<math.h>using namespace std;// this function gives the maximumfloat maxi(float arr,int n) {  float m = 0;  for (int i = 0; i < n; ++i)    if (m < arr)      m = arr;  return m;}// this function gives the minimumfloat mini(float arr, int n) {  float m = 1;  for (int i = 0; i < n; ++i)    if (m > arr)      m = arr;  return m;}void liang_barsky_clipper(float xmin, float ymin, float xmax, float ymax,                          float x1, float y1, float x2, float y2) {  // defining variables  float p1 = -(x2 - x1);  float p2 = -p1;  float p3 = -(y2 - y1);  float p4 = -p3;  float q1 = x1 - xmin;  float q2 = xmax - x1;  float q3 = y1 - ymin;  float q4 = ymax - y1;  float posarr, negarr;  int posind = 1, negind = 1;  posarr = 1;  negarr = 0;  rectangle(xmin, 467 - ymin, xmax, 467 - ymax); // drawing the clipping window  if ((p1 == 0 && q1 < 0) || (p3 == 0 && q3 < 0)) {      outtextxy(80, 80, "Line is parallel to clipping window!");      return;  }  if (p1 != 0) {    float r1 = q1 / p1;    float r2 = q2 / p2;    if (p1 < 0) {      negarr = r1; // for negative p1, add it to negative array      posarr = r2; // and add p2 to positive array    } else {      negarr = r2;      posarr = r1;    }  }  if (p3 != 0) {    float r3 = q3 / p3;    float r4 = q4 / p4;    if (p3 < 0) {      negarr = r3;      posarr = r4;    } else {      negarr = r4;      posarr = r3;    }  }  float xn1, yn1, xn2, yn2;  float rn1, rn2;  rn1 = maxi(negarr, negind); // maximum of negative array  rn2 = mini(posarr, posind); // minimum of positive array  if (rn1 > rn2)  { // reject    outtextxy(80, 80, "Line is outside the clipping window!");    return;  }  xn1 = x1 + p2 * rn1;  yn1 = y1 + p4 * rn1; // computing new points  xn2 = x1 + p2 * rn2;  yn2 = y1 + p4 * rn2;  setcolor(CYAN);  line(xn1, 467 - yn1, xn2, 467 - yn2); // the drawing the new line  setlinestyle(1, 1, 0);  line(x1, 467 - y1, xn1, 467 - yn1);  line(x2, 467 - y2, xn2, 467 - yn2);}int main() {  cout << "\nLiang-barsky line clipping";  cout << "\nThe system window outlay is: (0,0) at bottom left and (631, 467) at top right";  cout << "\nEnter the co-ordinates of the window(wxmin, wxmax, wymin, wymax):";  float xmin, xmax, ymin, ymax;  cin >> xmin >> ymin >> xmax >> ymax;  cout << "\nEnter the end points of the line (x1, y1) and (x2, y2):";  float x1, y1, x2, y2;  cin >> x1 >> y1 >> x2 >> y2;  int gd = DETECT, gm;  // using the winbgim library for C++, initializing the graphics mode  initgraph(&gd, &gm, "");  liang_barsky_clipper(xmin, ymin, xmax, ymax, x1, y1, x2, y2);  getch();  closegraph();}

参见

其他裁剪算法:

相关

  • 纳博特囊肿纳博特囊肿(nabothian cyst, nabothian follicle),也称纳囊、宫颈腺囊肿、宫颈腺体囊肿、子宫颈囊肿,是一类位于宫颈表面的囊肿。当外宫颈复层鳞状上皮生长覆盖过单层柱状上皮上
  • 氟代脱氧葡萄糖氟代脱氧葡萄糖是2-脱氧葡萄糖的氟代衍生物。其完整的化学名称为2-氟-2-脱氧-D-葡萄糖,通常简称为18F-FDG或FDG。FDG最常用于正电子发射断层扫描(PET)类的医学成像设备:FDG分子
  • 克尔科诺谢山克尔科诺谢山(捷克语:Krkonoše、波兰语:Karkonosze、德语:Riesengebirge)是一座位于捷克共和国北部、波兰南部的山脉,属于苏台德山脉。其正脊为捷克和波兰的边境。最高峰斯涅日卡
  • 龙目海峡龙目海峡(Lombok Strait),位于印度尼西亚小巽他群岛的西努沙登加拉省,是一个位处龙目岛和巴厘岛之间的海峡,北接爪哇海,南接印度洋。吉利群岛位于海峡靠近龙目岛的一方,在龙目岛的
  • 血细胞压积血细胞比容(德语:Hämatokrit,英语:hematocrit 源于希腊语:αιματοκρίτης,简写 HCT / Ht)又称血比容、红血球容积比、血容比,旧名红细胞压积(packed cell volume,简写PCV)指
  • 一氧化氯一氧化氯,为氧氯化合物,化学式为ClO。臭氧层破洞与它有关。在臭氧层中,氯原子与臭氧分子发生反应,产生氧分子与一氧化氯。
  • 有轨电车与轻轨系统列表有轨电车与轻轨系统列表,列出世界各城市运营中及已休业的有轨电车与轻轨系统。为方便起见,所有位于俄罗斯境内的系统,包括亚洲内的系统,都列于此条目的#欧洲部分。为方便起见,所
  • 田口祯则田口祯则(1965年9月14日-),前日本足球运动员。
  • 松永正敏松永 正敏(日语:まつなが まさとし、嘉永4年6月1日(1851年6月29日)- 明治45年(1912年)2月18日)为日本陆军军人。最终阶级陆军中将。男爵。幼名・山平。熊本藩士、郡吏・松永和吉的二
  • 克拉斯拉瓦县克拉斯拉瓦县 (Krāslavas rajons、俄语:Краславский район)是拉脱维亚东南部的一个县。面积2,285平方公里。人口36,836人。县治克拉斯拉瓦斯。下分2市1镇23村