So then... I broke the problem down into smaller triangles...
I'm using the convention of labelling the vertices of a Triangle in capital Letters and labelling the sides with the lower-case letter of it's opposite vertex. For example, the line between
A &
B in △
ABC would be
c : the line between
P &
B in △
BNP would be
n etc.
I'm also defining the coordinates of vertices relative to the top left of the image (x = 0, y = 0), and where the x-axis value of
A would be labelled
Ax and the y-axis value of
C would be
Cy etc. I think this is less fussy than referring to coordinates with brackets - B(x, y) or A(,y) or N(x,) etc.
Figure C:
.
.
What I'm interested in here is △
BNP. You can calculate the six properties (three sides, three angles) of any triangle if you know at least three of those properties to start with.
I can work out the length of the line
n in △
BNP from the coords of
P &
B with
the formula I posted earlier...
△BNP: n = √((Px - Bx)² + (Py - By)²)
Note that the length of
n in △
BNP is also the length of
c in △
BCP. Similarly, working out the angle of
B in △
BCP will give us the angle of
B in △
BNP. Using the same formula I can calculate the length of
c &
p in △
BCP...
△BCP: c = √((Px - Bx)² + (Py - By)²)
△BCP: b = √((Cx - Px)² + (Cy - Py)²)
△BCP: p = √((Cx - Bx)² + (Cy - By)²)
... and then use the 3-side method to find the angle of
B in △
BCP, which will also be the angle of
B in △
BNP...
△BCP: B° = arccos((c² + p² - b²) / (2 × c × p))
That gives me one side
n and one angle
B in △
BNP, so I need one more property to work out the rest - and it can only be angle
P.
Because I know the coords can give the distance between
B &
P on both the horizontal and vertical I can form right-angle triangle with its hypotenuse
q along
B &
P: △
BQP in Figure C above. Then its just a matter of using the cosine rule to work out angle
P in △
BQP...
△BQP: P° = arccos((Px - Bx) / (Py - By))
That gives me a segment of the angle I want, with the remainder being, in this case, the
reference angle of
a - that is; the angle between the terminal of
a and the x-axis. As
a terminates in the 3rd quadrant of a circle that means the remainder equals
a - 180. This allows me to work out angle
P in △
BNP by simple addition...
△BNP: P° = arccos((Px - Bx) / (Py - By)) + (a - 180)
So, for the triangle I'm interested in △
BNP I now have my three properties: the length of side
n and the angles
P° and
B° at either end of it. The interior angles of a triangle add to 180° so finding out angle
N is very easy...
△BNP: N° = 180 - (B° + P°)
Now it's just a matter of applying the sine rule, where the lengths of the sides of a triangle divided by the Sines of their opposite angle are always equal...
△BNP: n / sin(N°) = b / sin(B°) = p / sin(P°)
... I don't have a value for p, so ...
△BNP: n / sin(N°) = b / sin(B°)
OR
△BNP: b = (n / sin(N°)) × sin(B°)
The
b side of △
BNP is the value of
d I was looking for. All that pans out to:
d = (√((Px - Bx)² + (Py - By)²) / sin(180 - (arccos((((Px - Bx)² + (Py - By)²) + ((Cx - Bx)² + (Cy - By)²) - ((Px - Bx)² + (Py - By)²)) / (2 × √((Px - Bx)² + (Py - By)²) × √((Cx - Bx)² + (Cy - By)²))) + arccos((Px - Bx) / (Py - By)) + (a - 180)))) × sin(arccos((((Px - Bx)² + (Py - By)²) + ((Cx - Bx)² + (Cy - By)²) - ((Px - Bx)² + (Py - By)²)) / (2 × √((Px - Bx)² + (Py - By)²) × √((Cx - Bx)² + (Cy - By)²))))
However, in order to make a formula out of this that works for every △
ABC, point
P within it, and possible angle
a, I'll have to think about accounting for the quadrants of the main vertices of △
ABC fall into in relation to wherever
P is along with the terminal of whatever
a turns out to be.