梁友栋-柏世奇算法

✍ dations ◷ 2025-07-15 05:36:17 #计算机图形学,算法

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

参见

其他裁剪算法:

相关

  • 成功大学国立成功大学校务基金 20,518,898,000元国立成功大学(英语:National Cheng Kung University,缩写为NCKU或 National Koxinga University),简称成大、成功大学,是一所位于台湾台南
  • 背面月球背面是月球永远背对地球的那一面。月球背面的第一张影像由前苏联的月球3号太空船在1959年拍摄,而人类直到1968年的阿波罗8号任务环绕月球时,才直接用眼睛看见月球背面。月
  • 诺曼底大区诺曼底(法语:Normandie,发音:.mw-parser-output .IPA{font-family:"Charis SIL","Doulos SIL","Linux Libertine","Segoe UI","Lucida Sans Unicode","Code2000","Gentium","Gen
  • 日本人权 政治主题日本在政体上是君主立宪制国家。根据法务省的数据,法务局办公室和公民自由志愿者在2003年一共处理了359971项与人权相关的投诉和18786项涉嫌侵犯人权的报告。这些案
  • 2011年美国羽毛球黄金大奖赛2011年美国羽毛球黄金大奖赛为第2011年度的美国羽毛球公开赛,是2011年世界羽联大奖赛的其中一站。本届赛事于2011年7月11日-7月16日在美国加利福尼亚州的橙县举行,并获得YONEX
  • 00年代00年代通常可以指:
  • 大明历大明历可能指:
  • 安达拉帕区坐标:13°42′S 73°24′W / 13.700°S 73.400°W / -13.700; -73.400安达拉帕区(西班牙语:Distrito de Andarapa),是秘鲁的一个区,位于该国南部阿普里马克大区的安达韦拉斯省,始建
  • 字节复制字节复制是指将映像文件按照原先的字节顺序复制到存储器中。映像文件是按照存储器原始的字节顺序和特定格式所生成的文件,用于长期保存某一状态下的存储器,同时也便于共享和传
  • 穹丘构造地质学中的穹丘(Dome)是一种由对称的背斜组成的变形地质构造,在地质图上一般以圆形或卵形表示。这种构造的地层在中间是向上的,如果穹丘的顶部是被侵蚀的,结果就是一系列同心