Skip to content

Instantly share code, notes, and snippets.

@jc245410
Created April 25, 2012 08:33
Show Gist options
  • Select an option

  • Save jc245410/2488207 to your computer and use it in GitHub Desktop.

Select an option

Save jc245410/2488207 to your computer and use it in GitHub Desktop.
Plotting a Circle through three points.
% Plotting a Circle through three random points
%
% __________________________
% 3 random points ---> | calculate circle formula | -----> plot circle
% |__________________________|
%
%
clear
clc
%
%
% Outline 1: Randomize 3 points
%
randompoints = round(10*rand(2,3)) %random array from 0 - 10
%
x1=randompoints(1,1)% random points from array to make first coord
y1=randompoints(2,1)
%
x2=randompoints(1,2)%random points from array to make second coord
y2=randompoints(2,2)
%
x3=randompoints(1,3)%random points from array to make third coord
y3=randompoints(2,3)
%
plot(randompoints(1,1),randompoints(2,1),'r.') %plotting all 3 points on graph
hold all
plot(randompoints(1,2),randompoints(2,2),'g.')
plot(randompoints(1,3),randompoints(2,3),'b.')
axis([0 20 0 20]) % cleaning up the graph to look nicer
grid on
%
% Outline 2: Find Equation of AB line
%
gradientAB = (y2-y1)/(x2-x1) %gradient of the line AB
yintAB = (y1-(gradientAB*x1)) %y-intercept of line AB
%
% Outline 4: Find Equation of BC line
%
gradientBC = (y3-y2)/(x3-x2) %gradient of the line AB
yintBC = (y2-(gradientBC*x2)) %y-intercept of line AB
%
% Outline 3: Find equation of Perpendicular line to AB
%
gradientperpAB = -1/gradientAB %gradient of perpendicular line
xmidAB = (x1+x2)/2 %finds the middle of the the AB line
ymidAB = (y1+y2)/2 %
yintperpAB = ymidAB +(-1*gradientperpAB*xmidAB) %gets the y int of the perpendicular line
%
% Outline 5: Find Equation of perpendicular line to BC
%
gradientperpBC = -1/gradientBC %gradient of perpendicular line
xmidBC = (x2+x3)/2 %finds the middle of the the AB line
ymidBC = (y2+y3)/2
yintperpBC = ymidBC +(-1*gradientperpBC*xmidBC) %gets the y int of the perpendicular line
gradienta = gradientAB
gradientb = gradientBC
%Outline 6: Simultaneously solve the intersect point of the two lines
%
xorigin= (yintperpBC - yintperpAB) / (gradientperpAB - gradientperpBC) %finds the x value of the origin of the cirlce
yorigin=xorigin*gradientperpAB + yintperpAB %finds the y value of the origin of the circle
%
% Outline 7: find radius from this point which is the origin
%
radius=sqrt((xorigin-x1)^2+(yorigin-y1)^2)
%
% Outline 8: Plot the circle
%
% Credit for code to plot a cirlce goes to:
% http://www.mathworks.com/matlabcentral/answers/3058-plotting-circles
%
ang=0:0.01:2*pi;
xp=radius*cos(ang);
yp=radius*sin(ang);
plot(xorigin+xp,yorigin+yp,'k');
hold off
% END OF PROGRAM
@jc245364
Copy link

randompoints = round(10*rand(2,3))
You could try an alternative way of creating random points
randompoints = randi(10,2,3)

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