私は自分のバージョンの 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;
}
ありがとう。
EDIT
1教科書でアルゴリズムが明確に説明されていないため、アルゴリズムを理解していないようです。原論文を見たのですが、どうしても理解できませんでした。
2 私が書いたコードは再帰的ではありません。
3 ある頂点配列から別の頂点配列にコピーすることで、2 つのクリッパー間の頂点を渡しますが、これは効率的ではありません。linklist など、アルゴリズムのデータ構造がはるかに優れたものを使用できると思いました。
4 「標準実装」とは、ニコル-リー-ニコルのようなアルゴリズム設計者によって実装されたコード、または広く使用されている標準ライブラリ/グラフィック ライブラリの実装であることがより適切であることを意味します。