How to calculate the centre point of a circle given three points?

后端 未结 2 868
無奈伤痛
無奈伤痛 2021-01-27 03:42

I am using Javascript and I know the positions of 3 points. I wish to use these to find out the center point of a circle.

I have found this logic (Not the chosen answer

2条回答
  •  南方客
    南方客 (楼主)
    2021-01-27 04:23

    Thanks to @Gaurav Ojha in the comments I found this solution : What is the algorithm for finding the center of a circle from three points?

    And changed it to work with Javascript :

    function CalculateCircleCenter(A,B,C)
    {
        var yDelta_a = B.y - A.y;
        var xDelta_a = B.x - A.x;
        var yDelta_b = C.y - B.y;
        var xDelta_b = C.x - B.x;
    
        center = [];
    
        var aSlope = yDelta_a / xDelta_a;
        var bSlope = yDelta_b / xDelta_b;
    
        center.x = (aSlope*bSlope*(A.y - C.y) + bSlope*(A.x + B.x) - aSlope*(B.x+C.x) )/(2* (bSlope-aSlope) );
        center.y = -1*(center.x - (A.x+B.x)/2)/aSlope +  (A.y+B.y)/2;
        return center;
    
    
    }
    

    All you need to do is pass it 3 points :

     var threePoints = [{x:1, y: 2},{x:4, y: 4},{x:6, y: 2} ]
    
    console.log(CalculateCircleCenter(threePoints[0],threePoints[1],threePoints[2]))
    

    To get this answer :

    [x: 3.5, y: 1.5]
    

    Hope this helps :)

提交回复
热议问题