-
Notifications
You must be signed in to change notification settings - Fork 1
/
qsortv.c
86 lines (72 loc) · 1.88 KB
/
qsortv.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
* Copyright (c) 2011 Jesper Öqvist <jesper@llbit.se>
*
* This file is part of cTTF.
*
* cTTF is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* cTTF is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with cTTF; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "qsortv.h"
/* Quicksort utility function
*/
static void qsort_swap(vertex_t** a, unsigned x, unsigned y)
{
if (x != y) {
vertex_t* z = a[x];
a[x] = a[y];
a[y] = z;
}
}
/* Main vertex Quicksort function.
* A vertex p is below a vertex q if
* py < qy or py == qy and px > qx
* The vertices are sorted in order of
* decreasing height.
*/
static void qsort_work(vertex_t** a, unsigned start, unsigned end)
{
unsigned pivot;
unsigned i;
vector_t pv;
unsigned store;
if (start >= end)
return;
else if (start == end - 1) {
if (!vec_above(a[start]->vec, a[end]->vec))
qsort_swap(a, start, end);
return;
}
// select pivot
pivot = (start + end) / 2;
pv = a[pivot]->vec;
qsort_swap(a, pivot, end);
store = start;
for (i = start; i < end; ++i) {
if (vec_above(a[i]->vec, pv)) {
qsort_swap(a, i, store);
store++;
}
}
qsort_swap(a, store, end);
if (start < store)
qsort_work(a, start, store-1);
if (store < end)
qsort_work(a, store+1, end);
}
/* Vertex Quicksort for polygon triangulation
*/
void qsort_verts(vertex_t** verts, unsigned n)
{
qsort_work(verts, 0, n-1);
}