How can I plot a triangle with its altitudes?
이전 댓글 표시
Hi I'd just like to know whats wrong with my coding i have as when i run this it does not plot the altitudes correctly.
x = [0,2,4,0];
y = [0,3,2,0];
%Altitude CF
% Gradient Forumla
% m = (y2-y1)/(x2-x1)
m3 = (y(2)-y(1))/(x(2)-x(1));
altitudem3=-1/m3;
% c = y-(m*x)
base3_c = y(1)-(m3*x(1));
altitude3_c = y(3)-(altitudem3*x(3));
% y=mx+c
% mx+base_c=altitudemx+altitude_c
% x=((altitude_c)-(base_c))/(m-altitudem)
xintersection=(altitude3_c-base3_c)/(m3-altitudem3);
yintersection=(altitudem3*xintersection)+altitude3_c;
%Altitude BE
m2 = (y(3)-y(1))/(x(3)-x(1));
altitudem2=-1/m2;
% c = y-(m*x)
base2_c = y(1)-(m2*x(1));
altitude2_c = y(2)-(altitudem2*x(2));
% y=mx+c
% mx+base_c=altitudemx+altitude_c
% x=((altitude_c)-(base_c))/(m-altitudem)
xintersection2=(altitude2_c-base2_c)/(m2-altitudem2);
yintersection2=(altitudem2*xintersection)+altitude2_c;
%Altitude AD
m = (y(3)-y(2))/(x(3)-x(2));
altitudem=-1/m;
% c = y-(m*x)
base_c = y(2)-(m*x(2));
altitude_c = y(1)-(altitudem*x(1));
% y=mx+c
% mx+base_c=altitudemx+altitude_c
% x=((altitude_c)-(base_c))/(m-altitudem)
xintersection3=(altitude_c-base_c)/(m-altitudem);
yintersection3=(altitudem*xintersection)+altitude_c;
hold on
plot(x,y);
plot([xintersection,x(3)],[yintersection,y(3)])
plot([xintersection2,x(2)],[yintersection2,y(2)])
plot([xintersection3,x(1)],[yintersection3,y(1)])
hold off
댓글 수: 4
John BG
2018년 4월 30일
Jan
2018년 5월 3일
The "altitude" is the line through a vertex, which is orthogonal to the opposite edge.
Thanks Jan
.
I already supplied an answer satisfying the request of the question, the core point to solve being the calculation of such 'altutides' or normal segments to each triangle side.
The question originator added the request that in my opinion should be a new question to show the intersects of the altitudes, besides the intersects of the altitudes on each triangle side.
The answer you have supplied doesn't catch all altitudes for the following particular cases:
1. missing 1 altitude
x = [0,0,4]
y = [0,3,2]
.

.
2. missing 2 altitudes
x = [0,0,4]
y = [0,4,0]
.

.
the horizontal and vertical sides of the triangle should be coloured with the altitudes, but they aren't because this answer doesn't get these altitudes
.
My answer catches such altitudes missed by Jan's answer
.
x = [0,0,4]
y = [0,3,2]

.
x = [0,0,4]
y = [0,4,0]
.

.
John BG
John BG
2018년 5월 5일
Also, none of the supplied answers so far, including mine, consider negative coordinates of the input points
x =[ -6 -5 -4] y =[ -10 4 10]
.

채택된 답변
추가 답변 (2개)
sloppydisk
2018년 4월 30일
0 개 추천
Please clarify what you are trying to do. What would you like to see as output?
Hi Jack Bason
I have rearranged your code to do what you asked for, please find attached copy of this script.
close all;clear all;clc
x=[0,2,4];y=[0,3,2];
x2=[x x x(1)];y2=[y y y(1)];
plot(x2,y2,'b')
hold on
x_intersect=zeros(1,3)
y_intersect=zeros(1,3)
for k=1:1:length(x)
p1=[x2(k) y2(k)]
p2=[x2(k+1) y2(k+1)]
pref=[x2(k+2) y2(k+2)]
% 1st segment slope: m offset: k0
m1=(p2(2)-p1(2))/(p2(1)-p1(1))
switch abs(m1)
case Inf % vertical, 90.00 degree up
yint=pref(2)
xint=p1(1) % = p2(1), xint
m2=0
case 0 % horizontal, completely flat, 0.00 degree
yint=p1(2) % =p2(2)
xint=pref(1) % = p2(1)
m2=0
otherwise
k01=p1(2)-m1*p1(1)
m2=-1/m1 % 2nd segment m k0
k02=pref(2)-m2*pref(1)
xint=(k01-k02)/(m2-m1) % intersect point
yint=m1*xint+k01
x_intersect(k)=xint % logging intersect point
yIntersect(k)=yint
end
plot([p1(1) p2(1)],[p1(2) p2(2)],'g*')
plot(pref(1),pref(2),'y*')
plot(xint,yint,'g*')
plot([xint pref(1)],[yint pref(2)],'r')
end
.

.
if you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance for time and attention
John BG
카테고리
도움말 센터 및 File Exchange에서 Lengths and Angles에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!