梁友栋-柏世奇算法

✍ dations ◷ 2025-05-19 08:07:25 #计算机图形学,算法

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

参见

其他裁剪算法:

相关

  • 肝病毒科正肝去氧核糖核酸病毒属 Orthohepadnavirus 鸟类肝去氧核糖核酸病毒属 Avihepadnavirus肝病毒科(hepadnaviridae)又译肝去氧核糖核酸病毒科,DNA逆转录病毒的一类,主要感染对象为
  • 米兰公国米兰公国是1395年至1797年间于意大利北部的一个国家。公国虽然是神圣罗马帝国的一部分,但却是一个分权实体,公国先后由数个王朝统治,多数是意大利以外的势力。虽然公国的领土多
  • 三氧化碳三氧化碳(CO3)是一种不稳定的碳氧化合物。三氧化碳有多种可能的同分异构体,分子对称性分别为Cs、D3h和C2v。经过研究,C2v这种分子结构是CO3分子的基态结构。三氧化碳与碳酸根(CO3
  • B06AAA·B·C·D·G·H·QI·J·L·M·N·P·R·S·VATC代码B06(其它血液学药剂)是解剖学治疗学及化学分类系统的一个药物分组,这是由世界卫生组织药物统计方法整合中心(The WHO Coll
  • 麦当娜麦当娜·路易丝·西科尼(英语:Madonna Louise Ciccone,1958年8月16日-),是一名美国歌手、词曲作家、演员和企业家。出生于美国密歇根州贝城,她跳脱主流流行音乐歌词内容和音乐录影
  • 安徽省军区中国人民解放军安徽省军区,是中国人民解放军现属中央军委国防动员部的一个省级军区,管辖范围为安徽省。1951年12月由皖北、皖南合并成立的安徽军区,隶属华东军区。由刘飞担任首
  • 犯罪保险犯罪保险是一个赔偿因为犯罪而遭受损害的保险。许多企业购买犯罪保险,使他们能够索赔因为雇员盗窃或其它可能造成财务破产的犯罪行为。无政府资本主义主张个人也使用这个保险
  • 无轨电车系统列表无轨电车系统列表,列出世界各城市运营中及已停运的无轨电车系统。注释:1945年亦曾有示范性无轨电车在维多利亚,由英属哥伦比亚电气铁路(英语:British Columbia Electric Railway)
  • 戴春荣戴春荣(1961年2月2日-),陕西西安人,中国女性影视、戏剧(秦腔)演员。原西安市秦腔二团及陕西易俗社演员。第五届中国梅花奖得主,获奖作品为秦腔《卓文君》选段。后转入影视艺术行业发
  • 建宫敬仁亲王建宫敬仁亲王(日语平假名:たけのみやゆきひとしんのう,1877年9月23日-1878年7月26日),日本明治天皇第二皇子。母为柳原爱子,大正天皇同母兄。由于异母长兄稚瑞照彦尊出生当天即夭折