In: Computer Science
Write a program that draws the circumscribed circle (also known as the circumcircle) of a given triangle ABC; this circle passes through points A, B, and C. These points will be specified by the user by clicking the mouse button. Remember, the three perpendicular bisectors of the three edges of a triangle all pass through one point, the circumcenter, which is the center of the circumscribed circle.
Answer:
MATLAB code to draw circumscribed cirlce for a given triangel ABC:
function [r,cn]=circumcircle(cor,pl)
if (nargin==1) pl=0,end
if (size(cor) ~= [2,3])
error('Needs three vertices of a triangle');
end
%error check computing the area
ar=polyarea(cor(1,:),cor(2,:));
if (ar<1e-9)
error('Degenerate triangle','Three points are almost collinear');
end
%compute the length of sides (AB, BC and CA) of the triangle
c=norm(cor(:,1)-cor(:,2));
a=norm(cor(:,2)-cor(:,3));
b=norm(cor(:,1)-cor(:,3));
r=a*b*c/(4*ar);
bar=[a^2*(-a^2+b^2+c^2),b^2*(a^2-b^2+c^2),c^2*(a^2+b^2-c^2)];
cn=bar(1)*cor(:,1)+bar(2)*cor(:,2)+bar(3)*cor(:,3);
cn=cn/sum(bar);
if (pl==1)
th=linspace(0,2*pi);
x=cn(1)+r*cos(th);
y=cn(2)+r*sin(th);
pn=[cor,cor(:,1)];plot(x,y,pn(1,:),pn(2,:),'-*');
end
Let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions. Thank You! ===========================================================================