梁友栋-柏世奇算法

✍ dations ◷ 2025-06-27 12:43:34 #计算机图形学,算法

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

参见

其他裁剪算法:

相关

  • 专性厌氧专性厌氧菌(obligate anaerobes)是一类仅能进行无氧呼吸,且无法在正常大气(氧含量21%)等富氧环境下存活的微生物。这类生物对氧气的耐受力不同,部分专性厌氧菌可以在氧含量达8%的
  • 电生理学在神经科学,电生理学是一门研究生物细胞或组织的电学特性的科学,主要研究神经元的电学特性,尤其是动作电位包括细胞膜电势变化与跨膜电流的调节。它涉及在多种尺度上从单个离子
  • 母乳微生物群母乳微生物群(human milk microbiota)也称为母乳益生菌(human milk probiotics, HMP),是在人类母乳及乳腺中的微生物群(英语:Microbiota)。以往曾认为母乳无菌(英语:Asepsis),不过近来不
  • 詹姆斯·F·伯恩斯詹姆斯·弗朗西斯·伯恩斯(英语:James Francis Byrnes 1882年5月2日-1972年4月9日),生于南卡罗来纳州查尔斯顿,于1972年4月9日逝世,美国政治家,第49任美国国务卿和南卡罗来纳州州长
  • 自由基反应自由基(英语:Free Radical),又称游离基,是指化合物的分子在光热等外界条件下,共价键发生均裂而形成的具有不成对电子的原子或基团。在书写时,一般在原子符号或者原子团符号旁边加上
  • 桌面出版桌面出版(英语:Desktop publishing,DTP)是指在个人电脑上运用版面设计技巧来创建文档(以供印刷)。桌面出版软件可以生成布局,并生成在印刷质量上可以与传统排版和印刷相媲美的文本
  • 乔治·金雀花,第一代贝德福德公爵乔治·金雀花,第一代贝德福德公爵(George Plantagenet, 1st Duke of Bedford)(1477年3月-1479年3月) 英格兰国王爱德华四世和伊丽莎白·伍德维尔(Elizabeth Woodville)八个孩子中
  • 控制变量科学实验中的控制变量(英语:Control variable)或科学常量(英语:scientific constant)是整个研究过程中恒定和不变的实验要素。控制变量强烈影响实验结果,并且在实验过程中保持不变,
  • 单位元单位元是集合里的一种特别的元素,与该集合里的二元运算有关。当单位元和其他元素结合时,并不会改变那些元素。单位元被使用在群和其他相关概念之中。设 (
  • 娜娜奇娜娜奇(日语:ナナチ),又译奈奈祈,是土笔章人(日语:つくしあきひと)创作的奇幻题材漫画《来自深渊》中的主要角色之一,是一个有着类似兔子外形、且自称软绵绵有如布偶的角色,配音员是井