梁友栋-柏世奇算法

✍ dations ◷ 2025-12-04 09:18:57 #计算机图形学,算法

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

参见

其他裁剪算法:

相关

  • 白三烯C4合酶2PNO, 2UUH, 2UUI, 3B29, 3HKK, 3LEO, 3PCV· leukotriene-C4 synthase activity · glutathione peroxidase activity · protein binding · enzyme activator activit
  • 酒渣鼻酒糟鼻(Rosacea,又称玫瑰痤疮)是一种以脸部红斑 和丘疹(有时)为特点的慢性皮肤病。酒糟鼻在所有年龄层都可能发病并且分为四种类型,其中三种患及皮肤,而第四种患及眼睛。疾病若得不
  • 温莎大学温莎大学位于温莎市的大学城,有着友好的校园环境,浓厚的学术气氛,同时兼有所有大城市的便利。温莎大学学科齐全,商业,教育,工程,人类科学与体育,法律和社会学等领域,提供本科学位,研究
  • Nasub2/subWOsub4/sub钨酸钠,化学式Na2WO4,一般为二水合物(Na2WO4·2H2O)的形式。由三氧化钨与氢氧化钠溶液反应,再经蒸发结晶、离心脱水、干燥,得到成品。无色结晶或白色斜方结晶。溶于水,水溶液呈微碱
  • 都铎王朝本条目是分类中的文章都铎王朝(英语:House of Tudor),是1485-1603年间统治英格兰王国和其属土的王朝。而该王朝首位君主亨利·都铎乃威尔士古时的德赫巴思公国(Welsh principalit
  • 劳动新闻《劳动新闻》 (朝鲜语:로동신문/勞動新聞 Rodong Sinmun)是朝鲜劳动党中央委员会机关报,劳动新闻社负责出版,是北朝鲜最主要的报章。1945年11月1日创办,当时称为《正路》(정로)。194
  • 肯·汤普逊IEEE皮奥尔奖(英语:IEEE Emanuel R. Piore Award)(1982) 图灵奖(1983) IEEE理察·卫斯里·汉明奖章(1990) 计算机先驱奖(英语:Computer Pioneer Award)(1994) 计算机历史博物馆研究员奖(199
  • 茄子(学名:),福建人称为红菜,吴越人沿用宋代叫法称为落苏,广东人称为矮瓜、茄瓜,客家人称之为吊菜,是茄科茄属一年生草本植物,热带为多年生。其结出的果实可食用,颜色多为紫色或紫黑色
  • 马克斯·克林格尔马克斯·克林格尔 (德语:Max Klinger,1857年2月18日-1920年7月5日)是德国象征主义画家和雕塑家。克林格尔出生与莱比锡,在卡尔斯鲁厄学习,他最欣赏戈雅和门采尔的蚀刻版画,所以自学
  • P/NP问题P/NP问题是一个在理论信息学中计算复杂度理论领域里至今未被解决的问题,也是克雷数学研究所七个千禧年大奖难题之一。P/NP问题中包含了复杂度类P与NP的关系。1971年史提芬·