梁友栋-柏世奇算法

✍ dations ◷ 2025-11-13 08:41:29 #计算机图形学,算法

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

参见

其他裁剪算法:

相关

  • 阴极阴极(英文:Cathode)是发生还原反应的电极,相对于阳极为其对立面。在放电的电池中,阴极为正极。正极指电源中电位(电势)较高的一端。电流的方向为从正极流出,从负极流入。但是实际上
  • 金帐汗国钦察汗国(英语:Golden Horde;蒙古语:Алтан Орд,Altan Ord)(1242年-1502年)又称金帐汗国或大帐汗国,是蒙古四大汗国之一,元朝称之为大元钦察术赤兀鲁思,对金帐汗庭称为金斡耳朵,称
  • 十四酸肉豆蔻酸(Myristic acid),又称为十四(烷)酸,是一种饱和脂肪酸。它的分子式是C13H27COOH,简写为C14H28O2。肉豆蔻酸具有和其他羧酸相似的化学性质,比如可以和氢氧化钠反应:肉豆蔻酸对
  • 德仁天皇 德仁 皇后 雅子上皇 明仁上皇后 美智子德仁(日语:徳仁/なるひと Naruhito */?,1960年2月23日-)是日本第126代天皇,即今上天皇,年号为“令和”(日语:令和/れいわ Reiwa)。其为上
  • 大隅群岛大隅群岛(日语:大隅諸島/おおすみしょとう Ōsumi-shotō)位于日本九州鹿儿岛县大隅半岛佐多岬南方,是琉球群岛北部(萨南群岛)的一个群岛,属于鹿儿岛县。大隅群岛的主要岛屿包括种
  • 第1步兵师一战二战越南战争波斯湾战争反恐战争第1步兵师(英语:1st Infantry Division)是美国陆军中是历史最悠久的师。曾立下了不少战功。由于这个单位的标志有一个很明显红色阿拉伯数字
  • 离格离格(英语:ablative case,缩写: .mw-parser-output .smallcaps-all{font-variant:small-caps;text-transform:lowercase}.mw-parser-output .smallcaps-all *{font-variant:norm
  • 快乐的上帝《快乐的上帝》(英语:)或译《先生,愿主赐给您们欢欣》、《天赐欢乐》是英国经典的圣诞颂歌,它是16世纪或以前最古老的现存颂歌之一。这首歌有不同的版本在(歌词和旋律),它们在英国的
  • 楚德湖楚德湖(爱沙尼亚语:Peipsi-Pihkva järv,俄语:Чудско-Псковское озеро,德语:Peipussee),又称佩普西湖,是欧洲最大的跨国湖泊,位于俄罗斯和爱沙尼亚边境。楚德湖面
  • KISS OF LIFE《KISS OF LIFE》,日本男歌手平井坚的第13张单曲。2001年5月11日发行。