Skip to content

Instantly share code, notes, and snippets.

@jc245410
Created May 19, 2012 13:33
Show Gist options
  • Select an option

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

Select an option

Save jc245410/2730872 to your computer and use it in GitHub Desktop.
Plotting a Circle through three points. (Final version at top and early progression versions progressing down.
%PLOTTING A CIRCLE THROUGH 3 RANDOM POINTS OR USER INPUT POINTS
%Makes random array for random 3 points
randompoints = randi(10,2,3);
%loop to check whether all 3 points line up, if they do it re-generates the
%points.
while ((randompoints(2,2))-(randompoints(2,1)))/((randompoints(1,2))-(randompoints(1,1)))== ((randompoints(2,3)-randompoints(2,2))/((randompoints(1,3))-randompoints(1,2)))
randompoints = randi(10,2,3);
end
%sets variables for check
p1 = [randompoints(1,1),randompoints(2,1)];
p2 = [randompoints(1,2),randompoints(2,2)];
p3 = [randompoints(1,3),randompoints(2,3)];
% checks for any two points lieing on the same location
% re-generates if any are same
while (p1 == p2) | (p1 == p3) | (p2 == p3)
randompoints = randi(10,2,3);
p1 = [randompoints(1,1),randompoints(2,1)];
p2 = [randompoints(1,2),randompoints(2,2)];
p3 = [randompoints(1,3),randompoints(2,3)];
end
%re sets variables to use for another check, need to be reset because they
%may have changed because of previous checks.
x1=randompoints(1,1);
y1=randompoints(2,1);
x2=randompoints(1,2);
y2=randompoints(2,2);
x3=randompoints(1,3);
y3=randompoints(2,3);
%Test to check whether any points are horizontally alligned, which then
%changes the order they are accessed in So that the program can correctly
%calculate the circle.
while (y2-y1)/(x2-x1)==0 || (y3-y2)/(x3-x2)==0
randompoints = [x2 x3 x1; y2 y3 y1];
x1=randompoints(1,1);
y1=randompoints(2,1);
x2=randompoints(1,2);
y2=randompoints(2,2);
x3=randompoints(1,3);
y3=randompoints(2,3);
end
%plots each random points and sets up graph axis.
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
%find the equation of the lines and perpendicular lines.
gradientAB = (y2-y1)/(x2-x1);
yintAB = (y1-(gradientAB*x1));
gradientBC = (y3-y2)/(x3-x2);
yintBC = (y2-(gradientBC*x2));
gradientperpAB = -1/gradientAB;
xmidAB = (x1+x2)/2;
ymidAB = (y1+y2)/2;
yintperpAB = ymidAB +(-1*gradientperpAB*xmidAB);
gradientperpBC = -1/gradientBC;
xmidBC = (x2+x3)/2;
ymidBC = (y2+y3)/2;
yintperpBC = ymidBC +(-1*gradientperpBC*xmidBC);
%find centre coordinates of circle
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
plot(xorigin,yorigin,'k.')
%find radius of circle
radius=sqrt((xorigin-x1)^2+(yorigin-y1)^2);
%
%
% Credit for below 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
% Plotting a Circle through three random points
%
% __________________________
% 3 random points ---> | calculate circle formula | -----> plot circle
% |__________________________|
%
%
clear
clc
close all
%
%
% 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.')
%plot(x1,y1,'r.')
%plot(x2,y2,'g.')
%plot(x3,y3,'b.')
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 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 -(gradientperpAB*xmidAB) %gets the y int of the perpendicular line
%
% 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 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 -(gradientperpBC*xmidBC) %gets the y int of the perpendicular line
% 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 + yintperpBC %finds the y value of the origin of the circle
%
% Outline 7: find radius from this point which is the origin
%
%
% Outline 8: Plot the circle
%
%
% Outline 9: fix the graph
%
%
% Outline 10: CREATE GUI???
%
%
% Plotting a Circle through three random points
%
% __________________________
% 3 random points ---> | calculate circle formula | -----> plot circle
% |__________________________|
%
%
clear
clc
close all
%
%
% 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.')
%plot(x1,y1,'r.')
%plot(x2,y2,'g.')
%plot(x3,y3,'b.')
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 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 -(gradientperpAB*xmidAB) %gets the y int of the perpendicular line
%
% 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 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 -(gradientperpBC*xmidBC) %gets the y int of the perpendicular line
% 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 + yintperpBC %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
%
circle(xorigin,yorigin,radius)
%
% Outline 9: fix the graph
%
%
% Outline 10: CREATE GUI???
%
%
% 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])
%plot(x1,y1,'r.')
%plot(x2,y2,'g.')
%plot(x3,y3,'b.')
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 -(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 -(gradientperpBC*xmidBC) %gets the y int of the perpendicular line
% 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 - yintperpBC %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
%
% Code for plotting a circle taken from:
% http://www.mathworks.com/matlabcentral/answers/3058-plotting-circles
%
% function circle(x,y,r)
% x and y are the coordinates of the center of the circle
% r is the radius of the circle
% 0.01 is the angle step, bigger values will draw the circle faster but
% you might notice imperfections (not very smooth)
ang=0:0.01:2*pi;
xp=radius*cos(ang);
yp=radius*sin(ang);
plot(xorigin+xp,yorigin+yp,'k');
%
% Outline 9: fix the graph
%
%
% Outline 10: CREATE GUI???
%
%
hold off
% 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
%this while loop makes the generation of the random points exclude any
%points which are in a straight line to eliminate errors.
while ((randompoints(2,2))-(randompoints(2,1)))/((randompoints(1,2))-(randompoints(1,1)))== ((randompoints(2,3)-randompoints(2,2))/((randompoints(1,3))-randompoints(1,2)))
randompoints = round(10*rand(2,3))
end
%
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment