Root/coord.h

1/*
2 * coord.h - Coordinate representation and basic operations
3 *
4 * Written 2009, 2010, 2016 by Werner Almesberger
5 * Copyright 2009, 2010, 2016 by Werner Almesberger
6 * Copyright 2016, Erich Heinzle (gEDA additions)
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
14
15#ifndef COORD_H
16#define COORD_H
17
18#include <stdint.h>
19
20
21#define MICRON_UNITS 10
22#define MIL_UNITS (25.4*MICRON_UNITS)
23#define MM_UNITS (1000.0*MICRON_UNITS)
24#define UM_UNITS MICRON_UNITS
25#define KICAD_UNIT (MIL_UNITS/10.0)
26#define GEDA_UNIT (MIL_UNITS/100.0)
27
28#define MIL_IN_MM 0.0254
29#define UM_IN_MM 0.001
30
31
32typedef int32_t unit_type;
33
34
35#define UNIT_ERROR ((unit_type) 1 << (sizeof(unit_type)*8-1))
36
37
38struct coord {
39    unit_type x, y;
40};
41
42
43static inline unit_type mil_to_units(double mil)
44{
45    return mil*MIL_UNITS;
46}
47
48
49static inline unit_type mm_to_units(double mm)
50{
51    return mm*MM_UNITS;
52}
53
54
55static inline unit_type um_to_units(double mm)
56{
57    return mm*UM_UNITS;
58}
59
60
61static inline double units_to_mm(unit_type u)
62{
63    return (double) u/MM_UNITS;
64}
65
66
67static inline double units_to_um(unit_type u)
68{
69    return (double) u/UM_UNITS;
70}
71
72
73static inline double units_to_mil(unit_type u)
74{
75    return (double) u/MIL_UNITS;
76}
77
78
79static inline int units_to_kicad(unit_type u)
80{
81    return (double) u/KICAD_UNIT;
82}
83
84static inline int units_to_geda(unit_type u)
85{
86    return (double) u/GEDA_UNIT;
87}
88
89static inline int coord_eq(struct coord a, struct coord b)
90{
91    return a.x == b.x && a.y == b.y;
92}
93
94
95double mm_to_mil(double mm, int exponent);
96double mil_to_mm(double mil, int exponent);
97double um_to_mm(double um, int exponent);
98
99double units_to_best(unit_type u, int *mm);
100
101struct coord normalize(struct coord v, unit_type len);
102struct coord rotate(struct coord v, double angle);
103struct coord add_vec(struct coord a, struct coord b);
104struct coord sub_vec(struct coord a, struct coord b);
105struct coord neg_vec(struct coord v);
106
107struct coord rotate_r(struct coord c, unit_type r, double angle);
108double theta_vec(struct coord v);
109double theta(struct coord c, struct coord p);
110
111void sort_coord(struct coord *min, struct coord *max);
112
113unit_type dist_point(struct coord a, struct coord b);
114unit_type dist_line(struct coord p, struct coord a, struct coord b);
115unit_type dist_rect(struct coord p, struct coord a, struct coord b);
116int inside_rect(struct coord p, struct coord a, struct coord b);
117unit_type dist_circle(struct coord p, struct coord c, unit_type r);
118
119#endif /* !COORD_H */
120

Archive Download this file

Branches:
master



interactive