Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

20
LINES

## < > BotCompanyRepo | #1033970 // circleCenterFrom3Points

### JavaX fragment (include) [tags: use-pretranspiled]

 `1 ` ```// from https://stackoverflow.com/questions/4103405/what-is-the-algorithm-for-finding-the-center-of-a-circle-from-three-points ``` `2 ` ```// (by Adam Gawne-Cain) ``` `3 ` `4 ` ```static DoublePt circleCenterFrom3Points(DoublePt p1, DoublePt p2, DoublePt p3) { ``` `5 ` ``` double ax = (p1.x + p2.x) / 2; ``` `6 ` ``` double ay = (p1.y + p2.y) / 2; ``` `7 ` ``` double ux = (p1.y - p2.y); ``` `8 ` ``` double uy = (p2.x - p1.x); ``` `9 ` ``` double bx = (p2.x + p3.x) / 2; ``` `10 ` ``` double by = (p2.y + p3.y) / 2; ``` `11 ` ``` double vx = (p2.y - p3.y); ``` `12 ` ``` double vy = (p3.x - p2.x); ``` `13 ` ``` double dx = ax - bx; ``` `14 ` ``` double dy = ay - by; ``` `15 ` ``` double vu = vx * uy - vy * ux; ``` `16 ` ``` if (vu == 0) ``` `17 ` ``` null; // Points are collinear, so no unique solution ``` `18 ` ``` double g = (dx * uy - dy * ux) / vu; ``` `19 ` ``` ret DoublePt(bx + g * vx, by + g * vy); ``` `20 ` `}`