Deriving an angle from three points

Mark Wieder mwieder at ahsoftware.net
Wed Dec 16 04:02:04 EST 2009


Mark-

Tuesday, December 15, 2009, 10:21:47 PM, you wrote:

> 40,116
> 98,186
> 132,118

> How would one determine the angle created from three points, such as those above?

If I can assume that (98,186) is the center of the angle, then you can
calculate the angle (actually all three angles) from the lengths of
the three sides of the triangle:

on mouseUp
    local pointA, pointB, pointC
    local lengthA, lengthB, lengthC
    local tSub
    local tRadians
    
    put "40,116" into pointA
    put "98,186" into pointB -- assumed center of angle
    put "132,118" into pointC
    
    -- get the lengths of the three sides of the triangle
    put SideLength(pointA, pointB) into lengthA
    put SideLength(pointB, pointC) into lengthB
    put SideLength(pointA, pointC) into lengthC
    
    --calculate the angle as arccos( (b2+c2-a2) / 2bc)
    --the other two angles are arccos( (a2+c2-b2) / 2ac)
    -- and arccos( (a2+b2-c2) / 2ab)
    put (lengthB * lengthB) + (lengthC * lengthC) - (lengthA * lengthA) into tSub
    put acos(tSub / (2 * lengthB * lengthC))  into tRadians
    put tRadians * 180 / pi -- convert from radians to degrees
end mouseUp

-- calculate (x2-x1)^2 + (y2-y1)^2
-- return the square root of that
function SideLength pPointA, pPointB
    local lengthX, lengthY
    
    set the itemdelimiter to comma
    put item 1 of pPointB - item 1 of pPointA into lengthX
    put lengthX * lengthX into lengthX -- x squared
    put item 2 of pPointB - item 2 of pPointA into lengthY
    put lengthY * lengthY into lengthY -- y squared
    return sqrt(lengthX + lengthY) -- length of hypotenuse
end SideLength

...and I come up with 64.680313 degrees.

-- 
-Mark Wieder
 mwieder at ahsoftware.net




More information about the Use-livecode mailing list