-
22-09-2019 - |
题
我想一个算法计算凸4 2D点。我已经看过的算法的广义问题,但是我不知道,如果有一个简单的解决办法为4分。
解决方案
取三个点,并确定其是否三角形是顺时针或逆时针::
triangle_ABC= (A.y-B.y)*C.x + (B.x-A.x)*C.y + (A.x*B.y-B.x*A.y)
有关右手坐标系中,该值将是正的,如果ABC是逆时针方向,负为顺时针,和零,如果它们是共线的。但是,下面将工作一样好为左手坐标系统中,作为取向是相对的。
有三个三角形含有第四点计算可比值:
triangle_ABD= (A.y-B.y)*D.x + (B.x-A.x)*D.y + (A.x*B.y-B.x*A.y)
triangle_BCD= (B.y-C.y)*D.x + (C.x-B.x)*D.y + (B.x*C.y-C.x*B.y)
triangle_CAD= (C.y-A.y)*D.x + (A.x-C.x)*D.y + (C.x*A.y-A.x*C.y)
如果所有三个{ABD,BCD,CAD}具有相同的符号如ABC,然后d是内ABC,和船体是三角形ABC
如果两个{ABD,BCD,CAD}具有相同的符号如ABC,和一个具有相反的符号,则所有四个点是极值,和船体是四边形ABCD。
如果之一{ABD,BCD,CAD}具有相同的符号如ABC,和两个具有相反的符号,则该凸包是具有相同符号的三角形;剩余的点是在它里面。
如果任何三角形的值是零,这三个点是共线的并且中间点不是极值。如果所有的四点共线,所有四个值应该为零,而船体将是A线或点。当心在这些情况下的鲁棒性数值问题!
有关那些情况下,ABC是正的:
ABC ABD BCD CAD hull
------------------------
+ + + + ABC
+ + + - ABCD
+ + - + ABDC
+ + - - ABD
+ - + + ADBC
+ - + - BCD
+ - - + CAD
+ - - - [should not happen]
其他提示
或者只是使用贾维斯行军。
这里有一个更加特设算法的特定至4点:
- 找到的指数点最小,最大的-X、最小和最大的-Y和获得的独特价值观。例如,该指数可以0,2,1,2和独特的价值将0,2,1.
- 如果有4个独特的价值,然后凸是由所有4分。
- 如果有3个独特的价值,那么这3点都是肯定在凸包。检查,如果第4点就在这个三角形;如果没有,它也是部分凸包。
- 如果有2个独特的价值,那么这些2点,是在赫尔。其他2点,点,是进一步远离这条线连接这2点是肯定在船身上。做一个三角形遏制测试,以检查,如果其他一点也是在船体。
- 如果有1个独特的价值,那么所有这4点是共同的事件。
一些计算需要,如果有4个点,以便他们正确,以避免让一个 蝴蝶结 形状。嗯。...看起来像有足够的特殊情况来辩解使用的一般化算法。但是,你可能可以为你这个运行速度快于一般化算法。
我没有概念小提琴的证明的基础上,礼品包装算法的粗版本。
在一般情况下效率不高,但足以为仅4个点。
function Point (x, y)
{
this.x = x;
this.y = y;
}
Point.prototype.equals = function (p)
{
return this.x == p.x && this.y == p.y;
};
Point.prototype.distance = function (p)
{
return Math.sqrt (Math.pow (this.x-p.x, 2)
+ Math.pow (this.y-p.y, 2));
};
function convex_hull (points)
{
function left_oriented (p1, p2, candidate)
{
var det = (p2.x - p1.x) * (candidate.y - p1.y)
- (candidate.x - p1.x) * (p2.y - p1.y);
if (det > 0) return true; // left-oriented
if (det < 0) return false; // right oriented
// select the farthest point in case of colinearity
return p1.distance (candidate) > p1.distance (p2);
}
var N = points.length;
var hull = [];
// get leftmost point
var min = 0;
for (var i = 1; i != N; i++)
{
if (points[i].y < points[min].y) min = i;
}
hull_point = points[min];
// walk the hull
do
{
hull.push(hull_point);
var end_point = points[0];
for (var i = 1; i != N; i++)
{
if ( hull_point.equals (end_point)
|| left_oriented (hull_point,
end_point,
points[i]))
{
end_point = points[i];
}
}
hull_point = end_point;
}
/*
* must compare coordinates values (and not simply objects)
* for the case of 4 co-incident points
*/
while (!end_point.equals (hull[0]));
return hull;
}
这很有趣:)
我已经写了使用查找表comingstorm的答案的快速实现。所有的四点是共线的情况下的不可以处理,因为我的应用程序并不需要它。如果点是共线的算法将第一指针点[0]至零。船体包含的3个点,如果点[3]是空指针,否则该船体具有4分。船体处于逆时针为了使坐标系,其中y轴的点到顶部和x轴向右。
const char hull4_table[] = {
1,2,3,0,1,2,3,0,1,2,4,3,1,2,3,0,1,2,3,0,1,2,4,0,1,2,3,4,1,2,4,0,1,2,4,0,
1,2,3,0,1,2,3,0,1,4,3,0,1,2,3,0,0,0,0,0,0,0,0,0,2,3,4,0,0,0,0,0,0,0,0,0,
1,4,2,3,1,4,3,0,1,4,3,0,2,3,4,0,0,0,0,0,0,0,0,0,2,3,4,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,2,4,3,0,0,0,0,0,0,0,0,0,1,2,4,0,1,3,4,0,1,2,4,0,1,2,4,0,
0,0,0,0,0,0,0,0,1,4,3,0,0,0,0,0,0,0,0,0,0,0,0,0,1,3,4,0,0,0,0,0,0,0,0,0,
1,4,2,0,1,4,2,0,1,4,3,0,1,4,2,0,0,0,0,0,0,0,0,0,2,3,4,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,2,4,3,0,0,0,0,0,0,0,0,0,2,4,3,0,1,3,4,0,1,3,4,0,1,3,2,4,
0,0,0,0,0,0,0,0,2,4,3,0,0,0,0,0,0,0,0,0,1,3,2,0,1,3,4,0,1,3,2,0,1,3,2,0,
1,4,2,0,1,4,2,0,1,4,3,2,1,4,2,0,1,3,2,0,1,3,2,0,1,3,4,2,1,3,2,0,1,3,2,0
};
struct Vec2i {
int x, y;
};
typedef long long int64;
inline int sign(int64 x) {
return (x > 0) - (x < 0);
}
inline int64 orientation(const Vec2i& a, const Vec2i& b, const Vec2i& c) {
return (int64)(b.x - a.x) * (c.y - b.y) - (b.y - a.y) * (c.x - b.x);
}
void convex_hull4(const Vec2i** points) {
const Vec2i* p[5] = {(Vec2i*)0, points[0], points[1], points[2], points[3]};
char abc = (char)1 - sign(orientation(*points[0], *points[1], *points[2]));
char abd = (char)1 - sign(orientation(*points[0], *points[1], *points[3]));
char cad = (char)1 - sign(orientation(*points[2], *points[0], *points[3]));
char bcd = (char)1 - sign(orientation(*points[1], *points[2], *points[3]));
const char* t = hull4_table + (int)4 * (bcd + 3*cad + 9*abd + 27*abc);
points[0] = p[t[0]];
points[1] = p[t[1]];
points[2] = p[t[2]];
points[3] = p[t[3]];
}
根据@comingstorm答案我创建夫特溶液:
func convexHull4(a: Pt, b: Pt, c: Pt, d: Pt) -> [LineSegment]? {
let abc = (a.y-b.y)*c.x + (b.x-a.x)*c.y + (a.x*b.y-b.x*a.y)
let abd = (a.y-b.y)*d.x + (b.x-a.x)*d.y + (a.x*b.y-b.x*a.y)
let bcd = (b.y-c.y)*d.x + (c.x-b.x)*d.y + (b.x*c.y-c.x*b.y)
let cad = (c.y-a.y)*d.x + (a.x-c.x)*d.y + (c.x*a.y-a.x*c.y)
if (abc > 0 && abd > 0 && bcd > 0 && cad > 0) ||
(abc < 0 && abd < 0 && bcd < 0 && cad < 0) {
//abc
return [
LineSegment(p1: a, p2: b),
LineSegment(p1: b, p2: c),
LineSegment(p1: c, p2: a)
]
} else if (abc > 0 && abd > 0 && bcd > 0 && cad < 0) ||
(abc < 0 && abd < 0 && bcd < 0 && cad > 0) {
//abcd
return [
LineSegment(p1: a, p2: b),
LineSegment(p1: b, p2: c),
LineSegment(p1: c, p2: d),
LineSegment(p1: d, p2: a)
]
} else if (abc > 0 && abd > 0 && bcd < 0 && cad > 0) ||
(abc < 0 && abd < 0 && bcd > 0 && cad < 0) {
//abdc
return [
LineSegment(p1: a, p2: b),
LineSegment(p1: b, p2: d),
LineSegment(p1: d, p2: c),
LineSegment(p1: c, p2: a)
]
} else if (abc > 0 && abd < 0 && bcd > 0 && cad > 0) ||
(abc < 0 && abd > 0 && bcd < 0 && cad < 0) {
//acbd
return [
LineSegment(p1: a, p2: c),
LineSegment(p1: c, p2: b),
LineSegment(p1: b, p2: d),
LineSegment(p1: d, p2: a)
]
} else if (abc > 0 && abd > 0 && bcd < 0 && cad < 0) ||
(abc < 0 && abd < 0 && bcd > 0 && cad > 0) {
//abd
return [
LineSegment(p1: a, p2: b),
LineSegment(p1: b, p2: d),
LineSegment(p1: d, p2: a)
]
} else if (abc > 0 && abd < 0 && bcd > 0 && cad < 0) ||
(abc < 0 && abd > 0 && bcd < 0 && cad > 0) {
//bcd
return [
LineSegment(p1: b, p2: c),
LineSegment(p1: c, p2: d),
LineSegment(p1: d, p2: b)
]
} else if (abc > 0 && abd < 0 && bcd < 0 && cad > 0) ||
(abc < 0 && abd > 0 && bcd > 0 && cad < 0) {
//cad
return [
LineSegment(p1: c, p2: a),
LineSegment(p1: a, p2: d),
LineSegment(p1: d, p2: c)
]
}
return nil
}
根据comingstorm的溶液我已经创建,其处理退化例C#溶液(例如,4点形成线或点)。
https://gist.github.com/miyu/6e32e993d93d932c419f1f46020e23f0
public static IntVector2[] ConvexHull3(IntVector2 a, IntVector2 b, IntVector2 c) {
var abc = Clockness(a, b, c);
if (abc == Clk.Neither) {
var (s, t) = FindCollinearBounds(a, b, c);
return s == t ? new[] { s } : new[] { s, t };
}
if (abc == Clk.Clockwise) {
return new[] { c, b, a };
}
return new[] { a, b, c };
}
public static (IntVector2, IntVector2) FindCollinearBounds(IntVector2 a, IntVector2 b, IntVector2 c) {
var ab = a.To(b).SquaredNorm2();
var ac = a.To(c).SquaredNorm2();
var bc = b.To(c).SquaredNorm2();
if (ab > ac) {
return ab > bc ? (a, b) : (b, c);
} else {
return ac > bc ? (a, c) : (b, c);
}
}
// See https://stackoverflow.com/questions/2122305/convex-hull-of-4-points
public static IntVector2[] ConvexHull4(IntVector2 a, IntVector2 b, IntVector2 c, IntVector2 d) {
var abc = Clockness(a, b, c);
if (abc == Clk.Neither) {
var (s, t) = FindCollinearBounds(a, b, c);
return ConvexHull3(s, t, d);
}
// make abc ccw
if (abc == Clk.Clockwise) (a, c) = (c, a);
var abd = Clockness(a, b, d);
var bcd = Clockness(b, c, d);
var cad = Clockness(c, a, d);
if (abd == Clk.Neither) {
var (s, t) = FindCollinearBounds(a, b, d);
return ConvexHull3(s, t, c);
}
if (bcd == Clk.Neither) {
var (s, t) = FindCollinearBounds(b, c, d);
return ConvexHull3(s, t, a);
}
if (cad == Clk.Neither) {
var (s, t) = FindCollinearBounds(c, a, d);
return ConvexHull3(s, t, b);
}
if (abd == Clk.CounterClockwise) {
if (bcd == Clk.CounterClockwise && cad == Clk.CounterClockwise) return new[] { a, b, c };
if (bcd == Clk.CounterClockwise && cad == Clk.Clockwise) return new[] { a, b, c, d };
if (bcd == Clk.Clockwise && cad == Clk.CounterClockwise) return new[] { a, b, d, c };
if (bcd == Clk.Clockwise && cad == Clk.Clockwise) return new[] { a, b, d };
throw new InvalidStateException();
} else {
if (bcd == Clk.CounterClockwise && cad == Clk.CounterClockwise) return new[] { a, d, b, c };
if (bcd == Clk.CounterClockwise && cad == Clk.Clockwise) return new[] { d, b, c };
if (bcd == Clk.Clockwise && cad == Clk.CounterClockwise) return new[] { a, d, c };
// 4th state impossible
throw new InvalidStateException();
}
}
您需要实现这个样板为您的载体类型:
// relative to screen coordinates, so top left origin, x+ right, y+ down.
// clockwise goes from origin to x+ to x+/y+ to y+ to origin, like clockwise if
// you were to stare at a clock on your screen
//
// That is, if you draw an angle between 3 points on your screen, the clockness of that
// direction is the clockness this would return.
public enum Clockness {
Clockwise = -1,
Neither = 0,
CounterClockwise = 1
}
public static Clockness Clockness(IntVector2 a, IntVector2 b, IntVector2 c) => Clockness(b - a, b - c);
public static Clockness Clockness(IntVector2 ba, IntVector2 bc) => Clockness(ba.X, ba.Y, bc.X, bc.Y);
public static Clockness Clockness(cInt ax, cInt ay, cInt bx, cInt by, cInt cx, cInt cy) => Clockness(bx - ax, by - ay, bx - cx, by - cy);
public static Clockness Clockness(cInt bax, cInt bay, cInt bcx, cInt bcy) => (Clockness)Math.Sign(Cross(bax, bay, bcx, bcy));
这里是问题,高效的完整的分析 红宝石代码(最小化比较的数量)
# positions for d:
#
# abc > 0 abc < 0
# (+-+- doesn't exist) (-+-+ doesn't exist)
#
#
# | / ---+ \ --++ | -+++
# | / bdc \ acbd | acd
# | +-++ / \ |
# | abd / ---------A--------B---------
# | / \ --+- |
# | / \ acb |
# | / \ |
# | / \ |
# |/ ---- \ | -++-
# C adcb \ | acdb
# /| \ |
# / | \|
# ++++ / | C
# abcd / | |\
# / | +--+ | \
# / | abdc | \
# / ++-+ | | \
# / abc | | \
# ---------A--------B--------- | \
# +++- / | | \
# bcd / ++-- | +--- | -+-- \
# / adbc | adc | adb \
#
# or as table
#
# ++++ abcd -+++ acd
# +++- bcd -++- acdb
# ++-+ abc -+-+ XXXX
# ++-- adbc -+-- adb
# +-++ abd --++ acbd
# +-+- XXXX --+- acb
# +--+ abdc ---+ bdc
# +--- adc ---- adcb
#
# if there are some collinear points, the hull will be nil (for the moment)
#
def point_point_point_orientation(p, q, r)
(q.x - p.x) * (r.y - q.y) - (q.y - p.y) * (r.x - q.x)
end
def convex_hull_4_points(a, b, c, d)
abc = point_point_point_orientation(a, b, c)
if abc.zero?
# todo
return nil
end
bcd = point_point_point_orientation(b, c, d)
if bcd.zero?
# todo
return nil
end
cda = point_point_point_orientation(c, d, a)
if cda.zero?
# todo
return nil
end
dab = point_point_point_orientation(d, a, b)
if dab.zero?
# todo
return nil
end
if abc.positive?
if bcd.positive?
if cda.positive?
if dab.positive?
[a, b, c, d] # ++++
else
[b, c, d] # +++-
end
else
if dab.positive?
[a, b, c] # ++-+
else
[a, d, b, c] # ++--
end
end
else
if cda.positive?
if dab.positive?
[a, b, d] # +-++
else
raise # +-+-
end
else
if dab.positive?
[a, b, d, c] # +--+
else
[a, d, c] # +---
end
end
end
else
if bcd.positive?
if cda.positive?
if dab.positive?
[a, c, d] # -+++
else
[a, c, d, b] # -++-
end
else
if dab.positive?
raise # -+-+
else
[a, d, b] # -+--
end
end
else
if cda.positive?
if dab.positive?
[a, c, b, d] # --++
else
[a, c, b] # --+-
end
else
if dab.positive?
[b, d, c] # ---+
else
[a, d, c, b] # ----
end
end
end
end
end