
tikz を使ってかなりシンプルなシーンを描こうとしています。
私が抱えている問題は、半円上の終点の定義です。交差点検出のアルゴリズムを実装しようとしましたが、疑似コードは以下にあります。サークルライン交差点ただし、期待どおりには動作しません。さらに、\ifthenelse
句を使用するとコンパイルされません。
これを機能させる方法について何か提案はありますか?
\documentclass[11pt]{article}
\usepackage{tikz}
\usepackage{ifthen}
\usepackage{graphics, tkz-berge, tkz-graph}
%%%<
\usepackage{verbatim}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{5pt}%
%%%>
\tikzset{isometricXYZ/.style={x={(-0.866cm,-0.5cm)}, y={(0.866cm,-0.5cm)}, z={(0cm,1cm)}}}
%% document-wide tikz options and styles
\begin{document}
\begin{tikzpicture} [scale=4, line join=round,
opacity=.75, fill opacity=.35, text opacity=1.0,%
>=latex,
inner sep=0pt,%
outer sep=2pt,%
]
% First argument is a ray angle, second argument is an offset along x-axis.
\newcommand{\ray}[2]{
\def\r{1} % sphere radius
\def\l{2} % line length
\def\xc{#2} % offset
% Sphere center
\def\Cx{0}
\def\Cy{0}
% Ray start
\def\Ex{(\xc + (\l*cos(#1)))}
\def\Ey{(\l*sin(#1))}
% Ray end
\def\Lx{\xc}
\def\Ly{0}
% Vector from ray start to end
\def\dx{(\Lx -\Ex)}
\def\dy{(\Ly -\Ey)}
% Vector from ray start sphere center
\def\fx{(\Ex - \Cx)}
\def\fy{(\Ey - \Cy)}
% solve eq
\def\a{(\dx * \dx + \dy * \dy)}
\def\b{(2 * \fx * \dx + \fy * \dy)}
\def\c{(\fx * \fx + \fy * \fy - \r * \r)}
\def\discriminant{(\b*\b - 4*\a*\c)}
\ifthenelse{{\discriminant} < 0}
{
\def\endc{(\xc, 0)}
}
{
\def\sqdiscriminant{sqrt(\discriminant)}
\def\t{(-\b +\sqdiscriminant)/(2*\a)}
\def\endc{({\Ex + \t*\dx}, {\Ey + \t*\dy})}
}
\def\startc{({\Ex}, {\Ey})}
\draw [->] \startc -- \endc;
}
\draw[fill=gray, fill opacity=0.2] (1, 0) arc (0:180:1);
\draw [dotted] (0, 0) -- ({cos(30)}, {sin(30)}) node[above right] {$\alpha_1$};
\draw [dotted] (0, 0) -- ({cos(150)}, {sin(150)}) node[above left] {$\alpha_2$};
\draw [dotted] (0, 0) -- (0, 1.1);
\node[below right] (halfpi) at (1, 0) {$\frac{\pi}{2}$};
\node[below left] (minushalfpi) at (-1, 0) {$-\frac{\pi}{2}$};
\foreach \x in {2}
{
\ray{55}{\x}
};
\end{tikzpicture}
\end{document}
以下の両方の解決策を試しましたが、どちらの場合も交差点が間違って選択されました。これは、すべての場合に最初の交差点を選択するためだと思いますが、残念ながら、それが常に正しいとは限りません。常に正しい交差点を選択する方法はありますか?最も近い最初の交差点ではないですか?
pps :) 気にしないでください。パスの方向を逆にすることで修正しました。
答え1
ライブラリを使用するだけでintersections
、光線と円の交差を手動で計算する必要がなくなります。半円と線を組み合わせたパスなど、中程度に複雑なパスでも機能します。
交差点を取得するには最も近い「光源」に光線を向けるには、光源から光線を開始し、オプション を使用しますsort by=ray
。この方法では、(interection-1)
常に光源に最も近いものが使用されます。
そして、はい、TikZ はすべての計算とソートをTeX
. Witchcraft を使用して実行します。
\documentclass[tikz,margin=2pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture} [scale=4, line join=round, >=latex]
% Name this path for intersections
\draw[fill=gray, fill opacity=0.2, name path=outline]
(-2,0) -- (-1,0) arc (180:0:1)-- (2,0) ;
\draw [dotted] (0, 0) -- ({cos(30)}, {sin(30)}) node[above right] {$\alpha_1$};
\draw [dotted] (0, 0) -- ({cos(150)}, {sin(150)}) node[above left] {$\alpha_2$};
\draw [dotted] (0, 0) -- (0, 1.1);
\node[below right] (halfpi) at (1, 0) {$\frac{\pi}{2}$};
\node[below left] (minushalfpi) at (-1, 0) {$-\frac{\pi}{2}$};
% Properties of the "light source"
\def\angle{45}
\def\width{2.8cm}
% Draw the light source:
\draw[very thick] (\angle:2.5cm) coordinate(light)
++(\angle+90:0.5*\width) -- +(\angle-90:\width);
% Draw a number of rays:
\foreach \x in {-8,...,8} {
% Place a path from light source through circle and name it:
\path[name path=ray,overlay] (light) ++(\angle+90:\x*\width/17)
coordinate(start) -- +(180+\angle:5cm);
% Draw the arrow from light source to first intersection:
\draw[->, name intersections={of=outline and ray, sort by=ray}]
(start) -- (intersection-1);
}
\end{tikzpicture}
\end{document}
答え2
交差点を選択的に描画したい場合は、交差点が見つかるかどうかも実装できます。ただし、交差点の計算は繊細な作業であり、その精度に常に頼れるわけではありません。
\documentclass[tikz]{standalone}
\usetikzlibrary{intersections,calc}
\begin{document}
\begin{tikzpicture}[>=latex]
\begin{scope}[fill opacity=.35]
\fill[blue] (4cm,0)--++(40:1cm) arc (40:180:1cm);
\fill[gray] (4cm,0)--++(40:1cm) arc (40:0:1cm);
\end{scope}
\draw[ultra thick] (1cm,2.5cm) coordinate (ls) -- ++(30:4cm) coordinate (le);
\draw[ultra thick,name path=a] (2cm,-2cm)--++(7cm,0);
\draw[ultra thick,name path=b] (5cm,0) arc (0:180:1cm);
\foreach \x[count=\xi] in {0.1,0.15,...,0.9}{
\path[name path global=ray\xi,overlay] ($(ls)!\x!(le)$) --++(-60:15cm);
\draw[->,
name intersections={of=a and ray\xi,name=intaray\xi},
name intersections={of=b and ray\xi,name=intbray\xi}]
\pgfextra{% Test for the shape name existence
\csname pgfutil@ifundefined\endcsname{pgf@sh@ns@intbray\xi-1}{\def\temp{a}}{\def\temp{b}}%
}
($(ls)!\x!(le)$) -- (int\temp ray\xi-1);
}
\end{tikzpicture}
\end{document}