我想在平面环面上画一条曲线,但不是在整个R^2
平面上,而是在正方形上[0,1]x[0,1]
。
为此,我创建了一个仅包含有理斜率的初始数据的“do...while”。这是:
let m=p/q
let fx(u)=m(1-u) and fy(u)=(1-u)/m
x=0
DO
y=fx(x)
draw (x,0) -- (1,y);
x=fy(y)
draw (0,y) -- (x,1);
WHILE (x=1)
我为代码中的大错误道歉。我只是写下了我的想法,因为我不知道如何在 LaTeX 中实现它,使用蒂克兹例如。我该怎么做?
我已经手动完成了,但这不是最佳选择。尝试 putm=3/4
应该输出以下内容:
我不想编写必须手动绘制所有内容的代码,如下所示:
\documentclass[border=1mm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[red] (0,0) -- (1,3/4);
\draw[red] (0,3/4) -- (1/3,1);
\draw[red] (1/3,0) -- (1,1/2);
\draw[red] (0,1/2) -- (2/3,1);
\draw[red] (2/3,0) -- (1,1/4);
\draw[red] (0,1/4) -- (1,1);
\draw (0,0) -- (1,0) -- (1,1) -- (0,1) -- (0,0) -- (1,0);
\end{tikzpicture}
\end{document}
答案1
这是一个选项。我定义了一个\flattorus
带有三个参数的命令:
- (可选)TikZ 图片中正方形的边长,以厘米为单位。我将 4 作为默认值,但当然您可以轻松更改它。
- 将正方形分割成的列数。
- 将正方形分割成的行数。
\documentclass{article}
\usepackage{tikz}
\newcounter{mx}
\newcounter{my}
\newlength{\squareside}
\newcommand*{\flattorus}[3][4]{%
\setcounter{mx}{#2}
\setcounter{my}{#3}
\addtocounter{mx}{-1}
\addtocounter{my}{-1}
\setlength{\squareside}{#1 cm}
\begin{tikzpicture}[x=\dimexpr\squareside/#2, y=\dimexpr\squareside/#3]
\draw[thick] (0,0) rectangle (#2,#3);
\foreach \x in {0, ..., \value{mx}}
\foreach \y in {0, ..., \value{my}}{
\draw (\x,\y) -- ++(0,1);
\draw (\x,\y) -- ++(1,0);
\draw[red, thick] (\x,\y) -- ++(1,1);
};
\node[below left] at (0,0) {0};
\node[below] at (#2,0) {1};
\node[left] at (0,#3) {1};
\foreach \x in {1, ..., \value{mx}}
\node[below] at (\x,0) {\x/#2};
\foreach \y in {1, ..., \value{my}}
\node[left] at (0,\y) {\y/#3};
\end{tikzpicture}
}
\begin{document}
\flattorus{3}{4}
\flattorus{5}{2}
\flattorus[2]{2}{3}
\end{document}
答案2
@Vincent (+1) 发布了一个很好的解决方案,但是这个解决方案已经在进行中,所以我想我可以将其作为替代方案发布。
这是一个\flattorus
使用两个参数(以及一个附加可选参数)来绘制平面圆环的宏。在圆环上\flattorus[<scale factor>]{y}{x}
绘制斜线。默认比例为 2,生成边长为 2cm 的正方形。y/x
例如,\flattorus{3}{4}\qquad\flattorus{5}{3}
产生:
并\flattorus[6]{10}{11}
生产
\documentclass{article}
\usepackage{tikz}
\newcommand{\flattorus}[3][2]{\begin{tikzpicture}[scale=#1]
\foreach \k[evaluate=\k as \j using int(\k-1)] in {2,...,#2}{
\draw[gray!30] ({(\j)/#2},0)node[black, below]{$\frac{\j}{#2}$}--++(0,1);}
\foreach \k[evaluate=\k as \j using int(\k-1), evaluate=\k as \p using #2*#3] in {2,...,#3}{\xdef\xy{\p}
\draw[gray!30] (0,{(\j)/#3})node[black, left]{$^{\j}\!/\!_{#3}$}--++(1,0);}
\foreach \k[evaluate=\k as \j using int(\k-1), evaluate=\k as \x using frac(\k*#3/\xy), evaluate=\k as \y using frac(\k*#2/\xy)] in {1,...,\xy}{
\draw[red, thick]({frac(\j*#3/\xy)},{frac(\j*#2/\xy)})--({\x+less(\x,1/\xy)},{\y+less(\y,1/\xy)});}
\draw (0,0)node[below left]{0}--(1,0)node[below]{1}--(1,1)--(0,1)node[left]{1}--cycle;
\end{tikzpicture}}
\begin{document}
\flattorus{3}{4}\qquad\flattorus{5}{3}
\flattorus[6]{10}{11}
\end{document}
答案3
试试这个代码:
\documentclass[10pt,a4paper]{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=10]
\draw[gray!30,xstep=.3333,ystep=.25] (0,0) grid (1,1);
\draw[red,line width=2pt] (0,0) -- (1,3/4);
\draw[red,line width=2pt] (0,3/4) -- (1/3,1);
\draw[red,line width=2pt] (1/3,0) -- (1,1/2);
\draw[red,line width=2pt] (0,1/2) -- (2/3,1);
\draw[red,line width=2pt] (2/3,0) -- (1,1/4);
\draw[red,line width=2pt] (0,1/4) -- (1,1);
\foreach \x in {0,1/3,2/3,1}
\draw (\x,.02)--(\x,-.02) node[below] {\bfseries $\x$};
\foreach \y in {0,1/4,1/2,3/4,1}
\draw (.02,\y)--(-.02,\y) node[left] {\bfseries $\y$};
\end{tikzpicture}
\end{document}
输出: