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

20
LINES

< > BotCompany Repo | #1033970 // circleCenterFrom3Points

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

Libraryless. Click here for Pure Java version (4686L/26K).

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  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1033970
Snippet name: circleCenterFrom3Points
Eternal ID of this version: #1033970/3
Text MD5: 21029ee9a06cf7a7a2c048e3e95aaa1d
Transpilation MD5: a36ab6709ea420d2b4ac2732a68dc335
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-01-15 03:27:49
Source code size: 735 bytes / 20 lines
Pitched / IR pitched: No / No
Views / Downloads: 100 / 132
Version history: 2 change(s)
Referenced in: [show references]