梁友栋-柏世奇算法

✍ dations ◷ 2025-04-12 01:29: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();}

参见

其他裁剪算法:

相关

  • 拟菌病毒Acanthamoeba polyphaga mimivirus拟菌病毒是一个包括Acanthamoeba polyphaga mimivirus(APMV)的一个属,或许是与演化史相关的巨型病毒。通常所说的“拟菌病毒”就指APMV。在口
  • 中川融中川融(1911年3月30日-2001年8月31日),日本外交官、外交评论家,出生于日本本州新潟县,东京帝国大学法学部政治学科毕业。曾任外务省亚洲局(现亚洲大洋洲局)局长、外务省条约局(现国际
  • 云盖云盖国家森林(英语:El Yunque National Forest),前身为加勒比国家森林(英语:Caribbean National Forest),是一座位于波多黎各东北部的国家森林。它是美国国家森林中唯一一座热带雨林
  • 集体所有制企业集体企业(collective enterprises)指在社会主义社会中,部分劳动群众在一个集体的范围内,平等地共同占有生产资料和劳动成果,实行独立经营自负盈亏的一种社会主义公有制企业。在经
  • 2018年问题2018年问题(日语:2018ねんもんだい)是指日本因少子化,2018年开始造成18岁人口数持续下降,使大专院校无法招满学生和升学率无法提高,而可能产生的大量废校和合并问题。2018年问题早
  • 发酵剂发酵剂(英语:fermentation starter)是指用以生产发酵食品的细菌、微生物培养物。按发酵剂制备过程可分:
  • 吉隆坡循人中学吉隆坡循人中学(英语:Tsun Jin High School)是马来西亚吉隆坡的一所华文独立中学。循人学校于1913年4月7日由附设于雪兰莪惠州会馆之庙宇建筑物内的私塾蜕变而成为“循人初等小
  • 罗贝托·玛丽亚·奥尔蒂斯海梅·杰拉尔多·罗贝托·马塞利诺·玛丽亚·奥尔蒂斯·利萨尔迪(Jaime Gerardo Roberto Marcelino María Ortiz Lizardi,1886年9月24日-1942年7月15日),阿根廷总统(1938-1942)
  • 暖岛暖岛是中国大陆一家提供设计师产品折扣闪购的B2C购物网站,于2011年12月由美籍华人郭哲琳在北京创建,大部分销售产品均来自中国本土设计师品牌。2013年9月起,暖岛网重新调整团队
  • 发明家列表发明家列表罗列“物件发明家”和“方法发明家”两类。早期以“物件发明家”居多,随着世界性技术与商业不断融合、发展与深化,“方法发明家”有增多的趋势。