梁友栋-柏世奇算法

✍ dations ◷ 2025-11-20 10:04:09 #计算机图形学,算法

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

参见

其他裁剪算法:

相关

  • 甘露庚酮糖甘露庚酮糖(英语:Mannoheptulose)是一种庚糖,即有七个碳原子的单糖。它是己糖激酶的抑制剂,通过竞争性地与己糖激酶结合来阻断葡萄糖的磷酸化。结果是葡萄糖的降解被抑制。D-甘露
  • 米高·毕晓普约翰·米高·毕晓普(英语:John Michael Bishop,1936年2月22日-),美国免疫学家及微生物学家。1989年,他夺得了诺贝尔生理学或医学奖。目前,他则是任教于美国旧金山加利福尼亚大学。19
  • 威廉·萨克雷威廉·梅克比斯·萨克雷(William Makepeace Thackeray,1811年7月18日-1863年12月24日)是一位与狄更斯齐名的维多利亚时代的英国小说家。最著名的作品是《名利场》(一译《名利场》
  • 王磊王磊(1986年8月12日-),中国年轻篮球运动员。出生于山东济南,11岁被送到河南焦作少年体校开始接触正规篮球。2003年同易建联,唐正东,张庆鹏等人一起入选国青队,2008年,2009年两次入选
  • 阿富汗总统阿富汗总统,是现在阿富汗的国家元首兼政府首脑。阿富汗的共和制度是间断不连续的,只有在1973年至1992年间(阿富汗共和国及阿富汗民主共和国时期)和2001年后才算是共和制的国家,而
  • 沃尔特·哈尔斯坦沃尔特·哈尔斯坦(德语:Walter Hallstein,1901年-1982年3月29日)是德国学者及政治人物。欧洲经济共同体委员会第1任主席。1901年,哈尔斯坦出生于德意志帝国美因茨。29岁,成为德国最
  • 远藤周作远藤周作(日语:遠藤 周作,1923年3月27日-1996年9月29日),日本著名小说家、文学评论家和剧作家,以其独特的日本天主教徒创作视角闻名(日本人口中的基督徒比例小于1%)。与吉行淳之介、
  • 西冈晃西冈 晃(日语:西岡 晃/にしおか あきら ;1973年12月27日-),日本政治人物,无党籍。现任山口县美祢市长(1期)。山口县美祢市豊田前町出身。先后毕业于美祢市立丰田前小学校、美祢市立丰
  • 多布希纳冰洞多布希纳冰洞(斯洛伐克语:Dobšinská ľadová jaskyňa)是位于斯洛伐克城市多布希纳的一座冰洞。自2000年开始,多布希纳冰洞作为奥格泰莱克的喀斯特岩洞群的一部分被列入世界
  • 伽利略定位系统伽利略定位系统(意大利语:Galileo),是一个正在建造中的卫星定位系统,该系统由欧盟通过欧洲空间局和欧洲导航卫星系统管理局(英语:European GNSS Agency)建造,总部设在捷克共和国的布