1 /* RectArea.hpp 2 * Beat / envelope shaper LV2 plugin 3 * 4 * Copyright (C) 2019 by Sven Jähnichen 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 3, or (at your option) 9 * any later version. 10 * 11 * This program 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 this program; if not, write to the Free Software Foundation, 18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 19 */ 20 21 #ifndef BUTILITIES_RECTAREA_HPP_ 22 #define BUTILITIES_RECTAREA_HPP_ 23 24 #include "Point.hpp" 25 26 namespace BUtilities 27 { 28 29 class RectArea 30 { 31 protected: 32 Point p1, p2; 33 34 public: RectArea()35 RectArea () : RectArea (Point (), Point ()) {} RectArea(const double x1,const double y1,const double width,const double height)36 RectArea (const double x1, const double y1, const double width, const double height) : 37 RectArea (Point (x1, y1), Point (x1 + width, y1 + height)) {} RectArea(const Point & p1,const Point & p2)38 RectArea (const Point& p1, const Point& p2) : 39 p1 (Point ((p1.x < p2.x ? p1.x : p2.x), (p1.y < p2.y ? p1.y : p2.y))), 40 p2 (Point ((p1.x > p2.x ? p1.x : p2.x), (p1.y > p2.y ? p1.y : p2.y))) 41 {} 42 getPosition() const43 Point getPosition () const {return p1;} getX() const44 double getX () const {return p1.x;} getY() const45 double getY () const {return p1.y;} 46 getExtends() const47 Point getExtends () const {return Point (p2.x - p1.x, p2.y - p1.y);} getWidth() const48 double getWidth () const {return (p2.x - p1.x);} getHeight() const49 double getHeight () const {return (p2.y - p1.y);} 50 setX(const double x)51 void setX (const double x) {moveTo (x, getY());} setY(const double y)52 void setY (const double y) {moveTo (getX(), y);} moveTo(const double x,const double y)53 void moveTo (const double x, const double y) {moveTo (Point (x, y));} moveTo(const Point & position)54 void moveTo (const Point& position) 55 { 56 p2 = p2 - p1 + position; 57 p1 = position; 58 } 59 setWidth(const double width)60 void setWidth (const double width) {resize (width, getHeight());} setHeight(const double height)61 void setHeight (const double height) {resize (getWidth(), height);} resize(const double width,const double height)62 void resize (const double width, const double height) {resize (Point (width, height));} resize(const Point & extends)63 void resize (const Point& extends) {p2 = p1 + extends;} 64 contains(const Point & p) const65 bool contains (const Point& p) const 66 { 67 return ((p.x > p1.x) && (p.x < p2.x) && (p.y > p1.y) && (p.y < p2.y)); 68 } 69 includes(const RectArea & ra) const70 bool includes (const RectArea& ra) const 71 { 72 return ((ra.p1.x >= p1.x) && (ra.p1.y >= p1.y) && (ra.p2.x <= p2.x) && (ra.p2.y <= p2.y)); 73 } 74 overlaps(const RectArea & ra) const75 bool overlaps (const RectArea& ra) const 76 { 77 return !((ra.p2.x < p1.x) || (ra.p2.y < p1.y) || (ra.p1.x > p2.x) || (ra.p1.y > p2.y)); 78 } 79 extend(const RectArea & ra)80 void extend (const RectArea& ra) 81 { 82 if (*this == RectArea ()) *this = ra; 83 else if (ra != RectArea ()) 84 { 85 p1 = Point ((p1.x < ra.p1.x ? p1.x : ra.p1.x), (p1.y < ra.p1.y ? p1.y : ra.p1.y)); 86 p2 = Point ((p2.x > ra.p2.x ? p2.x : ra.p2.x), (p2.y > ra.p2.y ? p2.y : ra.p2.y)); 87 } 88 } 89 intersect(const RectArea & ra)90 void intersect (const RectArea& ra) 91 { 92 if ((*this == RectArea ()) || (ra == RectArea ()) || (!overlaps (ra))) *this = RectArea (); 93 94 else 95 { 96 double x1 = (ra.p1.x < p1.x ? p1.x : ra.p1.x); 97 double y1 = (ra.p1.y < p1.y ? p1.y : ra.p1.y); 98 double x2 = (ra.p2.x > p2.x ? p2.x : ra.p2.x); 99 double y2 = (ra.p2.y > p2.y ? p2.y : ra.p2.y); 100 p1 = Point (x1, y1); 101 p2 = Point (x2, y2); 102 } 103 } 104 operator +=(const RectArea & rhs)105 RectArea& operator+= (const RectArea& rhs) 106 { 107 this->extend (rhs); 108 return *this; 109 } 110 operator *=(const RectArea & rhs)111 RectArea& operator*= (const RectArea& rhs) 112 { 113 this->intersect (rhs); 114 return *this; 115 } 116 operator ==(const RectArea & lhs,const RectArea & rhs)117 friend bool operator== (const RectArea& lhs, const RectArea& rhs) 118 { 119 return ((lhs.p1 == rhs.p1) && (lhs.p2 == rhs.p2)); 120 } 121 operator !=(const RectArea & lhs,const RectArea & rhs)122 friend bool operator!= (const RectArea& lhs, const RectArea& rhs) {return !(lhs == rhs);} operator +(RectArea lhs,const RectArea & rhs)123 friend RectArea operator+ (RectArea lhs, const RectArea& rhs) {return (lhs += rhs);} operator *(RectArea lhs,const RectArea & rhs)124 friend RectArea operator* (RectArea lhs, const RectArea& rhs) {return (lhs *= rhs);} 125 126 }; 127 128 } 129 130 #endif /* BUTILITIES_RECTAREA_HPP_ */ 131