summaryrefslogtreecommitdiff
path: root/src/util.cpp
blob: b9c87b20b2738b480df3e1a37cd4e1b8856cd8ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
    TikZiT - a GUI diagram editor for TikZ
    Copyright (C) 2018 Aleks Kissinger

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

#include "util.h"


qreal bezierInterpolate(qreal dist, qreal c0, qreal c1, qreal c2, qreal c3) {
    qreal distp = 1 - dist;
    return  (distp*distp*distp) * c0 +
            3 * (distp*distp) * dist * c1 +
            3 * (dist*dist) * distp * c2 +
            (dist*dist*dist) * c3;
}

QPointF bezierInterpolateFull (qreal dist, QPointF c0, QPointF c1, QPointF c2, QPointF c3) {
    return QPointF(bezierInterpolate (dist, c0.x(), c1.x(), c2.x(), c3.x()),
                   bezierInterpolate (dist, c0.y(), c1.y(), c2.y(), c3.y()));
}


qreal roundToNearest(qreal stepSize, qreal val) {
    if (stepSize==0.0) return val;
    else return round(val/stepSize)*stepSize;
}

qreal radiansToDegrees (qreal radians) {
    return (radians * 180.0) / M_PI;
}

qreal degreesToRadians(qreal degrees) {
    return (degrees * M_PI) / 180.0;
}

int normaliseAngleDeg (int degrees) {
    while (degrees > 180) {
        degrees -= 360;
    }
    while (degrees <= -180) {
        degrees += 360;
    }
    return degrees;
}

qreal normaliseAngleRad (qreal rads) {
    while (rads > M_PI) {
        rads -= 2 * M_PI;
    }
    while (rads <= -M_PI) {
        rads += 2 * M_PI;
    }
    return rads;
}

bool almostZero(qreal f) {
    return (f >= -0.000001 && f <= 0.000001);
}

bool almostEqual(qreal f1, qreal f2) {
    return almostZero(f1 - f2);
}

// convert qreal to string, squashing very small qreals to zero
QString floatToString(qreal f) {
    if (almostZero(f)) return "0";
    else return QString::number(f);
}


static QList<QString> texConstantNames;
static QList<QString> texConstantCodes;
static QList<QString> texModifierNames;


void initTexConstants() {
    texConstantNames
        << "\\alpha"
        << "\\beta"
        << "\\gamma"
        << "\\delta"
        << "\\epsilon"
        << "\\zeta"
        << "\\eta"
        << "\\theta"
        << "\\iota"
        << "\\kappa"
        << "\\lambda"
        << "\\mu"
        << "\\nu"
        << "\\xi"
        << "\\pi"
        << "\\rho"
        << "\\sigma"
        << "\\tau"
        << "\\upsilon"
        << "\\phi"
        << "\\chi"
        << "\\psi"
        << "\\omega"
        << "\\Gamma"
        << "\\Delta"
        << "\\Theta"
        << "\\Lambda"
        << "\\Xi"
        << "\\Pi"
        << "\\Sigma"
        << "\\Upsilon"
        << "\\Phi"
        << "\\Psi"
        << "\\Omega"
        << "\\pm"
        << "\\to"
        << "\\Rightarrow"
        << "\\Leftrightarrow"
        << "\\forall"
        << "\\partial"
        << "\\exists"
        << "\\emptyset"
        << "\\nabla"
        << "\\in"
        << "\\notin"
        << "\\prod"
        << "\\sum"
        << "\\surd"
        << "\\infty"
        << "\\wedge"
        << "\\vee"
        << "\\cap"
        << "\\cup"
        << "\\int"
        << "\\approx"
        << "\\neq"
        << "\\equiv"
        << "\\leq"
        << "\\geq"
        << "\\subset"
        << "\\supset"
        << "\\cdot"
        << "\\ldots";

    texConstantCodes <<
        "\u03b1" << "\u03b2" << "\u03b3" << "\u03b4" << "\u03b5" << "\u03b6" << "\u03b7" <<
        "\u03b8" << "\u03b9" << "\u03ba" << "\u03bb" << "\u03bc" << "\u03bd" << "\u03be" <<
        "\u03c0" << "\u03c1" << "\u03c3" << "\u03c4" << "\u03c5" << "\u03c6" << "\u03c7" <<
        "\u03c8" << "\u03c9" << "\u0393" << "\u0394" << "\u0398" << "\u039b" << "\u039e" <<
        "\u03a0" << "\u03a3" << "\u03a5" << "\u03a6" << "\u03a8" << "\u03a9" <<

        "\u00b1" << "\u2192" << "\u21d2" << "\u21d4" << "\u2200" << "\u2202" << "\u2203" <<
        "\u2205" << "\u2207" << "\u2208" << "\u2209" << "\u220f" << "\u2211" << "\u221a" <<
        "\u221e" << "\u2227" << "\u2228" << "\u2229" << "\u222a" << "\u222b" << "\u2248" <<
        "\u2260" << "\u2261" << "\u2264" << "\u2265" << "\u2282" << "\u2283" << "\u22c5" <<
        "\u2026";

    texModifierNames
        << "\\tiny"
        << "\\scriptsize"
        << "\\footnotesize"
        << "\\small"
        << "\\normalsize"
        << "\\large"
        << "\\Large"
        << "\\LARGE"
        << "\\huge"
        << "\\Huge";
}

QString replaceTexConstants(QString s) {
    QString s1 = s;
    for (int i = 0; i < texConstantNames.length(); ++i) {
        s1 = s1.replace(texConstantNames[i], texConstantCodes[i]);
    }

    for (int i = 0; i < texModifierNames.length(); ++i) {
        s1 = s1.replace(texModifierNames[i], "");
    }

    if (s1.startsWith('$') && s1.endsWith('$')) {
        s1 = s1.mid(1, s1.length()-2);
    }

    return s1;
}