Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I tried this code:

\begin{tikzpicture}
    \draw[semithick,->] (-2,0) -- (2,0) node[right] {$ x_1 $};
    \draw[semithick,->] (0,-2) -- (0,2) node[left] {$ x_2 $};   
    \path[thick,blue,draw] (-2,2) .. controls (0,1) .. (0,0) .. controls (0,-1) .. (2,-2);
    \path[thick,red,rotate=90,draw] (0,0) parabola (-2,-2);
\end{tikzpicture}

and got some curve (blue), but it's not a parabola!

What I've done wrong?

share|improve this question

migrated from stackoverflow.com Feb 10 at 0:47

This question came from our site for professional and enthusiast programmers.

    
Welcome to TeX.sx! Your post was migrated here from Stack Overflow. Please register on this site, too, and make sure that both accounts are associated with each other (by using the same OpenID), otherwise you won't be able to comment on or accept answers or edit your question. –  Werner Feb 10 at 6:10

3 Answers 3

up vote 7 down vote accepted

In general, a Bézier is a cubic (a polynomial at³+bt²+ct+d), but you only want a quadratic (bt²+ct+d so you want a=0).

To force a Bézier A .. controls B and C .. D to be a parabola, you make ABCD a trapezium with AD||BC and with AD=3BC. The point where the parabola is parallel to AD and BC is the intersection of the diagonals, and is 1/4 the way along the diagonals.

So if you make ABCD an isosceles trapezium then the axis of the parabola will be aligned with the axis of the trapezium and the vertex (marked "apex" below - oops) will be at this point of intersection of the diagonals.

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
    \draw [help lines,black!20!white] (0,-3) grid (10,3);
    \draw[blue,dashed] 
        (0,0) coordinate(A)
        -- (3,-3) coordinate (B) 
        -- (4,-2) coordinate (C) 
        -- (3,3) coordinate (D) -- cycle
        (A)--(C) (B)--(D);
    \draw (A) .. controls (B) and (C) .. (D);
    \draw[red,dashed]
        (4,3) coordinate (P)
        -- (6,-1) coordinate(Q)
        -- (8,-1) coordinate(R)
        -- (10,3) coordinate(S) -- cycle (P)--(R) (Q)--(S);
    \draw (P) .. controls (Q) and (R) .. (S);
    \draw (7,0) circle (1mm) node[right,rotate=90]{apex};
    \foreach \lbl/\pos in {A/left,B/below,C/right,D/above,P/above,Q/below,R/below,S/above}
        \draw (\lbl) circle[radius=.5mm] (\lbl) node[\pos]{$\lbl$};
\end{tikzpicture}
\end{document}

enter image description here

FWIW the Bézier curve with identical control points — the curve that isn't a parabola in the original question — is a known curve. For example (1,0) .. controls (0,0) .. (0,1) and the equivalent code (1,0) .. controls (0,0) and (0,0) .. (0,1) give a segment of a superellipse with n=1/3

share|improve this answer
    
"make ABCD a trapezium with AD||BC and with AD=3BC" is what I wanted! Where can I get similar information to use Bezier curve for drawing various curves? –  dm-kiselev Feb 11 at 16:37
    
Generally I need to draw parabola from (0,0) to (2,-2), then plot parabola from (-1,2) via (1,0) to (-1,-2), but draw only the part of it from begin (-1,2) to intersection with first parabola. Also I need arrow on the end of this part to show the direction. –  dm-kiselev Feb 11 at 16:59
    
Regarding "Where can I get similar information.." - I don't know. –  Andrew Kepert Feb 12 at 2:53
    
Regarding the specific diagram, the easiest way to do this is to use the first parabola as a part of a \path[clip] cycle that is active when you draw the second parabola. Look up clipping paths and also \begin{scope}...\end{scope} in pgfmanual.pdf. The hard way to do it is to solve the equations to find the point of intersection, and from that the geometry of the trapezium. There is a TikZ intersections library that also may help, but I haven't used it and AFAIK it doesn't have the nice "cut after" syntax of metapost. –  Andrew Kepert Feb 12 at 2:54

You could also parametrize the curve using

x(t)=t^2
y(t)=t

and then plot it using pgfplots

screenshot

% arara: pdflatex
% !arara: indent: {overwrite: on}
\documentclass{standalone}
\usepackage{pgfplots}

% arrows as stealth fighters
\tikzset{>=stealth}
\begin{document}

\begin{tikzpicture}
    \begin{axis}[
            axis lines=middle,
            axis line style=<->,
            xmin=-5,xmax=5,
            ymin=-5,ymax=5,
            xlabel=$x_1$,
            ylabel=$x_2$,
            xtick=\empty,
            ytick=\empty,
            xticklabels=\empty,
            yticklabels=\empty,
        ]
        \addplot[smooth,thick, blue,-]({x^2},{x});
        \addplot[smooth,thick, red,-]({-x^2},{x});
    \end{axis}
\end{tikzpicture}

\end{document}
share|improve this answer
    
I believe you have done the same thing previously for another question in this site. –  In PSTricks we trust Feb 10 at 6:50
    
@CodeMocker almost certainly :) this site has a lot of duplicates –  cmhughes Feb 10 at 16:04

This is how Bezier curve is drawn. It requires 4 points where (x1,y1) and (x4,y4) are starting and end points while (x2,y2) nand (x3,y3) are auxiliary points, constituting a rectangle-like form and a continuous Bezier curve is plotted within. If only (x2,y2) point is given, it will be repeated for (x3,y3) and the curve will be sharpter, instead of flatter (see the plots below). So the location of (x2,y2) and (x3,y3) do affect the curvature.

(x1,y1) .. controls (x2,y2) and (x3,y3) .. (x4,y4);

enter image description here

The plot on the right is newly added for comparison.

Code

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}
    \draw[semithick,->] (-2,0) -- (2,0) node[right] {$ x_1 $};
    \draw[semithick,->] (0,-2) -- (0,2) node[left]  {$ x_2 $};   
    \path[thick,blue,draw] (-2,2) .. controls (0,1) .. (0,0) .. controls (0,-1) .. (2,-2);
    \path[thick,red,rotate=90,draw] (0,0) parabola (-2,-2);

    \path[thick,blue,draw] (-2,2) .. controls (0,1) .. (0,0) .. controls (0,-1) .. (2,-2);
\end{tikzpicture}
\begin{tikzpicture}
    \draw[semithick,->] (-2,0) -- (2,0) node[right] {$ x_1 $};
    \draw[semithick,->] (0,-2) -- (0,2) node[left]  {$ x_2 $};   
    \path[thick,yellow,draw] (-2,2) .. controls (0.65,1) and (0.65,-1) .. (-2,-2) node[right]{curve 1};
    \path[thick,yellow,draw] (2,2) .. controls (-0.65,1) and (-0.65,-1) .. (2,-2)node[right]{curve 2};
    \path[thick,cyan,draw]   (2,2) node[right]{curve 3} .. controls (-0.65,0) .. (2,-2);
    \path[thick,cyan,draw]   (-2,2)node[right]{curve 4} .. controls (0.65,0) .. (-2,-2);    % curve 3 and 4 are sharper because (x2,y2)=(x3,y3)
\end{tikzpicture}
\end{document}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.