如何在 Tikz 中刪除曲線的一部分?

如何在 Tikz 中刪除曲線的一部分?

我想這樣做: 在此輸入影像描述

我其實是從這段程式碼開始的:

\begin{figure}
\begin{tikzpicture}[scale=1.2]
% Axis
\draw [->] (0,0) node [below] {0} -- (0,0) -- (5.5,0) node [below] {Good 1};
\draw [->] (0,0) node [below] {0} -- (0,0) -- (0,5.5) node [above] {Good 2};
% Indifference curve
\draw (0.3,5) to   [out=280,in=175] (5.5,0.5);
\draw (0,4.1) to (4.1,0);
\end{tikzpicture}

在此輸入影像描述 此時此刻,我有這個。我想知道如何刪除線下的曲線部分,然後為曲線和線的整個交點添加顏色。

答案1

你可以嘗試使用漸近線

我將為您詳細描述我的程式碼。

程式碼可以在以下位置編譯http://asymptote.ualberta.ca/

unitsize(1cm);
/* page 48 (official documentation)
To make the user coordinates of picture pic represent multiples of x units in
the x direction and y units in the y direction, use
void unitsize(picture pic=currentpicture, real x, real y=x);
*/

defaultpen(linewidth(0.7bp)); // set the width of all paths by 0.7bp
/* page 41 (official documentation)
The pen line width is specified in PostScript units with pen linewidth(real). The
default line width is 0.5 bp; this value may be changed with defaultpen(pen).
*/

draw(Label("Good 1",Relative(.99)),(0,0)--(5.5,0),Arrow);
// 4.1 draw
// is equivalent to \draw [->] (0,0) node [below] {0} -- (0,0) -- (5.5,0) node [below] {Good 1};

draw(Label("Good 2",EndPoint),(0,0)--(0,5.5),Arrow);
// is equivalent to \draw [->] (0,0) node [below] {0} -- (0,0) -- (0,5.5) node [above] {Good 2};

guide g1=(0.3,5){dir(280)}..{dir(-5)}(5.5,0.5);
// 5 Bezier curves (official documentation)
// 6.2 Paths and guides
// the direction of out=280, the direction of in = -5 ( not = 175 )

path g2=(0,4.1)--(4.1,0);
// is equivalent to \draw (0,4.1) to (4.1,0);

picture pic; // Creat a picture named pic
draw(pic,g1,red); // add g1 to pic
unfill(pic,g2--(0,0)--cycle);
/* page 18 (official documentation)
The following routine uses evenodd clipping together with the ^^ operator to unfill a
region:
void unfill(picture pic=currentpicture, path g);
*/
draw(pic,g2,blue); // g2 to pic
add(pic); // add pic to currentpicture

// Find the intersection points of two paths and then connect them.
pair[] inter=intersectionpoints(g1,g2);
draw(inter[0]--inter[1],green);

shipout(bbox(2mm,invisible));

在此輸入影像描述

還有另一種方法使用subpath(第34頁(官方文件)),你可以自己做。

答案2

這是使用 2.0 版本的解決方案spath3庫(於 2021 年 1 月上傳到 CTAN)。此版本引入了在交叉點分割路徑,然後提取元件並將它們拼接在一起的功能。使用它,我們可以在交叉點分割曲線和直線,然後將每個曲線和直線的正確組成部分放在一起來定義突出顯示的路徑。

\documentclass{article}
%\url{https://tex.stackexchange.com/q/569881/86}
\usepackage{tikz}
\usetikzlibrary{intersections, spath3}

\ExplSyntaxOn

\cs_set_eq:NN \getComponentOf \clist_item:Nn

\ExplSyntaxOff

\begin{document}


\begin{tikzpicture}[scale=1.2]
% Axis
\draw [->] (0,0) node [below] {0} -- (0,0) -- (5.5,0) node [below] {Good 1};
\draw [->] (0,0) node [below] {0} -- (0,0) -- (0,5.5) node [above] {Good 2};
% Indifference curve
\draw[spath/save=curve] (0.3,5) to   [out=280,in=175] (5.5,0.5);
\draw[spath/save=line] (0,4.1) to (4.1,0);
\tikzset{
  spath/split at intersections={curve}{line},
  spath/get components of={curve}\ccpts,
  spath/get components of={line}\lcpts,
}

\draw [
  line width=3pt,
  cyan,
  opacity=.5,
  spath/restore=\getComponentOf\ccpts{1}
]
[
  spath/insert=\getComponentOf\lcpts{2},
  spath/insert=\getComponentOf\ccpts{3}
];

\end{tikzpicture}

\end{document}

如果您希望不同的部分具有不同的顏色,那麼您只需將它們放入不同的\draw命令中即可。

曲線和直線在相交處分開

相關內容