1 /** 2 Copyright: Copyright (c) 2016-2017 Andrey Penechko. 3 License: $(WEB boost.org/LICENSE_1_0.txt, Boost License 1.0). 4 Authors: Andrey Penechko. 5 */ 6 module voxelman.geometry.rect; 7 8 //import std.algorithm : alg_min = min, alg_max = max; 9 import voxelman.math; 10 11 struct Rect 12 { 13 ivec2 position; 14 ivec2 size; 15 16 int area() @property const @nogc 17 { 18 return size.x * size.y; 19 } 20 21 ivec2 endPosition() @property const 22 { 23 return position + size - ivec2(1,1); 24 } 25 26 bool empty() const @property 27 { 28 return size.x == 0 && size.y == 0; 29 } 30 31 bool contains(ivec2 point) const 32 { 33 if (point.x < position.x || point.x >= position.x + size.x) return false; 34 if (point.y < position.y || point.y >= position.y + size.y) return false; 35 return true; 36 } 37 38 /// Adds a point to a rectangle. 39 /// This results in the smallest rectangle that contains both the rectangle and the point. 40 void add(ivec2 point) 41 { 42 if (point.x < position.x) 43 { 44 immutable int diff_x = position.x - point.x; 45 size.x += diff_x; 46 position.x -= diff_x; 47 } 48 else if(point.x > position.x + size.x - 1) 49 { 50 immutable int diff_x = point.x - (position.x + size.x - 1); 51 size.x += diff_x; 52 } 53 54 if (point.y < position.y) 55 { 56 immutable int diff_y = position.y - point.y; 57 size.y += diff_y; 58 position.y -= diff_y; 59 } 60 else if(point.y > position.y + size.y - 1) 61 { 62 immutable int diff_y = point.y - (position.y + size.y - 1); 63 size.y += diff_y; 64 } 65 } 66 }