function convexity ()

  % Convex octagon.
  t1 = (1/16:1/8:1)' * 2*pi;
  x1 = sin (t1) - 0.8;
  y1 = cos (t1);
  vert = [x1, y1];
  isconvex (vert)

  % Change direction of points.
  isconvex (flipud (vert))

  % Non-convex shape.
  vert = [[1 2 1.2 1]', [1 1 1.2 2]'];
  isconvex (vert)

endfunction

function retval = isconvex (vert)

  % Create all contiguous triples of points.
  p1 = vert (1:end,:);
  p2 = [vert(2:end,:); vert(1,:)];
  p3 = [vert(3:end,:); vert(1:2,:)];

  % Subtract middle point to create two vectors.
  v1 = p1 - p2;
  v2 = p3 - p2;

  % Project one vector onto 90 degree rotation of the other vector.
  s = v1(:,2) .* v2(:,1) - v1(:,1) .* v2(:,2)

  % Test if all angles are in the same direction.
  retval = ( all (s <= 0) || all (s >= 0) );

endfunction
