I am trying to interpolate data based on an input forming a trapezoid. I found this solution by Nico Schertler, which outlines how to interpolate for any four non-aligned input points. However, I am unable to get it to generalize to trapezoids.
The interpolated value is found through:
interpolated value = (1 - alpha) * ((1 - beta) * p1 + beta * p2) + alpha * ((1 - beta) * p3 + beta * p4)
Where
alpha = -(b e - a f + d g - c h + sqrt(-4 (c e - a g) (d f - b h) +
(b e - a f + d g - c h)^2))/(2 c e - 2 a g)
beta = (b e - a f - d g + c h + sqrt(-4 (c e - a g) (d f - b h) +
(b e - a f + d g - c h)^2))/(2 c f - 2 b g)
or
alpha = (-b e + a f - d g + c h + sqrt(-4 (c e - a g) (d f - b h) +
(b e - a f + d g - c h)^2))/(2 c e - 2 a g)
beta = -((-b e + a f + d g - c h + sqrt(-4 (c e - a g) (d f - b h) +
(b e - a f + d g - c h)^2))/( 2 c f - 2 b g))
The coefficients are given by:
a = -p1.x + p3.x
b = -p1.x + p2.x
c = p1.x - p2.x - p3.x + p4.x
d = interpolated_point.x - p1.x
e = -p1.y + p3.y
f = -p1.y + p2.y
g = p1.y - p2.y - p3.y + p4.y
h = interpolated_point.y - p1.y
Where p1-p4 are coordinates of input vertices of the polygon. The issue occurs when p1.x=p3.x
and p2.x=p4.x
but p1.y!=p2.y!=p3.y!=p4.y
- which is the case for a trapezoid. In this case, a=c=0
and the alpha coefficient becomes becomes infinite upon division by 0.
So my question is: is there any way to modify (simplify?) Nico's method for it to be applicable to a trapezoid.