这提供了有关如何画一条过一个点并垂直于另一个点的线?。我想要做的是扩展它,添加常用的几何符号来表示这两条线确实是垂直的。我可以为每种情况编写一个手动解决方案,但我想要一个宏,一个可以绘制适当大小的宏,并允许我为这个符号选择方向(即选择四个象限之一)。
答案1
以下是使用 TikZ 样式的一种可能方法。您可以使用样式插入角度符号
right angle symbol={<Point 1>}{<Point 2>}{<Point 3>}
在draw
命令中,其中<Point 1>
和<Point 2>
是直线上的两个点(下图中A
和),是垂直线上的一个点(下图中或)。如果您想要单独使用角度符号,只需在新的 中使用它:B
<Point 3>
Q
P
draw command
\draw [right angle symbol={<Point 1>}{<Point 2>}{<Point 3>}];
可以使用 选择象限right angle quadrant=<1-4>
,使用 选择尺寸right angle length=<length>
。这两个选项都必须在 之前调用right angle symbol
。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\tikzset{
right angle quadrant/.code={
\pgfmathsetmacro\quadranta{{1,1,-1,-1}[#1-1]} % Arrays for selecting quadrant
\pgfmathsetmacro\quadrantb{{1,-1,-1,1}[#1-1]}},
right angle quadrant=1, % Make sure it is set, even if not called explicitly
right angle length/.code={\def\rightanglelength{#1}}, % Length of symbol
right angle length=2ex, % Make sure it is set...
right angle symbol/.style n args={3}{
insert path={
let \p0 = ($(#1)!(#3)!(#2)$) in % Intersection
let \p1 = ($(\p0)!\quadranta*\rightanglelength!(#3)$), % Point on base line
\p2 = ($(\p0)!\quadrantb*\rightanglelength!(#2)$) in % Point on perpendicular line
let \p3 = ($(\p1)+(\p2)-(\p0)$) in % Corner point of symbol
(\p1) -- (\p3) -- (\p2)
}
}
}
\begin{document}
\begin{tikzpicture}[dot/.style={circle,inner sep=1pt,fill,label={#1},name=#1},
extended line/.style={shorten >=-#1,shorten <=-#1},
extended line/.default=1cm]
\node [dot=A] at (0,0) {};
\node [dot=B] at (3,1) {};
\node [dot=P] at (0.9,-1.2) {};
\node [dot=Q] at (1.3,2.2) {};
\draw [extended line=0.5cm] (A) -- (B);
\draw [extended line] ($(A)!(P)!(B)$) -- (P);
\draw [red,right angle symbol={A}{B}{P}];
\draw [extended line,right angle quadrant=3,right angle symbol={A}{B}{Q}] ($(A)!(Q)!(B)$) -- (Q);
\end{tikzpicture}
\end{document}
答案2
另一种可能性是使用我的包 tkz-euclide(现在在 ctan 和 texlive 2011 上),但您不需要获取我定义的所有对象。只有角度是必要的。我以 Jake 的例子向您展示您可以混合使用 tikz 和 tkz。
\usetkzobj{angles}
此宏加载角度的所有宏,如果您需要其他对象,\usetkzobj{angles,polygons}
语法与相同\usetikzlibrary
,如果您想要所有对象,您可以编写\usetkzobj{all}
\documentclass{article}
\usepackage{tkz-euclide} % loads TikZ and tkz-base
\usetkzobj{angles} % important you want to use angles
\begin{document}
\begin{tikzpicture}[dot/.style={circle,inner sep=1pt,fill,label={#1},name=#1},
extended line/.style={shorten >=-#1,shorten <=-#1},
extended line/.default=1cm]
\node [dot=A] at (0,0) {};
\node [dot=B] at (3,1) {};
\node [dot=P] at (0.9,-1.2) {};
%\draw [extended line=0.5cm] (A) -- (B);
% In tkz-euclide I defined something like extend line
% but I prefer my method because I add a percentage of the segment at each sides
% with [add = % and %] left and right
\tkzDrawLine[add=1 and .5](A,B) % with 1 you double the line BA from A
\draw [extended line] ($(A)!(P)!(B)$) coordinate (H) -- (P);
% I named the projection H
\tkzMarkRightAngle[fill=blue!20,size=.5](A,H,P) % size number in cm
% and you need to give the points in an order counterclockwise
\tkzMarkRightAngle[fill=red!20,size=.8](B,H,P)
\end{tikzpicture}
% Now an example with only tkz-euclide
\begin{tikzpicture}[scale=.5]
\tkzDefPoint(0,0){A}
\tkzDefPoint(4,2.5){B}
\tkzDefPoint(4,5){C}
\tkzDrawLine[add= 0.5 and 0.8,color=blue](A,B)
\tkzDefPointBy[projection=onto B--A](C) \tkzGetPoint{H}
\tkzDrawLine[add = .5 and .2,color=red](C,H)
\tkzLabelPoint(H){$h$}
\tkzMarkRightAngle[fill=blue!20,size=.5](C,H,B)
\tkzDrawPoints(A,B,C)\tkzLabelPoints(A,B,C) % better now
\end{tikzpicture}
\end{document}