1 | /* graphene-line-segment-private.h: Segments |
2 | * |
3 | * SPDX-License-Identifier: MIT |
4 | * |
5 | * Copyright 2014 Emmanuele Bassi |
6 | * |
7 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
8 | * of this software and associated documentation files (the "Software"), to deal |
9 | * in the Software without restriction, including without limitation the rights |
10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
11 | * copies of the Software, and to permit persons to whom the Software is |
12 | * furnished to do so, subject to the following conditions: |
13 | * |
14 | * The above copyright notice and this permission notice shall be included in |
15 | * all copies or substantial portions of the Software. |
16 | * |
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
23 | * THE SOFTWARE. |
24 | */ |
25 | |
26 | #pragma once |
27 | |
28 | #include "graphene-point.h" |
29 | |
30 | typedef struct { |
31 | graphene_point_t start; |
32 | graphene_point_t end; |
33 | } graphene_line_segment_t; |
34 | |
35 | static inline graphene_line_segment_t |
36 | graphene_line_segment_init (const graphene_point_t *start, |
37 | const graphene_point_t *end) |
38 | { |
39 | graphene_line_segment_t l; |
40 | l.start = *start; |
41 | l.end = *end; |
42 | |
43 | return l; |
44 | } |
45 | |
46 | static inline bool |
47 | graphene_line_segment_points_on_same_side (graphene_line_segment_t s, |
48 | const graphene_point_t *a, |
49 | const graphene_point_t *b) |
50 | { |
51 | const float delta_x = (s.end.x - s.start.x); |
52 | const float delta_y = (s.end.y - s.start.y); |
53 | |
54 | const float one = delta_x * (a->y - s.start.y) - delta_y * (a->x - s.start.x); |
55 | const float two = delta_x * (b->y - s.start.y) - delta_y * (b->x - s.start.x); |
56 | |
57 | if ((one >= 0.f && two >= 0.f) || (one <= 0.f && two <= 0.f)) |
58 | return true; |
59 | |
60 | return false; |
61 | } |
62 | |