梁友栋-柏世奇算法

✍ dations ◷ 2025-12-11 09:48:46 #计算机图形学,算法

梁友栋—柏世奇算法(以梁友栋和柏世奇(英语: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();}

参见

其他裁剪算法:

相关

  • 危险素数安全素数是满足2p+1形式的一类数,在这里p也是素数。(相反地,素数p叫做索菲热尔曼素数。)开始的几个安全素数是:之所以叫它们是“安全”素数,是因为它们在加密算法中的运用:某些约数
  • 出版社出版社是指从事书籍、报纸及刊物出版,通过经营获得利益、名声或者知识积累目标的企业。它的主要工作是先付给作者一定的报酬,再把该作品通过合法的渠道进行市场推广和销售。作
  • 斯帕达宫斯帕达宫(意大利语:Palazzo Spada)是罗马市中心的一座宫殿,位于 Piazza Capo di Ferro 广场13号,非常接近法尔内塞宫。它有一个花园面向台伯河。宫殿内拥有大量的艺术收藏品,开辟
  • 葛均波葛均波(1962年11月-),山东五莲人,中国心血管病学家,同济大学副校长,复旦大学附属中山医院教授。1984年毕业于青岛医学院,1987年获山东医科大学硕士学位,1993年于德国美因茨大学获博士
  • 居廉居廉(1828年9月22日-1904年5月5日),广东省番禺县隔山乡(今广州市海珠区)人,字士刚,号古泉、隔山樵子、罗湖散人。居廉是中国近代岭南地区著名的国画画家,善画花鸟、草虫及人物,尤以写
  • DSMDSM可以指:
  • 1948年托马斯·曼特尔事件阴谋论曼特尔幽浮事件(英语:Mantell UFO incident)是早期最广为人知的不明飞行物事件之一。发生在1948年1月7日,美国空军上尉汤玛斯·曼特尔(英语:Thomas Francis Mantell Jr.)疑似于追踪
  • Ti Beta-CTi Beta-C是一种钛合金材料,最初于1960年代被 Titanium Engineers Inc.和Titanium Engineers Ltd.两家英国公司注册为商标。Ti Beta-C的化学组成为:Ti-3Al-8V-6Cr-4Zr-4Mo其主
  • 2017年中国足球冬季转会列表这是2017年中国足球冬季转会列表,中超联赛和中甲联赛的转会窗口于2017年1月1日开启,2017年2月28日关闭;中乙联赛的转会窗口于2017年3月1日开启,2017年3月15日关闭。(注:本列表按照
  • 汇智资讯汇智资讯股份有限公司 (英语:Cloudmax Inc.)位于台湾,是企业云端应用、数据中心委运管理、网站代管服务供应商。汇智资讯于1999年成立,初期以网站建置及网站托管服务为主业,主要服