GDS-Render v1.2.1
vector-operations.c
Go to the documentation of this file.
1/*
2 * GDSII-Converter
3 * Copyright (C) 2018 Mario Hüttel <mario.huettel@gmx.net>
4 *
5 * This file is part of GDSII-Converter.
6 *
7 * GDSII-Converter is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * GDSII-Converter is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with GDSII-Converter. If not, see <http://www.gnu.org/licenses/>.
18 */
19
31#include <math.h>
32#include <stdlib.h>
33
35
36#define ABS_DBL(a) ((a) < 0.0 ? -(a) : (a))
37
38double vector_2d_scalar_multipy(struct vector_2d *a, struct vector_2d *b)
39{
40 if (a && b)
41 return (a->x * b->x) + (a->y * b->y);
42 else
43 return 0.0;
44}
45
47{
48 double len;
49
50 if (!vec)
51 return;
52 len = sqrt(pow(vec->x, 2) + pow(vec->y, 2));
53 vec->x = vec->x/len;
54 vec->y = vec->y/len;
55}
56
57void vector_2d_rotate(struct vector_2d *vec, double angle)
58{
59 double sin_val, cos_val;
60 struct vector_2d temp;
61
62 if (!vec)
63 return;
64
65 sin_val = sin(angle);
66 cos_val = cos(angle);
67
68 (void)vector_2d_copy(&temp, vec);
69
70 /* Apply rotation matrix */
71 vec->x = (cos_val * temp.x) - (sin_val * temp.y);
72 vec->y = (sin_val * temp.x) + (cos_val * temp.y);
73}
74
75struct vector_2d *vector_2d_copy(struct vector_2d *opt_res, struct vector_2d *vec)
76{
77 struct vector_2d *res;
78
79 if (!vec)
80 return NULL;
81
82 if (opt_res)
83 res = opt_res;
84 else
85 res = vector_2d_alloc();
86
87 if (res) {
88 res->x = vec->x;
89 res->y = vec->y;
90 }
91 return res;
92}
93
95{
96 return (struct vector_2d *)malloc(sizeof(struct vector_2d));
97}
98
99void vector_2d_free(struct vector_2d *vec)
100{
101 if (vec)
102 free(vec);
103}
104
105void vector_2d_scale(struct vector_2d *vec, double scale)
106{
107 if (!vec)
108 return;
109
110 vec->x *= scale;
111 vec->y *= scale;
112}
113
114double vector_2d_abs(struct vector_2d *vec)
115{
116 double len = 0.0;
117
118 if (vec)
119 len = sqrt(pow(vec->x, 2) + pow(vec->y, 2));
120 return len;
121}
122
124{
125 double cos_angle;
126
127 if (!a || !b)
128 return 0.0;
129
130 cos_angle = ABS_DBL(vector_2d_scalar_multipy(a, b)) / (vector_2d_abs(a) * vector_2d_abs(b));
131 return acos(cos_angle);
132}
133
134void vector_2d_subtract(struct vector_2d *res, struct vector_2d *a, struct vector_2d *b)
135{
136 if (res && a && b) {
137 res->x = a->x - b->x;
138 res->y = a->y - b->y;
139 }
140}
141
142void vector_2d_add(struct vector_2d *res, struct vector_2d *a, struct vector_2d *b)
143{
144 if (res && a && b) {
145 res->x = a->x + b->x;
146 res->y = a->y + b->y;
147 }
148}
149
void vector_2d_scale(struct vector_2d *vec, double scale)
struct vector_2d * vector_2d_alloc(void)
void vector_2d_rotate(struct vector_2d *vec, double angle)
struct vector_2d * vector_2d_copy(struct vector_2d *opt_res, struct vector_2d *vec)
void vector_2d_normalize(struct vector_2d *vec)
double vector_2d_scalar_multipy(struct vector_2d *a, struct vector_2d *b)
double vector_2d_calculate_angle_between(struct vector_2d *a, struct vector_2d *b)
void vector_2d_subtract(struct vector_2d *res, struct vector_2d *a, struct vector_2d *b)
double vector_2d_abs(struct vector_2d *vec)
void vector_2d_add(struct vector_2d *res, struct vector_2d *a, struct vector_2d *b)
void vector_2d_free(struct vector_2d *vec)
#define ABS_DBL(a)
Header for 2D Vector operations.