Processing math: 100%

Monday, October 25, 2021

Centered triangles

Suppose you have an equilateral triangle. You pick three random points, one along each of its three edges, uniformly along the length of each edge — that is, each point along each edge has the same probability of being selected.

With those three randomly selected points, you can form a new triangle inside the original one. What is the probability that the center of the larger triangle also lies inside the smaller one?

Let's assume that the original triangle is the equilateral triangle with side lengths whose center is at the origin of the xy-plane. Thus the vertices of the original triangle are at A(1/2,3/4), B(0,3/4), and C(1/2,3/4). Let's say that t,u,vU(0,1) are i.i.d. uniformly distributed, then the random points along the edges of \Delta ABC can be modeled as X=(t12,34)Y=(1u2,3(2u1)4)Z=(1v2,3(2v1)4).

If we wanted to just brute force our way into the solution, we could just use the test that O=(0,0)ΔXYZ if and only if AΔOXY+AΔOXZ+AΔOYZ=AΔXYZ. However, we can also create the appropriate integral to calculate the theoretical answer. In this case, we will actually do both to make sure both are roughly on the right track.

Let's first focus on the siutation when t[0,12], or equivalently when XR×{34}. In this case, we can drawn a line from X through the origin O and see that if O is in the ΔXYZ then we must have Z=(z1,z2) located such that 34z1+(t12)z20. Therefore, in terms of t and v, we have 0341v2+(t12)3(2v1)4=38(1v+(2t1)(2v1))=38((4t3)v+2(1t)), or equivalently v2(1t)34t. See the figure below for context.

Similarly, we can draw a line from Y through the origin O and see that if O is in the triangle ΔXYZ then we must have Z=(z1,z2) located such that 34(2u1)z1+1u2z20. Therefore, in terms of u and v, we have 03(2u1)41v2+1u23(2v1)4=38((2u1)(1v)+(1u)(2v1))=38((3u2)+(34u)v), or equivalently v23u34u. Since we naturally have v[0,1], we see that if t[0,12] and u[0,1] that O is in the triangle ΔXYZ if and only if v[023u34u,2(1t)34t]. So P{OΔXYZ|XR×{34}}=120102(1t)34tmax{0,23u34u}dudt=120(2(1t)34t23023u34udu)dt. Using partial fractions, we get 23023u34udu=2303414(34u)du=[34u+116ln|34u|]230=1218ln3. So, again by applying the partial fractions technique we get that the probability is P{OΔXYZ|XR×{34}}=1202(1t)34t12+18ln3dt=12012+12(34t)12+18ln3dt=[18ln|34t|+t8ln3]120=3ln316.

From symmetry (by switching the restrictions of Y and Z whenever t[12,1]), we see that P{OΔXYZ|XR+×{34}}=P{OΔXYZ|XR×{34}}=3ln316, so we are left with the probability that the origin is in the randomly selected triangle ΔXYZ as P{OΔXYZ}=P{OΔXYZ|XR×{34}}+P{OΔXYZ|XR+×{34}}=3ln380.41197960825.....

As indicated, we can also use brute force to arrive at / verify this conclusion using the short python script shown below.

CenteredTriangles
In [1]:
import numpy as np
from numpy.random import uniform

def area_triangle(A, B, C):
    cross = (B[0] - A[0]) * (C[1] - A[1]) - (C[0] - A[0]) * (B[1] - A[1])
    return 0.5 * np.abs(cross)

num_samples = 1000
num_runs = 1000
O = np.array([0.,0.])
success = np.zeros(shape=(num_samples, num_runs))
for k in range(num_runs):
    U = uniform(size=(num_samples, 3))
    for i in range(num_samples):
        u = U[i,:]
        X = np.array([-0.5, -0.25*np.sqrt(3)]) + u[0] * np.array([ 1.0, 0.0])
        Y = np.array([-0.5, -0.25*np.sqrt(3)]) + u[1] * np.array([ 0.5, 0.5 * np.sqrt(3)])
        Z = np.array([ 0.5, -0.25*np.sqrt(3)]) + u[2] * np.array([-0.5, 0.5 * np.sqrt(3)])
        a1 = area_triangle(O, X, Y)
        a2 = area_triangle(O, X, Z)
        a3 = area_triangle(O, Y, Z)
        a = area_triangle(X, Y, Z)
        if np.abs(a - a1 - a2 - a3) < 1e-6:
            success[i,k] += 1
print('Over %d successive tests, each using %d samples, the probability of having the origin within ' % (num_runs, num_samples))
print('the random triangle is %f with SEM of %f.' % (success.mean(), success.mean(axis=0).std()/np.sqrt(num_runs)))
print('This compares favorably to the theoretical answer 3 * ln(3) / 8 = %f' %  (3. * np.log(3) / 8.))
Over 1000 successive tests, each using 1000 samples, the probability of having the origin within 
the random triangle is 0.411384 with SEM of 0.000495.
This compares favorably to the theoretical answer 3 * ln(3) / 8 = 0.411980