[421b7d2] | 1 | /* useful.h |
---|
[d1b1380] | 2 | * Lots of oddments that come in handy generally |
---|
[1dfd718] | 3 | * Copyright (C) 1993-2003,2004,2010,2011,2014,2025 Olly Betts |
---|
[846746e] | 4 | * |
---|
[89231c4] | 5 | * This program is free software; you can redistribute it and/or modify |
---|
| 6 | * it under the terms of the GNU General Public License as published by |
---|
| 7 | * the Free Software Foundation; either version 2 of the License, or |
---|
| 8 | * (at your option) any later version. |
---|
[846746e] | 9 | * |
---|
| 10 | * This program is distributed in the hope that it will be useful, |
---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
[89231c4] | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 13 | * GNU General Public License for more details. |
---|
[846746e] | 14 | * |
---|
[89231c4] | 15 | * You should have received a copy of the GNU General Public License |
---|
| 16 | * along with this program; if not, write to the Free Software |
---|
[ecbc6c18] | 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
[d1b1380] | 18 | */ |
---|
| 19 | |
---|
| 20 | /* only include once */ |
---|
| 21 | #ifndef USEFUL_H |
---|
| 22 | #define USEFUL_H |
---|
| 23 | |
---|
[04078a7] | 24 | #ifndef PACKAGE |
---|
| 25 | # error config.h must be included first in each C/C++ source file |
---|
| 26 | #endif |
---|
[fa42426] | 27 | |
---|
[d1b1380] | 28 | #include <stdio.h> |
---|
| 29 | #include <math.h> |
---|
| 30 | |
---|
[1ec8db0] | 31 | /* M_PI, etc may be defined in math.h */ |
---|
[bca0071] | 32 | #ifndef M_PI |
---|
[aec56a0] | 33 | # define M_PI 3.14159265358979323846264338327950288419716939937510582097494459 |
---|
[bca0071] | 34 | #endif |
---|
[bceebf4] | 35 | #ifndef M_PI_2 |
---|
| 36 | # define M_PI_2 (M_PI / 2.0) |
---|
| 37 | #endif |
---|
| 38 | #ifndef M_PI_4 |
---|
| 39 | # define M_PI_4 (M_PI / 4.0) |
---|
| 40 | #endif |
---|
[d1b1380] | 41 | |
---|
[a420b49] | 42 | #define MM_PER_INCH 25.4 /* exact value */ |
---|
[d1b1380] | 43 | #define METRES_PER_FOOT 0.3048 /* exact value */ |
---|
[bfe0bc01] | 44 | #define POINTS_PER_INCH 72.0 |
---|
| 45 | #define POINTS_PER_MM (POINTS_PER_INCH / MM_PER_INCH) |
---|
[d1b1380] | 46 | |
---|
| 47 | #define putnl() putchar('\n') /* print a newline char */ |
---|
[e02a6a6] | 48 | #define fputnl(FH) PUTC('\n', (FH)) /* print a newline char to a file */ |
---|
[d1b1380] | 49 | /* print a line followed by a newline char to a file */ |
---|
[1dfd718] | 50 | #define fputsnl(SZ, FH) do { fputs((SZ), (FH)); PUTC('\n', (FH)); } while (0) |
---|
[a420b49] | 51 | #define sqrd(X) ((X) * (X)) /* macro to square things */ |
---|
[27b8b59] | 52 | |
---|
| 53 | /* 2D Euclidean distance */ |
---|
| 54 | #ifndef HAVE_HYPOT |
---|
| 55 | # define hypot(X, Y) sqrt(sqrd((double)(X)) + sqrd((double)(Y))) |
---|
| 56 | #endif |
---|
[bca0071] | 57 | #define rad(X) ((M_PI / 180.0) * (X)) /* convert from degrees to radians */ |
---|
| 58 | #define deg(X) ((180.0 / M_PI) * (X)) /* convert from radians to degrees */ |
---|
[d1b1380] | 59 | |
---|
[bd1913f] | 60 | /* macro to convert argument to a string literal */ |
---|
[fa42426] | 61 | #define STRING(X) STRING_(X) |
---|
| 62 | #define STRING_(X) #X |
---|
[d1b1380] | 63 | |
---|
| 64 | #endif /* !USEFUL_H */ |
---|