2. Sample sort client
Sample sort client
Goal. Sort any type of data.
Goal. Sort any type of data.
Ex 2. Sort strings from standard input in alphabetical order.
Ex 3. Sort the files in a given directory by filename.
public class StringSorter
{
public static void main(String[] args)
{
String[] a = In.readStrings(args[0]);
Insertion.sort(a);
for (int i = 0; i < a.length; i++)
StdOut.println(a[i]);
}
}
% java FileSorter .
Insertion.class
Insertion.java
InsertionX.class
InsertionX.java
Selection.class
Selection.java
Shell.class
Shell.java
ShellX.class
ShellX.java
import java.io.File;
public class FileSorter
{
public static void main(String[] args)
{
File directory = new File(args[0]);
File[] files = directory.listFiles();
Insertion.sort(files);
for (int i = 0; i < files.length; i++)
StdOut.println(files[i].getName());
}
}
% more words3.txt
bed bug dad yet zoo ... all bad yes
% java StringSorter words3.txt
all bad bed bug dad ... yes yet zoo
5
Callbacks
Callbacks: roadmap
client
Goal. Sort any type of data.
java.io.File without any information about the type of an item's key?
Callbacks = reference to executable code.
Client passes array of objects to sort() function.
The sort() function calls back object's compareTo() method as needed.
Implementing callbacks.
•
•
•
•
•
object implementation
import java.io.File;
public class FileSorter
{
public static void main(String[] args)
{
File directory = new File(args[0]);
File[] files = directory.listFiles();
Insertion.sort(files);
for (int i = 0; i < files.length; i++)
StdOut.println(files[i].getName());
}
}
Q. How can sort() know to compare data of type Double, String, and
•
•
6
Comparable interface (built in to Java)
Java: interfaces.
C: function pointers.
public interface Comparable<Item>
{
public int compareTo(Item that);
}
C++: class-type functors.
C#: delegates.
Python, Perl, ML, Javascript: first-class functions.
key point: no reference to File
7
public class File
implements Comparable<File>
{
...
public int compareTo(File b)
{
...
return -1;
...
return +1;
...
return 0;
}
}
sort implementation
public static void sort(Comparable[] a)
{
int N = a.length;
for (int i = 0; i < N; i++)
for (int j = i; j > 0; j--)
if (a[j].compareTo(a[j-1]) < 0)
exch(a, j, j-1);
else break;
}
8
3. Comparable API
Implementing the Comparable interface
Implement compareTo() so that v.compareTo(w)
Date data type. Simplified version of java.util.Date.
•
Returns a negative integer, zero, or positive integer
•
Throws an exception if incompatible types or either is null.
public class Date implements Comparable<Date>
{
private final int month, day, year;
if v is less than, equal to, or greater than w.
w
v
less than (return -1)
v
w
equal to (return 0)
public Date(int m, int d, int y)
{
month = m;
day
= d;
year = y;
}
v
w
greater than (return +1)
Required property. Must be a total order.
•
•
•
public int compareTo(Date that)
{
if (this.year < that.year )
if (this.year > that.year )
if (this.month < that.month)
if (this.month > that.month)
if (this.day
< that.day )
if (this.day
> that.day )
return 0;
}
Reflexive: v = v.
Antisymmetric: if v < w, then w > v; if v = w, then w = v.
Transitive: if v ≤ w and w ≤ x, then v ≤ x.
Built-in comparable types. String, Double, Integer, Date, File, ...
User-defined comparable types. Implement the Comparable interface.
only compare dates
to other dates
return
return
return
return
return
return
-1;
+1;
-1;
+1;
-1;
+1;
}
9
10
Two useful sorting abstractions
Testing
Helper functions. Refer to data through compares and exchanges.
Goal. Test if an array is sorted.
Less. Is item v less than w ?
private static boolean isSorted(Comparable[] a)
{
for (int i = 1; i < a.length; i++)
if (less(a[i], a[i-1])) return false;
return true;
}
private static boolean less(Comparable v, Comparable w)
{ return v.compareTo(w) < 0; }
Exchange. Swap item in array a[] at index i with the one at index j.
private static void exch(Comparable[] a, int i, int j)
{
Comparable swap = a[i];
a[i] = a[j];
a[j] = swap;
}
Q. If the sorting algorithm passes the test, did it correctly sort the array?
A.
11
12
4. Selection sort demo
‣
‣
‣
‣
‣
‣
rules of the game
selection sort
insertion sort
shellsort
shuffling
convex hull
13
14
Selection sort
Selection sort inner loop
Algorithm. ↑ scans from left to right.
To maintain algorithm invariants:
Invariants.
•
•
•
Entries the left of ↑ (including ↑) fixed and in ascending order.
No entry to right of ↑ is smaller than any entry to the left of ↑.
Move the pointer to the right.
i++;
in final order
•
Identify index of minimum entry on right.
int min = i;
for (int j = i+1; j < N; j++)
if (less(a[j], a[min]))
min = j;
•
in final order
↑
↑
in final order
↑
↑
↑
↑
Exchange into position.
exch(a, i, min);
in final order
15
16
5. Selection sort: Java implementation
Selection sort: mathematical analysis
Proposition. Selection sort uses (N – 1) + (N – 2) + ... + 1 + 0 ~ N 2 / 2 compares
public class Selection
{
public static void sort(Comparable[] a)
{
int N = a.length;
for (int i = 0; i < N; i++)
{
int min = i;
for (int j = i+1; j < N; j++)
if (less(a[j], a[min]))
min = j;
exch(a, i, min);
}
}
and N exchanges.
a[]
i min
0
1
2
3
4
5
6
7
8
9 10
S
R
T
E
X
A
M
P
L
E
6
4
S
A
O
O
R
R
T
T
E
E
X
X
A
S
M
M
P
P
L
L
E
E
2
3
4
5
6
10
9
7
7
8
A
A
A
A
A
E
E
E
E
E
R
E
E
E
E
T
T
L
L
L
O
O
O
M
M
X
X
X
X
O
S
S
S
S
S
M
M
M
O
X
P
P
P
P
P
L
L
T
T
T
E
R
R
R
R
7
8
9
10
8
9
A
A
A
E
E
E
E
E
E
L
L
L
M
M
M
O
O
O
P
P
P
X
R
R
S
S
S
T
T
T
R
X
X
10
10
A
E
E
L
M
O
P
R
S
T
X
A
private static boolean less(Comparable v, Comparable w)
{ /* as before */ }
O
0
1
E
E
L
M
O
P
R
S
T
entries in black
are examined to find
the minimum
X
entries in red
are a[min]
entries in gray are
in final position
Trace of selection sort (array contents just after each exchange)
private static void exch(Comparable[] a, int i, int j)
{ /* as before */ }
Running time insensitive to input. Quadratic time, even if input array is sorted.
}
Data movement is minimal. Linear number of exchanges.
17
Selection sort: animations
18
Selection sort: animations
20 random items
20 partially-sorted items
algorithm position
algorithm position
in final order
in final order
not in final order
not in final order
http://www.sorting-algorithms.com/selection-sort
http://www.sorting-algorithms.com/selection-sort
19
20
6. Selection sort: Gypsy folk dance
‣
‣
‣
‣
‣
‣
rules of the game
selection sort
insertion sort
shellsort
shuffling
convex hull
21
Insertion sort demo
22
Insertion sort
Algorithm. ↑ scans from left to right.
Invariants.
•
•
Entries to the left of ↑ (including ↑) are in ascending order.
Entries to the right of ↑ have not yet been seen.
in order
23
↑
not yet seen
24
7. Insertion sort inner loop
Insertion sort: Java implementation
To maintain algorithm invariants:
•
public class Insertion
{
public static void sort(Comparable[] a)
{
int N = a.length;
for (int i = 0; i < N; i++)
for (int j = i; j > 0; j--)
if (less(a[j], a[j-1]))
exch(a, j, j-1);
else break;
}
Move the pointer to the right.
i++;
↑
in order
•
not yet seen
Moving from right to left, exchange
a[i] with each larger entry to its left.
private static boolean less(Comparable v, Comparable w)
{ /* as before */ }
for (int j = i; j > 0; j--)
if (less(a[j], a[j-1]))
exch(a, j, j-1);
else break;
private static void exch(Comparable[] a, int i, int j)
{ /* as before */ }
}
↑ ↑ ↑↑
in order
not yet seen
25
Insertion sort: mathematical analysis
26
Insertion sort: trace
Proposition. To sort a randomly-ordered array with distinct keys,
insertion sort uses ~ ¼ N 2 compares and ~ ¼ N 2 exchanges on average.
Pf. Expect each entry to move halfway back.
a[]
i
j
0
1
2
3
4
5
6
7
8
9 10
S
O
R
T
E
X
A
M
P
L
E
1
2
0
1
O
O
S
R
R
S
T
T
E
E
X
X
A
A
M
M
P
P
L
L
E
E
3
4
5
6
7
3
0
5
0
2
O
E
E
A
A
R
O
O
E
E
S
R
R
O
M
T
S
S
R
O
E
T
T
S
R
X
X
X
T
S
A
A
A
X
T
M
M
M
M
X
P
P
P
P
P
L
L
L
L
L
E
E
E
E
E
8
9
4
2
A
A
E
E
M
L
O
M
P
O
R
P
S
R
T
S
X
T
L
X
E
E
10
2
A
E
E
L
M
O
P
R
S
T
X
A
E
E
L
M
O
P
R
S
T
X
entries in gray
do not move
entry in red
is a[j]
entries in black
moved one position
right for insertion
Trace of insertion sort (array contents just after each insertion)
27
28
8. Insertion sort: animation
Insertion sort: best and worst case
40 random items
Best case. If the array is in ascending order, insertion sort makes
N - 1 compares and 0 exchanges.
A E E L M O P R S T X
Worst case. If the array is in descending order (and no duplicates),
insertion sort makes ~ ½ N 2 compares and ~ ½ N 2 exchanges.
X T S R P O M L E E A
algorithm position
in order
not yet seen
http://www.sorting-algorithms.com/insertion-sort
29
Insertion sort: animation
30
Insertion sort: partially-sorted arrays
40 reverse-sorted items
Def. An inversion is a pair of keys that are out of order.
A E E L M O T R X P S
T-R T-P T-S R-P X-P X-S
(6 inversions)
Def. An array is partially sorted if the number of inversions is O(N).
•
•
Ex 1. A small array appended to a large sorted array.
Ex 2. An array with only a few entries out of place.
Proposition. For partially-sorted arrays, insertion sort runs in linear time.
algorithm position
Pf. Number of exchanges equals the number of inversions.
in order
not yet seen
http://www.sorting-algorithms.com/insertion-sort
number of compares = exchanges + (N-1)
31
32
9. Insertion sort: animation
Insertion sort: Romanian folk dance
40 partially-sorted items
algorithm position
in order
not yet seen
http://www.sorting-algorithms.com/insertion-sort
34
33
Shellsort overview
Idea. Move entries more than one position at a time by h-sorting the array.
an h-sorted array is h interleaved sorted subsequences
h=4
L
L
‣
‣
‣
‣
‣
‣
E
E
A
M
M
E
rules of the game
selection sort
insertion sort
shellsort
shuffling
convex hull
H
L
E
P
P
H
E
S
O
L
T
T
S
L
A
S
X
R
S
O
E
X
L
R
h = 13
P
H
E
L
L
S
O
R
T
E
X
A
M
S
L
E
Shellsort. [Shell 1959] h-sort the array for decreasing sequence of values of h.
P
S
H
input
S
H
L
E
E
E
L L
L
S
O
R
T
E
X
A
M
P
L
L
L
L
S
O
R
T
E
X
A
M
S
L
E
A
M
H
L
E
P
S
O
L
T
S
X
R
S
T
X
13-sort P
H
E
4-sort
L
E
E
1-sort
A
E
E
(8 additional files of size 1)
An h-sorted file is h interleaved sorted files
E
H
L
L
L
M
O
P
R
S
E
Shellsort trace (array contents after each pass)
35
36
10. h-sorting
Shellsort example: increments 7, 3, 1
How to h-sort an array? Insertion sort, with stride length h.
input
S
3-sorting an array
M
E
E
E
A
A
A
A
A
A
O
O
E
E
E
E
E
E
E
E
L
L
L
L
L
L
L
L
L
L
E
M
M
M
E
E
E
E
E
E
E
E
O
O
O
O
O
O
O
O
X
X
X
X
X
X
P
P
P
P
A
A
A
A
M
M
M
M
M
M
S
S
S
S
S
S
S
S
S
S
P
P
P
P
P
P
X
X
X
X
R
R
R
R
R
R
R
R
R
R
T
T
T
T
T
T
T
T
T
T
R
T
E
X
A
M
P
L
R
R
R
L
L
T
T
T
T
E
E
E
E
E
E
X
X
X
X
X
A
A
A
A
A
M
S
S
S
S
P
P
P
P
P
L
L
L
R
R
E
E
E
E
T
L
L
L
L
L
L
L
L
L
E
M
M
M
E
E
E
E
E
E
E
O
O
O
O
O
O
O
X
X
X
X
X
X
P
P
P
A
A
A
A
M
M
M
M
M
S
S
S
S
S
S
S
S
S
P
P
P
P
P
P
X
X
X
R
R
R
R
R
R
R
R
R
A
A
A
A
A
A
A
A
A
A
A
E
T
T
T
T
T
T
T
T
T
7-sort
S
M
M
M
M
O
O
O
O
O
E
E
E
E
E
E
E
E
E
E
E
L
L
L
E
E
E
E
E
E
E
E
E
E
E
L
L
L
L
L
L
L
L
O
O
O
O
O
O
M
M
M
M
M
P
P
P
P
P
P
O
O
O
O
O
M
M
M
M
M
M
P
P
P
P
P
S
S
S
S
S
S
S
S
S
R
R
X
X
X
X
X
X
X
X
X
S
S
R
R
R
R
R
R
R
R
R
X
T
T
T
T
T
T
T
T
T
T
T
X
E
L
M
O
P
R
S
T
X
3-sort
M
E
E
E
A
A
A
A
A
Why insertion sort?
•
•
1-sort
O
Big increments ⇒ small subarray.
Small increments ⇒ nearly in order. [stay tuned]
O
O
E
E
E
E
E
E
E
result
A
E
37
38
Shellsort: intuition
Shellsort: which increment sequence to use?
Proposition. A g-sorted array remains g-sorted after h-sorting it.
Powers of two. 1, 2, 4, 8, 16, 32, ...
No.
7-sort
M
M
M
M
M
O
O
O
O
O
Powers of two minus one. 1, 3, 7, 15, 31, 63, ...
3-sort
R
R
L
L
L
T
T
T
E
E
E
E
E
E
E
X
X
X
X
X
A
A
A
A
A
S
S
S
S
S
P
P
P
P
P
L
L
R
R
R
E
E
E
T
T
M
E
E
E
A
A
A
A
A
A
O
O
E
E
E
E
E
E
E
E
L
L
L
L
L
L
L
L
L
L
E
M
M
M
E
E
E
E
E
E
E
E
O
O
O
O
O
O
O
O
X
X
X
X
X
X
P
P
P
P
A
A
A
A
M
M
M
M
M
M
S
S
S
S
S
S
S
S
S
S
P
P
P
P
P
P
X
X
X
X
R
R
R
R
R
R
R
R
R
R
Maybe.
T
T
T
T
T
T
T
T
T
T
3x + 1. 1, 4, 13, 40, 121, 364, ...
OK. Easy to compute.
merging of (9 ⨉ 4i) – (9 ⨉ 2i) + 1 and 4i – (3 ⨉ 2i) + 1
Sedgewick. 1, 5, 19, 41, 109, 209, 505, 929, 2161, 3905, ...
Good. Tough to beat in empirical studies.
still 7-sorted
Interested in learning more?
•
•
Challenge. Prove this fact—it's more subtle than you'd think!
39
See Section 6.8 of Algs, 3rd edition or Volume 3 of Knuth for details.
Do a JP on the topic.
40
11. Shellsort: Java implementation
Shellsort: visual trace
public class Shell
{
public static void sort(Comparable[] a)
{
int N = a.length;
input
3x+1 increment
sequence
int h = 1;
while (h < N/3) h = 3*h + 1; // 1, 4, 13, 40, 121, 364, 1093, ...
while (h >= 1)
{ // h-sort the array.
for (int i = h; i < N; i++)
{
for (int j = i; j >= h && less(a[j], a[j-h]); j -= h)
exch(a, j, j-h);
}
40-sorted
insertion sort
13-sorted
move to next
increment
4-sorted
h = h/3;
}
}
private
{ /* as
private
{ /* as
static
before
static
before
result
boolean less(Comparable v, Comparable w)
*/ }
boolean void(Comparable[] a, int i, int j)
*/ }
}
Visual trace of shellsort
41
Shellsort: animation
42
Shellsort: animation
50 random items
50 partially-sorted items
algorithm position
algorithm position
h-sorted
h-sorted
current subsequence
http://www.sorting-algorithms.com/shell-sort
current subsequence
other elements
http://www.sorting-algorithms.com/shell-sort
43
other elements
44
12. Shellsort: analysis
Why are we interested in shellsort?
Proposition. The worst-case number of compares used by shellsort with the
3x+1 increments is
Example of simple idea leading to substantial performance gains.
O(N 3/2).
Useful in practice.
•
•
•
Property. The number of compares used by shellsort with the 3x+1 increments
is at most by a small multiple of N times the # of increments used.
N
compares
N1.289
Fast unless array size is huge.
Tiny, fixed footprint for code (used in embedded systems).
Hardware sort prototype.
2.5 N lg N
5,000
93
58
209
143
230
20,000
467
349
495
40,000
1022
855
1059
80,000
2266
2089
Simple algorithm, nontrivial performance, interesting questions.
106
10,000
2257
•
•
•
Asymptotic growth rate?
Best sequence of increments?
open problem: find a better increment sequence
Average-case performance?
measured in thousands
Lesson. Some good algorithms are still waiting discovery.
Remark. Accurate model has not yet been discovered (!)
45
46
Shellsort: Hungarian folk dance
‣
‣
‣
‣
‣
‣
47
rules of the game
selection sort
insertion sort
shellsort
shuffling
convex hull
48
13. How to shuffle an array
How to shuffle an array
Shuffling. Rearrange an array so that result is a uniformly random permutation.
Shuffling. Rearrange an array so that result is a uniformly random permutation.
49
Knuth shuffle demo
50
Knuth shuffle
Knuth shuffle. [Fisher-Yates 1938]
•
•
In iteration i, pick integer r between 0 and i uniformly at random.
Swap a[i] and a[r].
r
i
shuffled
not yet seen
Proposition. Knuth shuffling algorithm produces a uniformly random
permutation of the input array in linear time.
51
assuming integers
uniformly at random
52
14. Knuth shuffle
Knuth shuffle
Knuth shuffle. [Fisher-Yates 1938]
Knuth shuffle. [Fisher-Yates 1938]
•
•
•
•
In iteration i, pick integer r between 0 and i uniformly at random.
Swap a[i] and a[r].
r
In iteration i, pick integer r between 0 and i uniformly at random.
Swap a[i] and a[r].
common bug: between 0 and N – 1
correct variant: between i and N – 1
i
shuffled
public class StdRandom
{
...
public static void shuffle(Object[] a)
{
int N = a.length;
for (int i = 0; i < N; i++) {
int r = StdRandom.uniform(i + 1);
exch(a, i, r);
}
}
}
not yet seen
Proposition. Knuth shuffling algorithm produces a uniformly random
permutation of the input array in linear time.
between 0 and i
assuming integers
uniformly at random
53
54
Shuffle sort
Shuffle sort
Shuffle sort.
Shuffle sort.
•
•
•
•
Generate a random real number for each array entry.
Sort the array.
0.8003
0.9706
useful for shuffling
columns in a spreadsheet
0.9157
0.9649
0.1576
0.4854
0.1419
0.4218
0.9572
Generate a random real number for each array entry.
Sort the array.
0.1419
0.1576
useful for shuffling
columns in a spreadsheet
0.4218
0.4854
0.8003
0.9157
0.9572
0.9649
0.9706
Proposition. Shuffle sort produces a uniformly random permutation
Proposition. Shuffle sort produces a uniformly random permutation
of the input array, provided no duplicate values.
of the input array, provided no duplicate values.
assuming real numbers
uniformly at random
55
assuming real numbers
uniformly at random
56
15. War story (Microsoft)
War story (Microsoft)
Microsoft antitrust probe by EU. Microsoft agreed to provide a randomized
Microsoft antitrust probe by EU. Microsoft agreed to provide a randomized
ballot screen for users to select browser in Windows 7.
ballot screen for users to select browser in Windows 7.
Solution? Implement shuffle sort by making comparator always return a
random answer.
http://www.browserchoice.eu
public int compareTo(Browser that)
Microsoft's implementation in Javascript
{
double r = Math.random();
function RandomSort (a,b)
if
{ (r < 0.5) return -1;
if return (0.5 - Math.random());
(r > 0.5) return +1;
return 0;
}
}
browser comparator
(should implement a total order)
appeared last
50% of the time
57
War story (online poker)
58
War story (online poker)
Texas hold'em poker. Software must shuffle electronic cards.
Shuffling algorithm in FAQ at www.planetpoker.com
for i := 1 to 52 do begin
r := random(51) + 1;
swap := card[r];
card[r] := card[i];
card[i] := swap;
end;
between 1 and 51
Bug 1. Random number r never 52 ⇒ 52nd card can't end up in 52nd place.
Bug 2. Shuffle not uniform (should be between i and 51).
Bug 3. random() uses 32-bit seed ⇒ 232 possible shuffles.
Bug 4. Seed = milliseconds since midnight ⇒ 86.4 million possible shuffles.
How We Learned to Cheat at Online Poker: A Study in Software Security
http://itmanagement.earthweb.com/entdev/article.php/616221
“ The generation of random numbers is too important to be left to chance. ”
Exploit. After seeing 5 cards and synchronizing with server clock,
— Robert all future
can determine R. Coveyou cards in real time.
59
60
16. War story (online poker)
Best practices for shuffling (if your business depends on it).
•
•
•
Use a hardware random-number generator that has passed
•
Use an unbiased shuffling algorithm.
FIPS 140-2 and the NIST statistical test suite.
Continuously monitor statistic properties because hardware
random-number generators are fragile and fail silently.
‣
‣
‣
‣
‣
‣
rules of the game
selection sort
insertion sort
shellsort
shuffling
convex hull
Bottom line. Shuffling a deck of cards is hard!
61
62
Convex hull
Convex hull
The convex hull of a set of N points is the smallest perimeter fence
The convex hull of a set of N points is the smallest perimeter fence
enclosing the points.
enclosing the points.
vertex
on convex hull boundary,
but not vertices
Equivalent definitions.
•
•
•
Smallest convex set containing all the points.
Convex hull output. Sequence of vertices in counterclockwise order.
Smallest area convex polygon enclosing the points.
Convex polygon enclosing the points, whose vertices are points in the set.
63
64
17. Convex hull: mechanical algorithm
Convex hull application: motion planning
Mechanical algorithm. Hammer nails perpendicular to plane; stretch elastic
Robot motion planning. Find shortest path in the plane from s to t
rubber band around points.
that avoids a polygonal obstacle.
obstacle
s
t
http://www.dfanning.com/math_tips/convexhull_1.gif
Fact. Shortest path is either straight line from s to t or it is one of two
polygonal chains of convex hull.
65
66
Convex hull application: farthest pair
Convex hull: geometric properties
Farthest pair problem. Given N points in the plane, find a pair of points with
Fact. Can traverse the convex hull by making only counterclockwise turns.
the largest Euclidean distance between them.
Fact. The vertices of convex hull appear in increasing order of polar angle
with respect to point p with lowest y-coordinate.
5
1
4
2
8
7
9
6
3
10
Fact. Farthest pair of points are extreme points on convex hull.
12
11
p
67
68
18. Convex hull: Graham scan
•
•
•
Graham scan demo
Choose point p with smallest y-coordinate.
Sort points by polar angle with p.
Consider points in order, and discard unless that would create a ccw turn.
5
1
4
2
8
7
9
6
3
10
12
11
0
69
70
Graham scan: implementation challenges
Implementing ccw
Q. How to find point p with smallest y-coordinate?
CCW. Given three points a, b, and c, is a → b → c a counterclockwise turn?
A. Define a total order, comparing y-coordinate.
is c to the left of the ray a→b
Q. How to sort points by polar angle with respect to p ?
A. Define a total order for each point p. [next lecture]
c
b
b
b
a
c
a
a
b
a
yes
(∞-slope)
no
(collinear)
no
(collinear)
no
(collinear)
c
b
c
Q. How to determine whether p1 → p2 → p3 is a counterclockwise turn?
c
c
b
A. Computational geometry. [next two slides]
a
a
Q. How to sort efficiently?
yes
A. Mergesort sorts in N log N time. [next lecture]
no
Q. How to handle degeneracies (three or more points on a line)?
Lesson. Geometric primitives are tricky to implement.
A. Requires some care, but not hard.
•
•
71
Dealing with degenerate cases.
Coping with floating-point precision.
72
19. Implementing ccw
Immutable point data type
CCW. Given three points a, b, and c, is a → b → c a counterclockwise turn?
•
Determinant (or cross product) gives twice signed area of planar triangle.
2 × Area(a, b, c) =
ax
bx
cx
ay 1
by 1
cy 1
public class Point2D
{
private final double x;
private final double y;
= (bx − a x )(c y − a y ) − (by − a y )(c x − a x )
public Point(double x, double y)
{ this.x = x; this.y = y; }
(b - a) × (c - a)
public double distanceTo(Point that)
{
double dx = this.x - that.x;
double dy = this.y - that.y;
return Math.sqrt(dx*dx + dy*dy);
}
€
• If signed area > 0, then a → b → c is counterclockwise.
• If signed area < 0, then a → b → c is clockwise.
• If signed area = 0, then a → b → c are collinear.
(bx, by)
(bx, by)
public static int ccw(Point a, Point b, Point c)
{
int area2 = (b.x-a.x)*(c.y-a.y) - (b.y-a.y)*(c.x-a.x);
if
(area2 < 0) return -1; // clockwise
else if (area2 > 0) return +1; // counter-clockwise
else
return 0; // collinear
}
(bx, by)
=0
>0
danger of floating-point
roundoff error
(cx, cy)
<0
}
(cx, cy)
(ax, ay)
counterclockwise
(ax, ay)
(cx, cy)
clockwise
(ax, ay)
collinear
73
74