梁友栋-柏世奇算法

✍ dations ◷ 2025-02-23 20:24:02 #计算机图形学,算法

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

参见

其他裁剪算法:

相关

  • 曲安奈德曲安奈德(英语:Triamcinolone acetonide)是一种用来治疗各种皮肤病或减轻口疡带来不适的合成皮质类固醇。它是更有效的去炎松,大约是强的松的八倍。
  • 鳞柄白毒鹅膏菌蕈伞凸面鳞柄白鹅膏(学名:Amanita virosa),又称毁灭天使、招魂天使、破坏天使 (Destroying Angel),是一个隶属于伞菌目鹅膏菌科鹅膏菌属下的有毒真菌种。其为中至大型的菇菌,全体
  • 牛至牛至(学名:Origanum vulgare),又名滇香薷,欧洲语言中称为“俄勒杆叶”、“披萨草”或“野马郁兰”(意大利语:origano;英语:oregano、pizza herb、wild marjoram),是唇形科牛至属中的一
  • 杯状病毒Lagovirus Nebovirus 诺如病毒 Norovirus Sapovirus Vesivirus杯状病毒科(Caliciviridae),又称嵌杯病毒科,是巴尔的摩分类法(Baltimore classification)第四纲病毒,它们具有正股不
  • 特伦特大学坐标:44°21′27.95″N 78°17′22.42″W / 44.3577639°N 78.2895611°W / 44.3577639; -78.2895611加拿大特伦特大学(英语:Trent University)又称:川特大学,于1964年正式建校,并
  • 三氧化二镨三氧化二镨(化学式:Pr2O3),又称氧化镨(Ⅲ),是稀土元素镨的倍半氧化物。它是黄绿色固体,难溶于水和碱液,但可溶于强无机酸溶液中。固体属六方晶系,金属离子配位数为7,有6个氧原子占据八
  • 张恕琳张恕琳(1875年-1919年),字心如,号云门,山东掖县城东南隅村人。晚清进士,学者,书画家。张家自元延祐年间定居莱州以来,文脉兴盛,明末清初有张孔教、张忻、张端,祖孙三代皆为进士;清代中期
  • 亨利·赛登托夫亨利·赛登托夫(Henry Friedrich Wilhelm Siedentopf)( 1872年9月22日-1940年5月8日),德国物理学家。亨利·赛登托夫从1899年至1938年,在耶拿卡尔·蔡司公司工作。从1919年开始,他在
  • 戴衢亨戴衢亨(1755年-1811年),字荷之,号莲士,室名震无咎斋,江西大庾人,祖籍安徽休宁隆阜。清朝政治人物、状元。父戴第元,字正宇,号簋圃,官至太仆寺少卿。父、叔、兄皆进士,时称“西江四戴”。
  • 中井将贵中井将贵(日语:中井 将貴,4月27日-),日本男性配音员。出身于爱知县。以前所属的经纪公司是Sigma Seven(1999年至2007年)。※粗体字表示说明饰演的主要角色。