Skip to content

Instantly share code, notes, and snippets.

@edgar-maciel
Forked from mutoo/circumcircle.js
Last active August 29, 2015 14:06
Show Gist options
  • Save edgar-maciel/44246ea6e51e3a0f0110 to your computer and use it in GitHub Desktop.
Save edgar-maciel/44246ea6e51e3a0f0110 to your computer and use it in GitHub Desktop.
function circumcircle(a, b, c) {
this.a = a
this.b = b
this.c = c
var A = b.x - a.x,
B = b.y - a.y,
C = c.x - a.x,
D = c.y - a.y,
E = A * (a.x + b.x) + B * (a.y + b.y),
F = C * (a.x + c.x) + D * (a.y + c.y),
G = 2 * (A * (c.y - b.y) - B * (c.x - b.x)),
minx, miny, dx, dy
/* If the points of the triangle are collinear, then just find the
* extremes and use the midpoint as the center of the circumcircle. */
if(Math.abs(G) < 0.000001) {
minx = Math.min(a.x, b.x, c.x)
miny = Math.min(a.y, b.y, c.y)
dx = (Math.max(a.x, b.x, c.x) - minx) * 0.5
dy = (Math.max(a.y, b.y, c.y) - miny) * 0.5
this.x = minx + dx
this.y = miny + dy
this.r = sqrt(dx * dx + dy * dy)
}
else {
this.x = (D*E - B*F) / G
this.y = (A*F - C*E) / G
dx = this.x - a.x
dy = this.y - a.y
this.r = sqrt(dx * dx + dy * dy)
}
}
@edgar-maciel
Copy link
Author

    /* If the points of the triangle are colarcar, then just find the
     * extremes and use the midpoint as the center of the circumCircle. */
    //        if (Math.abs(G) < 0.000001) {
    //            minx = Math.min(a.x, b.x, c.x)
    //            miny = Math.min(a.y, b.y, c.y)
    //            dx = (Math.max(a.x, b.x, c.x) - minx) * 0.5
    //            dy = (Math.max(a.y, b.y, c.y) - miny) * 0.5
    //
    //            return {
    //                x: minx + dx,
    //                y: miny + dy,
    //                r: Math.sqrt(dx * dx + dy * dy)
    //            }
    //        } else {
    dx = ((D * E - B * F) / G) - a.x
    dy = ((A * F - C * E) / G) - a.y

    return {
        x: (D * E - B * F) / G,
        y: (A * F - C * E) / G,
        r: Math.sqrt(dx * dx + dy * dy)
    }

    //        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment