Root/coord.h

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

Archive Download this file

Branches:
master



interactive