%Currently only quadratic Bezier curves are supported.
%
%Usage:
%
%\input{"makeBezier.pretex"}
%\newLocalFunc{bezierY1}{... \return{...}}[1]   - first function
%\newLocalFunc{bezierDY1}{... \return{...}}[1]  - first function derivative
%\newLocalFunc{bezierY2}{... \return{...}}[1]   - second function
%\newLocalFunc{bezierDY2}{... \return{...}}[1]  - second function derivative
%\bezierSet{...}                                - setting Xi values (no need to specify indexes)
%...
%\makeBezierQ{k1}{k2}                           - returns generated text
%\printBezierQ{k1}{k2}                          - inserts generated text into document
%
%Maximum Xi rray size is 100.
%To override, use \newVar{bezierArraySize}[...] function before \input command to specify required size.

\tryNewVar{bezierArraySize}[100]
\newArray{_bezierArray}{\bezierArraySize{}}
\newVar{_bezierCounter}[0]

\newLocalFunc{bezierSet}{
    \if{\>={\_bezierCounter{}}{\bezierArraySize{}}}{
        \showMessage{"Array index out of range"}["warning"]["Warning"]
        \return{}
    }
    \set{_bezierArray}{\toReal{#0}}[\_bezierCounter{}]
    \set{_bezierCounter}{\+{\_bezierCounter{}}{1}}
}[1]

\newLocalFunc{_bezierY}{
    \newVar{x}[\_bezierArray{#0}]
    \if{\>={#0}{\-{\divide{\_bezierCounter{}}{2}}{0}}}{
        \return{\bezierY2{\x{}}}
    }[
        \return{\bezierY1{\x{}}}
    ]
}[1]

\newLocalFunc{_bezierDY}{
    \newVar{x}[\_bezierArray{#0}]
    \if{\>={#0}{\-{\divide{\_bezierCounter{}}{2}}{0}}}{
        \return{\bezierDY2{\x{}}}
    }[
        \return{\bezierDY1{\x{}}}
    ]
}[1]

\newLocalFunc{_bezierXi}{
    \newVar{i}[#0]
    \newVar{i1}[\+{\i{}}{1}]
    \newVar{x0}[\_bezierArray{\i{}}]
    \newVar{x1}[\if{\<{\i1{}}{\_bezierCounter{}}}{\_bezierArray{\i1{}}}[0.0]]
    \return{\divide{
        \+{
            \-{
                \*{
                    \x0{}
                }{
                    \_bezierDY{\i{}}
                }
            }{
                \*{
                    \x1{}
                }{
                    \_bezierDY{\i1{}}
                }
            }[
                \_bezierY{\i{}}
            ]
        }{
            \_bezierY{\i1{}}
        }
    }{
        \-{
            \_bezierDY{\i{}}
        }{
            \_bezierDY{\i1{}}
        }
    }}
}[1]

\newLocalFunc{_bezierYi}{
    \newVar{i}[#0]
    \newVar{i1}[\+{\i{}}{1}]
    \newVar{x0}[\_bezierArray{\i{}}]
    \newVar{x1}[\if{\<{\i1{}}{\_bezierCounter{}}}{\_bezierArray{\i1{}}}[0.0]]
    \return{\divide{
        \+{
            \*{
                \-{
                    \x0{}
                }{
                    \x1{}
                }
            }{
                \_bezierDY{\i{}}
            }[
                \_bezierDY{\i1{}}
            ]
        }{
            \-{
                \*{
                    \_bezierDY{\i{}}
                }{
                    \_bezierY{\i1{}}
                }
            }{
                \*{
                    \_bezierDY{\i1{}}
                }{
                    \_bezierY{\i{}}
                }
            }
        }
    }{
        \-{
            \_bezierDY{\i{}}
        }{
            \_bezierDY{\i1{}}
        }
    }}
}[1]

\newLocalFunc{makeBezierQ}{
    \if{\not{
        \and{
            \isDefined{bezierY1}
        }{
            \isDefined{bezierY2}
        }[
            \isDefined{bezierDY1}
        ][
            \isDefined{bezierDY2}
        ]
    }}{
        \showMessage{"One of the functions is not defined"}["critical"]["Error"]
        \return{}
    }
    \if{\<{\_bezierCounter{}}{4}}{
        \showMessage{"Not enough variables"}["critical"]["Error"]
        \return{}
    }
    \if{\notEqual{\modulo{\_bezierCounter{}}{2}}{0}}{
        \showMessage{"Incorrect variable count"}["critical"]["Error"]
        \return{}
    }
    \newVar{k1}[\toReal{#0}]
    \newVar{k2}[\toReal{#1}]
    \if{\<={\k1{}}{0}}{\set{k1}{1.0}}
    \if{\<={\k2{}}{0}}{\set{k2}{1.0}}
    \newVar{text}
    \for{i}{0}{\-{\_bezierCounter{}}{1}}{1}{
        \newVar{i1}[\+{\i{}}{1}]
        \newVar{x1}[\_bezierArray{\i{}}]
        \newVar{x2}[\if{\<{\i1{}}{\_bezierCounter{}}}{\_bezierXi{\i{}}}[0.0]]
        \newVar{x3}[\if{\<{\i1{}}{\_bezierCounter{}}}{\_bezierArray{\i1{}}}[0.0]]
        \newVar{y1}[\_bezierY{\i{}}]
        \newVar{y2}[\if{\<{\i1{}}{\_bezierCounter{}}}{\_bezierYi{\i{}}}[0.0]]
        \newVar{y3}[\if{\<{\i1{}}{\_bezierCounter{}}}{\_bezierY{\i1{}}}[0.0]]
        \newVar{k1x1}[\*{\x1{}}{\k1{}}]
        \newVar{k1x2}[\*{\x2{}}{\k1{}}]
        \newVar{k1x3}[\*{\x3{}}{\k1{}}]
        \newVar{k2y1}[\*{\y1{}}{\k2{}}]
        \newVar{k2y2}[\*{\y2{}}{\k2{}}]
        \newVar{k2y3}[\*{\y3{}}{\k2{}}]
        \set{text}{\+{\text{}}{"\\put(000.00,000.00){\\qbezier("}}
        \set{text}{\+{\text{}}{\format{\k1x1{}}{"7.f.2"}}}
        \set{text}{\+{\text{}}{","}}
        \set{text}{\+{\text{}}{\format{\k2y1{}}{"7.f.2"}}}
        \set{text}{\+{\text{}}{")("}}
        \set{text}{\+{\text{}}{\format{\k1x2{}}{"7.f.2"}}}
        \set{text}{\+{\text{}}{","}}
        \set{text}{\+{\text{}}{\format{\k2y2{}}{"7.f.2"}}}
        \set{text}{\+{\text{}}{")("}}
        \set{text}{\+{\text{}}{\format{\k1x3{}}{"7.f.2"}}}
        \set{text}{\+{\text{}}{","}}
        \set{text}{\+{\text{}}{\format{\k2y3{}}{"7.f.2"}}}
        \set{text}{\+{\text{}}{")}\n"}}
    }
    \return{\text{}}
}[2]

\newLocalFunc{printBezierQ}{
    \insert{\makeBezierQ{#0}{#1}}
}[2]