学习凸包(一):暴力算法求解

凸包:令S是平面上的一个点集,封闭S中所有顶点的最小凸多边形,称为S的凸包。

如下图中由红线段表示的多边形就是点集Q={p0,p1,...p12}的凸包。

[img]http://dl.iteye.com/upload/attachment/0077/9553/e9f2543f-c269-3d03-b258-366920fc6df4.gif[/img]


[img]http://dl.iteye.com/upload/attachment/0077/9560/8cc3a65d-cdd2-370a-85c2-1dcd258c4057.gif[/img]


算法代码:
import java.util.*;
class Line {//线
Point p1, p2;
Line(Point p1, Point p2) {
this.p1 = p1;
this.p2 = p2;
}
}

class Point{//点
int x;
int y;
}

public class BaoLiTuBao {
List<Point> pts = null;//点集
List<Line> lines = new ArrayList<Line>();//点集pts的凸包

public void setPointList(List<Point> pts) {
this.pts = pts;
}

public List<Line> eval() {
lines.clear();
if (pts == null) { return lines; }
int n = pts.size();
int a, b, c, cc, l, r;
for (int i = 0; i < n; i++) {
for (int j = i+1; j < n; j++) {//穷举点集中任意两点组成的直线ax+by=c
a = pts.get(j).y - pts.get(i).y;//
b = pts.get(i).x - pts.get(j).x;
c = pts.get(i).x * pts.get(j).y -pts.get(i).y * pts.get(j).x;
l = r = 0;
int k;
for (k = 0; k < n; k++) {//穷举点集中的每一点
cc = a * pts.get(k).x + b * pts.get(k).y;
if (cc > c) l++;
else if (cc < c) r++;
if (l * r != 0) break;//直线两侧都有点,
}
if (k == n) {//凸包中添加一条边
lines.add(new Line(pts.get(i), pts.get(j)));
}
}
}
return lines;
}
}


未完待续。。。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值