求最小凸多边形的算法和源代码

ondelettes 2005-03-22 11:09:57
各位高手多谢了
btw,有一种方法是不断的作膨胀腐蚀,在一定程度上可以实现目标,但是该算法效率不高
...全文
340 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
dsgsos 2005-04-30
  • 打赏
  • 举报
回复
算法在《数字高程模型》上有。
huanyun 2005-04-03
  • 打赏
  • 举报
回复
int GetConvexVertexList(XPOINT* pPtDst, XPOINT* pPtSrc, int nSrc)
{
int nDst = -1;
int i,nStart, nPre, nCur, nNext;
XPOINT ptPre;
if(pPtDst == NULL || pPtSrc == NULL || nSrc < 3) return nDst;

nStart = 0;
for (i = 1; i < nSrc; ++i)
{
if(pPtSrc[i].y < pPtSrc[nStart].y ||
(pPtSrc[i].y == pPtSrc[nStart].y
&& pPtSrc[i].x < pPtSrc[nStart].x))
{
nStart = i;
}
}
double* k = new double[nSrc];
BOOL* bUser= new BOOL[nSrc];
memset(bUser,0,nSrc*sizeof(BOOL));
double kMin=0;
nDst = 0;
pPtDst[0] = pPtSrc[nStart];
ptPre.x = pPtSrc[nStart].x - 1000;
ptPre.y = pPtSrc[nStart].y + 1;
nPre = nNext = -1;
nCur = nStart;
double ax,ay,bx,by,cx,cy;
BOOL bLine;
while(pPtSrc[nNext].x != pPtSrc[nStart].x || pPtSrc[nNext].y != pPtSrc[nStart].y)
{
bLine = FALSE;
memset(k,0,nSrc*sizeof(double));
for (i=0; i<nSrc; ++i)
{
if(bUser[i] || i == nCur || i == nPre)
{
k[i] = 0x7FFFFFFF;
}
else
{
ax = pPtSrc[nCur].x - pPtSrc[i].x;
ay = pPtSrc[nCur].y - pPtSrc[i].y;
bx = pPtSrc[nCur].x - ptPre.x;
by = pPtSrc[nCur].y - ptPre.y;
cx = pPtSrc[i].x - ptPre.x;
cy = pPtSrc[i].y - ptPre.y;
if(bx * cy == cx * by)
{
if(fabs(bx) > fabs(cx))
{
if(i != nStart) bUser[i] = TRUE;
bLine = TRUE;
break;
}
else if(fabs(bx) < fabs(cx))
{
pPtDst[nDst] = pPtSrc[i];
if(i != nStart) bUser[i] = TRUE;
bLine = TRUE;
break;
}
else if(fabs(by) < fabs(cy))
{
pPtDst[nDst] = pPtSrc[i];
if(i != nStart) bUser[i] = TRUE;
bLine = TRUE;
break;
}
k[i] = 0x7FFFFFFF;
}
else
{
ax = ax * ax; ay = ay * ay;
bx = bx * bx; by = by * by;
cx = cx * cx; cy = cy * cy;
k[i] = (ax + ay + bx + by - cx - cy)/ (2 * sqrt((ax + ay) * (bx + by)));
}
}
}

if(!bLine)
{
nNext = -1;
kMin = (double)(0x7FFFFFFF);
for (i = 0; i < nSrc; ++i)
{
if(!bUser[i] && k[i] < kMin)
{
kMin = k[i];
nNext = i;
}
}
nDst++;
pPtDst[nDst] = pPtSrc[nNext];
bUser[nNext] = TRUE;
ptPre = pPtSrc[nCur];
nPre = nCur;
nCur = nNext;
}
}
delete []k;
delete []bUser;
return nDst;
}
ondelettes 2005-03-24
  • 打赏
  • 举报
回复
一些连通区域,甚至只有一个连通区域
syy64 2005-03-24
  • 打赏
  • 举报
回复
mark
zzwu 2005-03-24
  • 打赏
  • 举报
回复
/*
* Ken Clarkson wrote this. Copyright (c) 1996 by AT&T..
* Permission to use, copy, modify, and distribute this software for any
* purpose without fee is hereby granted, provided that this entire notice
* is included in all copies of any software which is or includes a copy
* or modification of this software and in all copies of the supporting
* documentation for such software.
* THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR AT&T MAKE ANY
* REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
* OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
*/


/*
* two-dimensional convex hull
* read points from stdin,
* one point per line, as two numbers separated by whitespace
* on stdout, points on convex hull in order around hull, given
* by their numbers in input order
* the results should be "robust", and not return a wildly wrong hull,
* despite using floating point
* works in O(n log n); I think a bit faster than Graham scan;
* somewhat like Procedure 8.2 in Edelsbrunner's "Algorithms in Combinatorial
* Geometry", and very close to:
* A.M. Andrew, "Another Efficient Algorithm for Convex Hulls in Two Dimensions",
* Info. Proc. Letters 9, 216-219 (1979)
* (See also http://geometryalgorithms.com/Archive/algorithm_0110/algorithm_0110.htm)
*/


#include <stdlib.h>
#include <stdio.h>
#include <assert.h>

typedef double coord;
char input_format[] = "%lf%lf";

#define N 100000

coord points[N][2], *P[N+1]; /* an extra position is used */

int read_points(void) {
int n = 0;
char buf[100];
while (fgets(buf, sizeof(buf), stdin)) {
assert(2==sscanf(buf, input_format, &points[n][0], &points[n][1]));
P[n] = points[n];
assert(++n <= N);
}
return n;
}

void print_hull(coord **P, int m) {
int i;
for (i=0; i<m; i++)
printf("%d ", (P[i]-points[0])/2);
printf("\n");
}


int ccw(coord **P, int i, int j, int k) {
coord a = P[i][0] - P[j][0],
b = P[i][1] - P[j][1],
c = P[k][0] - P[j][0],
d = P[k][1] - P[j][1];
return a*d - b*c <= 0; /* true if points i, j, k counterclockwise */
}


#define CMPM(c,A,B) \
v = (*(coord**)A)[c] - (*(coord**)B)[c];\
if (v>0) return 1;\
if (v<0) return -1;

int cmpl(const void *a, const void *b) {
double v;
CMPM(0,a,b);
CMPM(1,b,a);
return 0;
}

int cmph(const void *a, const void *b) {return cmpl(b,a);}


int make_chain(coord** V, int n, int (*cmp)(const void*, const void*)) {
int i, j, s = 1;
coord* t;

qsort(V, n, sizeof(coord*), cmp);
for (i=2; i<n; i++) {
for (j=s; j>=1 && ccw(V, i, j, j-1); j--){}
s = j+1;
t = V[s]; V[s] = V[i]; V[i] = t;
}
return s;
}

int ch2d(coord **P, int n) {
int u = make_chain(P, n, cmpl); /* make lower hull */
if (!n) return 0;
P[n] = P[0];
return u+make_chain(P+u, n-u+1, cmph); /* make upper hull */
}


void main(int argc, char** argv) {
print_hull(P, ch2d(P, read_points()));
exit(0);
}

寻开心 2005-03-22
  • 打赏
  • 举报
回复
如果找理论,可以看《计算几何》周陪德,卢开澄
http://zh1110.nease.net/AI/PZ.htm 是简单的描述
http://dev.gameres.com/Program/Abstract/Geometry.htm#凸包的求法

海星的主页曾经有过,
http://algorithm.myrice.com/resources/code_center/algorithm/geometry_lib/geometry_lib_pas.htm
现在这个连接似乎已经失效,你可以和他联系,以前也是csdn的大斑竹呢
梧桐168 2005-03-22
  • 打赏
  • 举报
回复
求凸包吧?
zzwu 2005-03-22
  • 打赏
  • 举报
回复
求最小凸多边形的已知数据是什么?是一组平面点集吗?

4,499

社区成员

发帖
与我相关
我的任务
社区描述
图形图像/机器视觉
社区管理员
  • 机器视觉
  • 迪菲赫尔曼
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧