9fbdc3eInteractive hit-and-run sampling of a 2D convex regionJeremy Magland 1function [vx, vy] = make_region()
2%MAKE_REGION Random convex polygon = convex hull of random points in a disk.
3% [VX, VY] = MAKE_REGION() returns the counterclockwise vertices of a random
4% convex region. Anisotropic scaling makes it a bit more interesting than a
5% circle. The vertices are ordered CCW so the interior is to the left of each
6% edge (which hit_and_run relies on for its inward half-plane normals).
7m = 12;
8ang = 2 * pi * rand(m, 1);
9r = sqrt(rand(m, 1)); % sqrt -> uniform over the disk
10px = 1.4 * r .* cos(ang);
11py = 1.0 * r .* sin(ang);
12k = convhull(px, py); % boundary indices, closed (last == first)
13k = k(1:end-1); % drop the repeated closing vertex
14vx = px(k);
15vy = py(k);
16% Ensure counterclockwise.
17if signed_area(vx, vy) < 0
18 vx = vx(end:-1:1);
19 vy = vy(end:-1:1);
20end
21end
23function A = signed_area(vx, vy)
24%SIGNED_AREA Shoelace area; positive when the vertices run counterclockwise.
25n = numel(vx);
26A = 0;
27for i = 1:n
28 j = mod(i, n) + 1;
29 A = A + (vx(i) * vy(j) - vx(j) * vy(i));
30end
31A = A / 2;
32end