【问题标题】:Where are the standard C implementations of sutherland-hodgman polygon clipping algorithm?sutherland-hodgman 多边形裁剪算法的标准 C 实现在哪里?
【发布时间】:2011-03-07 02:27:56
【问题描述】:

我已经实现了我的 sutherland-hodgman 多边形裁剪算法版本,但我相信我的实现会更好。所以我想知道是否有标准的实现。

这是我的实现

bool shclip(stPt** verts, int *n, float left, float right, float bottom, float top)
{
 if (leftclip(verts, n, left) &&
  rightclip(verts, n, right) &&
  bottomclip(verts, n, bottom) &&
  topclip(verts, n, top))
  return true;
 else
  return false;
}

bool leftclip(stPt** verts, int *n, float left)
{
 int v1, v2;
 float x1, x2, y1, y2;
 float relx, rely;

 v1 = v2 = 0;
 while (v1 < *n) {
  x1 = ((*verts)[v1]).x; 
  x2 = ((*verts)[(v1 + 1) % *n]).x;
  if (x1 < left) {
   if (x2 > left) {
    y1 = ((*verts)[v1]).y; y2 = ((*verts)[(v1 + 1) % *n]).y;
    relx = x2 - x1; rely = y2 - y1;
    nverts1[v2].y = (left - x1) * rely / relx + y1;
    nverts1[v2].x = left;
    nverts1[v2+1].y = ((*verts)[(v1 + 1) % *n]).y; 
    nverts1[v2+1].x = ((*verts)[(v1 + 1) % *n]).x;
    v2 += 2; 
   }

  } else {
   if (x2 > left) {
    nverts1[v2].x = ((*verts)[(v1 + 1) % *n]).x; nverts1[v2].y = ((*verts)[(v1 + 1) % *n]).y;
    v2++; 
   } else {
    y1 = ((*verts)[v1]).y; y2 = ((*verts)[(v1 + 1) % *n]).y;
    relx = x2 - x1; rely = y2 - y1;
    nverts1[v2].y = (left - x1) * rely / relx + y1;
    nverts1[v2].x = left;
    v2++; 
   }
  }
  v1++;
 }

 if (v2 != 0) {
  *n = v2;
  (*verts) = nverts1;
  return true;
 } else
  return false; 
}

谢谢。

编辑
1 似乎我不懂算法,因为我的教科书没有解释清楚。我查看了原始论文,但无法弄清楚。

2 我写的代码不是可重生的。

3 我通过从一个顶点数组复制到另一个顶点数组来传递两个裁剪器之间的顶点,这是高效的。我想我可以使用链接列表或其他更好的算法数据结构。

4 我所说的“标准实现”最好是由算法设计者实现的代码,如 nicholl-lee-nicholl 或广泛使用的标准库/图形库中的实现。

【问题讨论】:

    标签: polygon clipping


    【解决方案1】:

    您会接受什么作为标准?是否需要由“知名人士”编码?或者发表在著名的书籍、文本期刊或网站上?

    有几种 C 实现可供您比较(当然,该算法已广泛发布)。你的代码对我来说看起来不错。你不喜欢它的是什么?

    我不会向你证明我可以用谷歌搜索,因为我确信你自己可以做到这一点,只是说this 看起来不错。它是佛罗里达理工学院论文的一部分,解释了算法并给出了一个看起来不错的代码清单。

    同样,如果您对自己的代码有任何特别的顾虑,您能指出来吗?我觉得很好。

    【讨论】:

      猜你喜欢
      • 2011-12-04
      • 2015-04-23
      • 2012-08-27
      • 2015-07-20
      • 2017-10-28
      • 1970-01-01
      • 1970-01-01
      • 2011-05-20
      • 2017-03-12
      相关资源
      最近更新 更多