Skip to content

Instantly share code, notes, and snippets.

@joriki
Created January 19, 2013 18:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joriki/4574077 to your computer and use it in GitHub Desktop.
Save joriki/4574077 to your computer and use it in GitHub Desktop.
Estimate the distribution of the number of points in the convex hull of five points uniformly randomly picked in a triangle, parallelogram or ellipse; see http://math.stackexchange.com/questions/282147.
public class Question282147 {
final static int TRIANGLE = 0;
final static int PARALLELOGRAM = 1;
final static int ELLIPSE = 2;
final static int shape = ELLIPSE;
final static int ntrials = 1000000;
public static void main (String [] args) {
double [] [] x = new double [5] [2];
int [] counts = new int [3];
for (int n = 0;n < ntrials;n++) {
for (int i = 0;i < 5;i++)
for (;;) {
for (int j = 0;j < 2;j++)
x [i] [j] = Math.random ();
if (shape == PARALLELOGRAM)
break;
else if (shape == TRIANGLE) {
if (x [i] [0] > x [i] [1])
break;
}
else if (shape == ELLIPSE) {
double r = 0;
for (int j = 0;j < 2;j++) {
double d = x [i] [j] - 0.5;
r += d * d;
}
if (r < 0.25)
break;
}
else
throw new Error ();
}
int count = 0;
for (int k = 1;k < 5;k++)
outer:
for (int l = 0;l < k;l++) {
boolean first = true;
boolean positive = true;
for (int m = 0;m < 5;m++)
if (m != k && m != l) {
boolean orientation = (x [m] [0] - x [k] [0]) * (x [l] [1] - x [k] [1]) - (x [m] [1] - x [k] [1]) * (x [l] [0] - x [k] [0]) > 0;
if (first) {
positive = orientation;
first = false;
}
else if (positive != orientation)
continue outer;
}
count++;
}
counts [count - 3]++;
}
for (int i = 0;i < 3;i++)
System.out.println ((i + 3) + " : " + counts [i] / (double) ntrials);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment