(original) (raw)

function [ r, center ] = triangle_circumcircle ( t ) %*****************************************************************************80 % %% triangle_circumcircle() computes the circumcircle of a triangle in 2D. % % Discussion: % % The circumcenter of a triangle is the center of the circumcircle, the % circle that passes through the three vertices of the triangle. % % The circumcircle contains the triangle, but it is not necessarily the % smallest triangle to do so. % % If all angles of the triangle are no greater than 90 degrees, then % the center of the circumscribed circle will lie inside the triangle. % Otherwise, the center will lie outside the triangle. % % The circumcenter is the intersection of the perpendicular bisectors % of the sides of the triangle. % % In geometry, the circumcenter of a triangle is often symbolized by "O". % % Licensing: % % This code is distributed under the MIT license. % % Modified: % % 04 December 2010 % % Author: % % John Burkardt % % Input: % % real T(2,3), the triangle vertices. % % Output: % % real R, CENTER(1,2), the circumradius and circumcenter. % % % Circumradius. % a = norm ( t(2,:) - t(1,:) ); b = norm ( t(3,:) - t(2,:) ); c = norm ( t(1,:) - t(3,:) ); bot = ( a + b + c ) * ( - a + b + c ) * ( a - b + c ) * ( a + b - c ); if ( bot <= 0.0 ) r = -1.0; center = [ 0.0, 0.0 ]; return end r = a * b * c / sqrt ( bot ); % % Circumcenter. % f = zeros ( 1, 2 ); f(1,1) = ( t(2,1) - t(1,1) ).^2 + ( t(2,2) - t(1,2) ).^2; f(1,2) = ( t(3,1) - t(1,1) ).^2 + ( t(3,2) - t(1,2) ).^2; top = zeros ( 1, 2 ); top(1,1) = ( t(3,2) - t(1,2) ) * f(1,1) - ( t(2,2) - t(1,2) ) * f(1,2); top(1,2) = - ( t(3,1) - t(1,1) ) * f(1,1) + ( t(2,1) - t(1,1) ) * f(1,2); det = ( t(3,2) - t(1,2) ) * ( t(2,1) - t(1,1) ) ... - ( t(2,2) - t(1,2) ) * ( t(3,1) - t(1,1) ) ; center = t(1,1:2) + 0.5 * top(1,1:2) / det; return end