1 /*
2 	Copyright (c) 2013-2014, 2016-2020 Cong Xu
3 	All rights reserved.
4 
5 	Redistribution and use in source and binary forms, with or without
6 	modification, are permitted provided that the following conditions are met:
7 
8 	Redistributions of source code must retain the above copyright notice, this
9 	list of conditions and the following disclaimer.
10 	Redistributions in binary form must reproduce the above copyright notice,
11 	this list of conditions and the following disclaimer in the documentation
12 	and/or other materials provided with the distribution.
13 
14 	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 	AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 	IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 	ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18 	LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 	CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 	POSSIBILITY OF SUCH DAMAGE.
25 */
26 #include "vector.h"
27 
28 #include <math.h>
29 
30 #include "tile.h"
31 
svec2i_scale_divide(const struct vec2i v,const mint_t scale)32 struct vec2i svec2i_scale_divide(const struct vec2i v, const mint_t scale)
33 {
34 	return svec2i(v.x / scale, v.y / scale);
35 }
36 
Vec2iToTile(struct vec2i v)37 struct vec2i Vec2iToTile(struct vec2i v)
38 {
39 	return svec2i(v.x / TILE_WIDTH, v.y / TILE_HEIGHT);
40 }
41 
Vec2iCenterOfTile(struct vec2i v)42 struct vec2i Vec2iCenterOfTile(struct vec2i v)
43 {
44 	return svec2i(
45 		v.x * TILE_WIDTH + TILE_WIDTH / 2,
46 		v.y * TILE_HEIGHT + TILE_HEIGHT / 2);
47 }
48 
Vec2ToTile(const struct vec2 v)49 struct vec2i Vec2ToTile(const struct vec2 v)
50 {
51 	return svec2i((int)(v.x / TILE_WIDTH), (int)(v.y / TILE_HEIGHT));
52 }
53 
Vec2CenterOfTile(const struct vec2i v)54 struct vec2 Vec2CenterOfTile(const struct vec2i v)
55 {
56 	return svec2_assign_vec2i(Vec2iCenterOfTile(v));
57 }
58 
Rect2iNew(const struct vec2i pos,const struct vec2i size)59 Rect2i Rect2iNew(const struct vec2i pos, const struct vec2i size)
60 {
61 	Rect2i r;
62 	r.Pos = pos;
63 	r.Size = size;
64 	return r;
65 }
Rect2iZero(void)66 Rect2i Rect2iZero(void)
67 {
68 	return Rect2iNew(svec2i_zero(), svec2i_zero());
69 }
70 
Rect2iIsZero(const Rect2i r)71 bool Rect2iIsZero(const Rect2i r)
72 {
73 	return svec2i_is_zero(r.Pos) && svec2i_is_zero(r.Size);
74 }
75 
Rect2iIsAtEdge(const Rect2i r,const struct vec2i v)76 bool Rect2iIsAtEdge(const Rect2i r, const struct vec2i v)
77 {
78 	return v.y == r.Pos.y || v.y == r.Pos.y + r.Size.y - 1 || v.x == r.Pos.x ||
79 		   v.x == r.Pos.x + r.Size.x - 1;
80 }
81 
Rect2iIsInside(const Rect2i r,const struct vec2i v)82 bool Rect2iIsInside(const Rect2i r, const struct vec2i v)
83 {
84 	return !(
85 		v.y < r.Pos.y || v.y > r.Pos.y + r.Size.y - 1 || v.x < r.Pos.x ||
86 		v.x > r.Pos.x + r.Size.x - 1);
87 }
88 
Rect2iOverlap(const Rect2i r1,const Rect2i r2)89 bool Rect2iOverlap(const Rect2i r1, const Rect2i r2)
90 {
91 	return r1.Pos.x < r2.Pos.x + r2.Size.x &&
92 		   r1.Pos.x + r1.Size.x > r2.Pos.x &&
93 		   r1.Pos.y < r2.Pos.y + r2.Size.y && r1.Pos.y + r1.Size.y > r2.Pos.y;
94 }
95 
Rect2iCenter(const Rect2i r)96 struct vec2i Rect2iCenter(const Rect2i r)
97 {
98 	return svec2i_add(r.Pos, svec2i_scale_divide(r.Size, 2));
99 }
100