Implementing the same with scipy.spatial
's Delaunay
and computing barycentric coordinates using the transformation object, modifying @fmw42's implementation. For all the simplices (when number of simplices are small it will be fast) we can find the points inside a simplex, compute corresponding barycentric coordinate and the corresponding RGB value, as shown in the code below, along with the output obtained.
from scipy.spatial import Delaunay
import numpy as np
from time import time
t = time()
a, b, c = [250, 100], [100, 400], [400, 400]
tri = Delaunay(np.array([a, b, c]))
# bounding box of the triangle
xleft, xright = min(a[0], b[0], c[0]), max(a[0], b[0], c[0])
ytop, ybottom = min(a[1], b[1], c[1]), max(a[1], b[1], c[1])
xv, yv = np.meshgrid(range(xleft, xright), range(ytop, ybottom))
xv, yv = xv.flatten(), yv.flatten()
pp = np.vstack((xv, yv)).T
ss = tri.find_simplex(pp)
ndim = tri.transform.shape[2]
print(len(np.unique(ss)))
# 2
out = np.zeros((450,450,3), dtype=np.uint8)
for i in np.unique(ss): # for all simplices (triangles)
p = pp[ss == i] # all points in the simplex
# compute the barycentric coordinates of the points
b = tri.transform[i, :ndim].dot(np.transpose(p) - tri.transform[i, ndim].reshape(-1,1))
αβγ = np.c_[np.transpose(b), 1 - b.sum(axis=0)]
indices = np.where(np.all(αβγ>=0, axis=1))
out[p[indices,0], p[indices,1]] = αβγ[indices]@np.array([[0,0,255], [0,255,0], [255,0,0]])
print(f'time: {time() - t} sec')
# time: 0.03899240493774414 sec
plt.imshow(out)