Given the image below
1. I have updated the image based on feedback from Steven_W so there are 4 triangles in the right hand diagram instead of 3 which makes more sense!
2. update image again to label sub triangles as A, B, C, D in grey
What is the pseudo algorithm for mapping a coordinate (x,y) in the left hand square such that a coordinate (u,v) is produced within the rectangle bounding the triangle on the right so that points are interpolated between the mapping points as illustrated on the diagram?
1 to 4 are equidistant on the triangle from left to right even though my illustration is a bit rough around the edges :)
This is to generate a rough and ready panel for the lid of a skybox from the top half a 360 degree panoramic photo.
update 3 based on feedback
The first step appears to be working out which triangle we are in for the left hand diagram based on the (x,y) coordinates.
The second steep is to work out the distance along the vertices of that triangle. Then use those distances to get the coordinates on the related triangle in the diagram on the right
update 4 - code to identify triangle in left hand diagram
Public Function TriangleIndex(ByVal x As Integer, ByVal y As Integer, ByVal w as integer, ByVal h as integer) as integer
Dim AboveForwardSlashDiagonal As Boolean = ((((h * x) + (w * y)) - (h * w)) < 0)
Dim AboveBackSlashDiagonal As Boolean = (((h * x) - (w * y)) > 0)
If AboveForwardSlashDiagonal Then
If AboveBackSlashDiagonal
return 2 ' C
else
return 3 ' D
end if
else
If AboveBackSlashDiagonal
return 1 ' B
else
return 0 ' A
end if
End If
End Function
update 5 - template for code solution
w1 and h1 are dimensions of left diagram w2 and h2 are dimensions of right diagram
Private Function TranslateToTriangle(ByVal x1 As Integer, ByVal y1 As Integer, ByVal w1 As Integer, ByVal h1 As Integer, ByVal w2 As Integer, ByVal h2 As Integer) As System.Drawing.Point
Dim ReturnPoint As New System.Drawing.Point
select case TriangleIndex(x1,y1,w1,h1)
case 0
case 1
case 2
case 3
end select
Return ReturnPoint
End Function
update 6 formula for area of triangle given it's lengths - which might be helpful in calculating barycentric weights?
Private Function AreaOfTriangle(ByVal LengthA As Single, ByVal LengthB As Single, ByVal LengthC As Single) As Single
Dim Perimeter As Single = LengthA + LengthB + LengthC
Return 1 / 4 * Math.Sqrt(Perimeter * (Perimeter - 2 * LengthA) * (Perimeter - 2 * LengthB) * (Perimeter - 2 * LengthC))
End Function