function [vx, vy] = make_region() %MAKE_REGION Random convex polygon = convex hull of random points in a disk. % [VX, VY] = MAKE_REGION() returns the counterclockwise vertices of a random % convex region. Anisotropic scaling makes it a bit more interesting than a % circle. The vertices are ordered CCW so the interior is to the left of each % edge (which hit_and_run relies on for its inward half-plane normals). m = 12; ang = 2 * pi * rand(m, 1); r = sqrt(rand(m, 1)); % sqrt -> uniform over the disk px = 1.4 * r .* cos(ang); py = 1.0 * r .* sin(ang); k = convhull(px, py); % boundary indices, closed (last == first) k = k(1:end-1); % drop the repeated closing vertex vx = px(k); vy = py(k); % Ensure counterclockwise. if signed_area(vx, vy) < 0 vx = vx(end:-1:1); vy = vy(end:-1:1); end end function A = signed_area(vx, vy) %SIGNED_AREA Shoelace area; positive when the vertices run counterclockwise. n = numel(vx); A = 0; for i = 1:n j = mod(i, n) + 1; A = A + (vx(i) * vy(j) - vx(j) * vy(i)); end A = A / 2; end