# How can I plot a triangle with its altitudes?

조회 수: 2(최근 30일)
Jack Bason 2018년 4월 30일
편집: John BG 2018년 5월 11일
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
% 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;
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표시숨기기 이전 댓글 수: 3
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]
.

댓글을 달려면 로그인하십시오.

### 채택된 답변

Jan 2018년 5월 4일
편집: Jan 2018년 5월 4일
If the question is: "I'd just like to know whats wrong with my coding", this is the answer:
x = [0,2,4,0];
y = [0,3,2,0];
% This is fine:
m3 = (y(2)-y(1))/(x(2)-x(1));
altitudem3=-1/m3;
base3_c = y(1)-(m3*x(1));
altitude3_c = y(3)-(altitudem3*x(3));
xintersection=(altitude3_c-base3_c)/(m3-altitudem3);
yintersection=(altitudem3*xintersection)+altitude3_c;
m2 = (y(3)-y(1))/(x(3)-x(1));
altitudem2=-1/m2;
base2_c = y(1)-(m2*x(1));
altitude2_c = y(2)-(altitudem2*x(2));
xintersection2=(altitude2_c-base2_c)/(m2-altitudem2);
% yintersection2=(altitudem2*xintersection)+altitude2_c;
% ^ Here is the problem
% It must be xintersection2:
yintersection2=(altitudem2*xintersection2)+altitude2_c;
m = (y(3)-y(2))/(x(3)-x(2));
altitudem=-1/m;
base_c = y(2)-(m*x(2));
altitude_c = y(1)-(altitudem*x(1));
xintersection3=(altitude_c-base_c)/(m-altitudem);
yintersection3=(altitudem*xintersection)+altitude_c;
% ^ Here is the problem
% It must be xintersection3:
yintersection3=(altitudem*xintersection3)+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)])
This is a typical problem, when code is duplicated by copy&paste. Modifying all occurrences of the differences is tricky. A more reliable solution is to use a loop instead. See John BG's answer, which considers m==0 in addition. But a code as near as possible to your code:
x = [0,2,4,0];
y = [0,3,2,0];
hold on
plot(x,y);
v = mod(1:5, 3) + 1;
for k = 1:3
i1 = v(k);
i2 = v(k+1);
i3 = v(k+2);
m = (y(i2)-y(i1)) / (x(i2)-x(i1));
altitudem = -1/m;
base_c = y(i1) - m * x(i1);
altitude_c = y(i3) - altitudem * x(i3);
xintersection(k) = (altitude_c - base_c) / (m - altitudem);
yintersection(k) = (altitudem * xintersection(k)) + altitude_c;
plot([xintersection(k), x(i3)], [yintersection(k), y(i3)])
end
Using loops instead of copy&past + modifications reduces the chance for typos.

댓글을 달려면 로그인하십시오.

### 추가 답변(2개)

sloppydisk 2018년 4월 30일
Please clarify what you are trying to do. What would you like to see as output?
##### 댓글 수: 1표시숨기기 없음
Jack Bason 2018년 4월 30일
As an output I would like to see a plotted triangle with all 3 altitudes that intersect at one point. The coding needs to have the ability to change the co-ordinates and it still work.

댓글을 달려면 로그인하십시오.

John BG 2018년 5월 1일
편집: John BG 2018년 5월 1일
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
.
.
thanks in advance for time and attention
John BG

댓글을 달려면 로그인하십시오.

### 범주

Find more on Graphics Performance in Help Center and File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by