/src/h3/src/h3lib/lib/vec2d.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2016-2017 Uber Technologies, Inc. |
3 | | * |
4 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | | * you may not use this file except in compliance with the License. |
6 | | * You may obtain a copy of the License at |
7 | | * |
8 | | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | * |
10 | | * Unless required by applicable law or agreed to in writing, software |
11 | | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | * See the License for the specific language governing permissions and |
14 | | * limitations under the License. |
15 | | */ |
16 | | /** @file vec2d.c |
17 | | * @brief 2D floating point vector functions. |
18 | | */ |
19 | | |
20 | | #include "vec2d.h" |
21 | | |
22 | | #include <float.h> |
23 | | #include <math.h> |
24 | | #include <stdio.h> |
25 | | |
26 | | /** |
27 | | * Calculates the magnitude of a 2D cartesian vector. |
28 | | * @param v The 2D cartesian vector. |
29 | | * @return The magnitude of the vector. |
30 | | */ |
31 | 44.0M | double _v2dMag(const Vec2d *v) { return sqrt(v->x * v->x + v->y * v->y); } |
32 | | |
33 | | /** |
34 | | * Finds the intersection between two lines. Assumes that the lines intersect |
35 | | * and that the intersection is not at an endpoint of either line. |
36 | | * @param p0 The first endpoint of the first line. |
37 | | * @param p1 The second endpoint of the first line. |
38 | | * @param p2 The first endpoint of the second line. |
39 | | * @param p3 The second endpoint of the second line. |
40 | | * @param inter The intersection point. |
41 | | */ |
42 | | void _v2dIntersect(const Vec2d *p0, const Vec2d *p1, const Vec2d *p2, |
43 | 258k | const Vec2d *p3, Vec2d *inter) { |
44 | 258k | Vec2d s1, s2; |
45 | 258k | s1.x = p1->x - p0->x; |
46 | 258k | s1.y = p1->y - p0->y; |
47 | 258k | s2.x = p3->x - p2->x; |
48 | 258k | s2.y = p3->y - p2->y; |
49 | | |
50 | 258k | double t; |
51 | 258k | t = (s2.x * (p0->y - p2->y) - s2.y * (p0->x - p2->x)) / |
52 | 258k | (-s2.x * s1.y + s1.x * s2.y); |
53 | | |
54 | 258k | inter->x = p0->x + (t * s1.x); |
55 | 258k | inter->y = p0->y + (t * s1.y); |
56 | 258k | } |
57 | | |
58 | | /** |
59 | | * Whether two 2D vectors are almost equal, within some threshold |
60 | | * @param v1 First vector to compare |
61 | | * @param v2 Second vector to compare |
62 | | * @return Whether the vectors are almost equal |
63 | | */ |
64 | 0 | bool _v2dAlmostEquals(const Vec2d *v1, const Vec2d *v2) { |
65 | 0 | return fabs(v1->x - v2->x) < FLT_EPSILON && |
66 | 0 | fabs(v1->y - v2->y) < FLT_EPSILON; |
67 | 0 | } |