How can I draw a right angle with MetaPost?

I want to use MetaPost to draw a right angle BAC. A = (20,30), B = (0,0), the top of the angle is point A.

How can I compute the coordinate of point C? • Angle's top is on which point? – Thanos Nov 3 '12 at 12:21
• It is point A. – 比尔盖子 Nov 3 '12 at 12:22
• since the right angle B is at origin, you can have C at (-20,30) or (20,-30) based on A's coordinate. – mythealias Nov 3 '12 at 12:36
• Oops, I made a terrible typo. In fact, I want to draw right angle BAC. – 比尔盖子 Nov 3 '12 at 12:41

You can use dotprod:

u:=1mm;
beginfig(1);
z0=origin;
z1=(20u,30u);
y2=y0;
(z1-z0) dotprod (z2-z1)=0;
draw z0--z1--z2--cycle;
endfig;
end.

run with xelatex

\documentclass[a4paper,10pt]{article}
\def\drawAngle(#1)(#2){%
\psnode(#1){A}{A}\psnode(#2){B}{B}
\psnode(!\psGetNodeCenter{A}\psGetNodeCenter{B}
A.y B.y sub A.x add A.x B.x sub neg A.y add ){C}{C}
\psline(B)(A)(C)
\psarc(A){1}{!\psGetNodeCenter{A}\psGetNodeCenter{B}
B.y A.y sub B.x A.x sub atan}{!B.y A.y sub B.x A.x sub atan 90 add}%
}
\begin{document}
\begin{pspicture}[showgrid](0,-3)(4,3)
\drawAngle(1,2)(2,-2)
\psset{linecolor=red}
\drawAngle(4,-1)(2,1)
\end{pspicture}
\end{document} • I use MetaPost only. – 比尔盖子 Nov 3 '12 at 13:10
• should not be difficult to write \drawAngle in Metapost. You can use B.x also in MP. – user2478 Nov 3 '12 at 13:19

Just 4 fun with PSTricks: \documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-eucl}
\begin{document}
\begin{pspicture}(6.6,3)
\psset{PointSymbol=none}
\pstGeonode[PosAngle={45,-135}](2,3){A}(0,0){B}
\pstRotation[RotAngle=90,PointName=none]{A}{B}
\pnode(A|B){B''}
\pstInterLL{A}{B'}{B}{B''}{C}
\pspolygon(A)(B)(C)
\pstRightAngle{B}{A}{C}
\end{pspicture}
\end{document}

Descriptions:

• \psset{PointSymbol=none} to turn the dots off.

• \pstGeonode[PosAngle={45,-135}](2,3){A}(0,0){B} to specify the point A and B.

• \pstRotation[RotAngle=90,PointName=none]{A}{B} to rotate point B 90 degrees about A, the new point is implicitly named as B'.

• \pnode(A|B){B''} to define an auxiliary point B'' whose coordinate is (A.x,B.y).

• \pstInterLL{A}{B'}{B}{B''}{C} to find the intersection point C between the lines AB' and BB''.

• \pspolygon(A)(B)(C) to draw the triangle ABC.

• \pstRightAngle{B}{A}{C} to attach the L-shape right angle mark.

• I'm using MetaPost, but it's really a good tutorial! – 比尔盖子 Nov 9 '12 at 8:41

First of all you understand that there are infinite point that are perpindicular to AB. So actually you are looking for a line. To find it, just assume the vector AB with coordinates BA=(20-0,30-0)=(20,30)=a and vector BC=(x-0, y-0)=(x,y)=b.

To find vector coordinates you just apply (xend-xstart, yend-ystart). The requirement you have is that a and b must be perpendicular. In vector analysis, this means that the dot product must be zero, ie

a.b=0->
(20,30).(x,y)=0->20x+30y=0->
y=-(2/3)x

So choose an x apply the previous line equation and you'll get your y. Edit: I saw you changed your question. In that way you will have a new line with same slope but with different intercept. So your line will be something like

y=ux+w

with u being the same slope, u=-2/3, that is. On the same time it should pass from point A so this point has to be a solution of the line. So replacing A's coordinates in line equation you'll get

30=-(2/3)20+b->
...->
b=130/3

So your new line is

y=-(2/3)x+130/3

Again for every x you'll get a y.

• Can I write a MetaPost macro to solve it? – 比尔盖子 Nov 3 '12 at 13:00