如何在 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命令中。

曲线和直线在相交处分裂

相关内容