Elements used in the Balls and Things games for the RETRO.
Dependents: RETRO_BallsAndPaddle RETRO_BallAndHoles
Shapes.cpp@4:f421e34313d3, 2015-02-28 (annotated)
- Committer:
- maxint
- Date:
- Sat Feb 28 11:40:24 2015 +0000
- Revision:
- 4:f421e34313d3
- Parent:
- 3:441dc90d10ce
- Child:
- 7:4fa3edaa1201
Added support for walls
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
maxint | 0:3d0db4e183ee | 1 | #include "mbed.h" |
maxint | 0:3d0db4e183ee | 2 | #include "Shapes.h" |
maxint | 0:3d0db4e183ee | 3 | |
maxint | 0:3d0db4e183ee | 4 | |
maxint | 0:3d0db4e183ee | 5 | |
maxint | 0:3d0db4e183ee | 6 | /////////////////// |
maxint | 0:3d0db4e183ee | 7 | // POINT |
maxint | 0:3d0db4e183ee | 8 | /////////////////// |
maxint | 0:3d0db4e183ee | 9 | Point::Point() |
maxint | 0:3d0db4e183ee | 10 | { // constructor |
maxint | 0:3d0db4e183ee | 11 | x1=0; |
maxint | 0:3d0db4e183ee | 12 | y1=0; |
maxint | 0:3d0db4e183ee | 13 | } |
maxint | 0:3d0db4e183ee | 14 | |
maxint | 0:3d0db4e183ee | 15 | Point::Point(int x, int y) |
maxint | 0:3d0db4e183ee | 16 | { // constructor |
maxint | 0:3d0db4e183ee | 17 | x1=x; |
maxint | 0:3d0db4e183ee | 18 | y1=y; |
maxint | 0:3d0db4e183ee | 19 | } |
maxint | 0:3d0db4e183ee | 20 | |
maxint | 0:3d0db4e183ee | 21 | int Point::getX() |
maxint | 0:3d0db4e183ee | 22 | { |
maxint | 0:3d0db4e183ee | 23 | return x1; |
maxint | 0:3d0db4e183ee | 24 | } |
maxint | 0:3d0db4e183ee | 25 | |
maxint | 0:3d0db4e183ee | 26 | int Point::getY() |
maxint | 0:3d0db4e183ee | 27 | { |
maxint | 0:3d0db4e183ee | 28 | return y1; |
maxint | 0:3d0db4e183ee | 29 | } |
maxint | 0:3d0db4e183ee | 30 | |
maxint | 0:3d0db4e183ee | 31 | void Point::set(int x, int y) |
maxint | 0:3d0db4e183ee | 32 | { |
maxint | 0:3d0db4e183ee | 33 | x1=x; |
maxint | 0:3d0db4e183ee | 34 | y1=y; |
maxint | 0:3d0db4e183ee | 35 | } |
maxint | 0:3d0db4e183ee | 36 | |
maxint | 0:3d0db4e183ee | 37 | |
maxint | 0:3d0db4e183ee | 38 | /////////////////// |
maxint | 3:441dc90d10ce | 39 | // LINE |
maxint | 3:441dc90d10ce | 40 | /////////////////// |
maxint | 3:441dc90d10ce | 41 | |
maxint | 3:441dc90d10ce | 42 | Line::Line(int x,int y, int xx, int yy) |
maxint | 3:441dc90d10ce | 43 | { |
maxint | 3:441dc90d10ce | 44 | x1 = x; |
maxint | 3:441dc90d10ce | 45 | x2 = xx; |
maxint | 3:441dc90d10ce | 46 | y1 = y; |
maxint | 3:441dc90d10ce | 47 | y2 = yy; |
maxint | 3:441dc90d10ce | 48 | } |
maxint | 3:441dc90d10ce | 49 | |
maxint | 3:441dc90d10ce | 50 | Line::Line(Point p1, Point p2) |
maxint | 3:441dc90d10ce | 51 | { |
maxint | 3:441dc90d10ce | 52 | x1 = p1.getX(); |
maxint | 3:441dc90d10ce | 53 | x2 = p2.getX(); |
maxint | 3:441dc90d10ce | 54 | y1 = p1.getY(); |
maxint | 3:441dc90d10ce | 55 | y2 = p2.getY(); |
maxint | 3:441dc90d10ce | 56 | } |
maxint | 3:441dc90d10ce | 57 | |
maxint | 3:441dc90d10ce | 58 | Point Line::get1() |
maxint | 3:441dc90d10ce | 59 | { |
maxint | 3:441dc90d10ce | 60 | return(Point(x1, y1)); |
maxint | 3:441dc90d10ce | 61 | } |
maxint | 3:441dc90d10ce | 62 | |
maxint | 3:441dc90d10ce | 63 | Point Line::get2() |
maxint | 3:441dc90d10ce | 64 | { |
maxint | 3:441dc90d10ce | 65 | return(Point(x2, y2)); |
maxint | 3:441dc90d10ce | 66 | } |
maxint | 3:441dc90d10ce | 67 | |
maxint | 3:441dc90d10ce | 68 | |
maxint | 3:441dc90d10ce | 69 | float Line::getSizeFloat() |
maxint | 3:441dc90d10ce | 70 | { // get the size of the vector |
maxint | 3:441dc90d10ce | 71 | return(sqrt((float)(x2-x1)*(float)(x2-x1)+(float)(y2-y1)*(float)(y2-y1))); |
maxint | 3:441dc90d10ce | 72 | } |
maxint | 3:441dc90d10ce | 73 | |
maxint | 3:441dc90d10ce | 74 | int Line::getSize() |
maxint | 3:441dc90d10ce | 75 | { // get the size of the vector |
maxint | 3:441dc90d10ce | 76 | return((int)getSizeFloat()); |
maxint | 3:441dc90d10ce | 77 | } |
maxint | 3:441dc90d10ce | 78 | |
maxint | 3:441dc90d10ce | 79 | Rectangle Line::getBoundingRectangle() |
maxint | 3:441dc90d10ce | 80 | { |
maxint | 3:441dc90d10ce | 81 | return(Rectangle(x1, y1, x2, y2)); |
maxint | 3:441dc90d10ce | 82 | } |
maxint | 3:441dc90d10ce | 83 | |
maxint | 3:441dc90d10ce | 84 | int Line::getDistance(Point pt) |
maxint | 3:441dc90d10ce | 85 | { // get the distance of a line to a point |
maxint | 3:441dc90d10ce | 86 | //TODO |
maxint | 3:441dc90d10ce | 87 | Line ln1(get1(), pt); |
maxint | 3:441dc90d10ce | 88 | Line ln2(get2(), pt); |
maxint | 3:441dc90d10ce | 89 | int nDist1=ln1.getSize(); |
maxint | 3:441dc90d10ce | 90 | int nDist2=ln2.getSize(); |
maxint | 3:441dc90d10ce | 91 | int nDistApprox=nDist1+nDist2-getSize(); |
maxint | 3:441dc90d10ce | 92 | |
maxint | 3:441dc90d10ce | 93 | // simple distance calculation: |
maxint | 3:441dc90d10ce | 94 | // if distance to either line-end combined equal to the line-length, the point is on the line |
maxint | 3:441dc90d10ce | 95 | // if the point is nearby, the difference might be good approximation of the distance |
maxint | 3:441dc90d10ce | 96 | if(nDistApprox<nDist1 && nDistApprox<nDist2) |
maxint | 3:441dc90d10ce | 97 | return(nDistApprox); |
maxint | 3:441dc90d10ce | 98 | |
maxint | 3:441dc90d10ce | 99 | return(MIN(nDist1,nDist2)); |
maxint | 3:441dc90d10ce | 100 | } |
maxint | 3:441dc90d10ce | 101 | |
maxint | 3:441dc90d10ce | 102 | |
maxint | 3:441dc90d10ce | 103 | |
maxint | 3:441dc90d10ce | 104 | |
maxint | 3:441dc90d10ce | 105 | /////////////////// |
maxint | 0:3d0db4e183ee | 106 | // RECTANGLE |
maxint | 0:3d0db4e183ee | 107 | /////////////////// |
maxint | 0:3d0db4e183ee | 108 | |
maxint | 0:3d0db4e183ee | 109 | Rectangle::Rectangle(int x,int y, int xx, int yy) |
maxint | 0:3d0db4e183ee | 110 | { |
maxint | 0:3d0db4e183ee | 111 | x1 = x; |
maxint | 0:3d0db4e183ee | 112 | x2 = xx; |
maxint | 0:3d0db4e183ee | 113 | y1 = y; |
maxint | 0:3d0db4e183ee | 114 | y2 = yy; |
maxint | 0:3d0db4e183ee | 115 | } |
maxint | 0:3d0db4e183ee | 116 | |
maxint | 0:3d0db4e183ee | 117 | Rectangle::Rectangle(Point pt1, Point pt2) |
maxint | 0:3d0db4e183ee | 118 | { |
maxint | 0:3d0db4e183ee | 119 | x1 = pt1.getX(); |
maxint | 0:3d0db4e183ee | 120 | x2 = pt2.getX(); |
maxint | 0:3d0db4e183ee | 121 | y1 = pt1.getY(); |
maxint | 0:3d0db4e183ee | 122 | y2 = pt2.getY(); |
maxint | 0:3d0db4e183ee | 123 | } |
maxint | 0:3d0db4e183ee | 124 | |
maxint | 0:3d0db4e183ee | 125 | |
maxint | 0:3d0db4e183ee | 126 | bool Rectangle::collides(Point pt) |
maxint | 0:3d0db4e183ee | 127 | { |
maxint | 0:3d0db4e183ee | 128 | if(pt.getX() >= x1 && pt.getX() <= x2) { |
maxint | 0:3d0db4e183ee | 129 | if(pt.getY() >= y1 && pt.getY() <= y2) { |
maxint | 0:3d0db4e183ee | 130 | return true; |
maxint | 0:3d0db4e183ee | 131 | } |
maxint | 0:3d0db4e183ee | 132 | } |
maxint | 0:3d0db4e183ee | 133 | return false; |
maxint | 0:3d0db4e183ee | 134 | } |
maxint | 0:3d0db4e183ee | 135 | |
maxint | 0:3d0db4e183ee | 136 | /* |
maxint | 0:3d0db4e183ee | 137 | Rectangle Rectangle::intersection(Rectangle r) |
maxint | 0:3d0db4e183ee | 138 | { // return the intersection of two rectangles or null |
maxint | 0:3d0db4e183ee | 139 | Rectangle rResult; |
maxint | 0:3d0db4e183ee | 140 | rResult=Rectangle() |
maxint | 0:3d0db4e183ee | 141 | if(this->x2 >= |
maxint | 0:3d0db4e183ee | 142 | } |
maxint | 0:3d0db4e183ee | 143 | */ |
maxint | 0:3d0db4e183ee | 144 | |
maxint | 0:3d0db4e183ee | 145 | bool Rectangle::collides(Rectangle r) |
maxint | 0:3d0db4e183ee | 146 | { // check if two rectangles collide |
maxint | 0:3d0db4e183ee | 147 | // method 1: check if corners of eithter rectangle collide |
maxint | 0:3d0db4e183ee | 148 | // method 2: compare sides |
maxint | 0:3d0db4e183ee | 149 | if(this->collides(r.get1()) || this->collides(r.get2())) return(true); |
maxint | 0:3d0db4e183ee | 150 | if(this->collides(r.get3()) || this->collides(r.get4())) return(true); |
maxint | 0:3d0db4e183ee | 151 | if(r.collides(this->get1()) || r.collides(this->get2())) return(true); |
maxint | 0:3d0db4e183ee | 152 | if(r.collides(this->get3()) || r.collides(this->get4())) return(true); |
maxint | 0:3d0db4e183ee | 153 | // TODO: check other corners |
maxint | 0:3d0db4e183ee | 154 | return(false); |
maxint | 0:3d0db4e183ee | 155 | } |
maxint | 0:3d0db4e183ee | 156 | |
maxint | 0:3d0db4e183ee | 157 | |
maxint | 0:3d0db4e183ee | 158 | Point Rectangle::get1() |
maxint | 0:3d0db4e183ee | 159 | { |
maxint | 0:3d0db4e183ee | 160 | return(Point(x1, y1)); |
maxint | 0:3d0db4e183ee | 161 | } |
maxint | 0:3d0db4e183ee | 162 | |
maxint | 0:3d0db4e183ee | 163 | Point Rectangle::get2() |
maxint | 0:3d0db4e183ee | 164 | { |
maxint | 0:3d0db4e183ee | 165 | return(Point(x2, y2)); |
maxint | 0:3d0db4e183ee | 166 | } |
maxint | 0:3d0db4e183ee | 167 | |
maxint | 0:3d0db4e183ee | 168 | Point Rectangle::get3() |
maxint | 0:3d0db4e183ee | 169 | { |
maxint | 0:3d0db4e183ee | 170 | return(Point(x2, y1)); |
maxint | 0:3d0db4e183ee | 171 | } |
maxint | 0:3d0db4e183ee | 172 | |
maxint | 0:3d0db4e183ee | 173 | Point Rectangle::get4() |
maxint | 0:3d0db4e183ee | 174 | { |
maxint | 0:3d0db4e183ee | 175 | return(Point(x1, y2)); |
maxint | 0:3d0db4e183ee | 176 | } |
maxint | 0:3d0db4e183ee | 177 | |
maxint | 0:3d0db4e183ee | 178 | |
maxint | 0:3d0db4e183ee | 179 | |
maxint | 0:3d0db4e183ee | 180 | int Rectangle::getX1() |
maxint | 0:3d0db4e183ee | 181 | { |
maxint | 0:3d0db4e183ee | 182 | return x1; |
maxint | 0:3d0db4e183ee | 183 | } |
maxint | 0:3d0db4e183ee | 184 | |
maxint | 0:3d0db4e183ee | 185 | int Rectangle::getX2() |
maxint | 0:3d0db4e183ee | 186 | { |
maxint | 0:3d0db4e183ee | 187 | return x2; |
maxint | 0:3d0db4e183ee | 188 | } |
maxint | 0:3d0db4e183ee | 189 | |
maxint | 0:3d0db4e183ee | 190 | int Rectangle::getY1() |
maxint | 0:3d0db4e183ee | 191 | { |
maxint | 0:3d0db4e183ee | 192 | return y1; |
maxint | 0:3d0db4e183ee | 193 | } |
maxint | 0:3d0db4e183ee | 194 | |
maxint | 0:3d0db4e183ee | 195 | int Rectangle::getY2() |
maxint | 0:3d0db4e183ee | 196 | { |
maxint | 0:3d0db4e183ee | 197 | return y2; |
maxint | 0:3d0db4e183ee | 198 | } |
maxint | 0:3d0db4e183ee | 199 | |
maxint | 0:3d0db4e183ee | 200 | int Rectangle::getCenterX() |
maxint | 0:3d0db4e183ee | 201 | { |
maxint | 0:3d0db4e183ee | 202 | return x1 + (x2-x1)/2; |
maxint | 0:3d0db4e183ee | 203 | } |
maxint | 0:3d0db4e183ee | 204 | |
maxint | 0:3d0db4e183ee | 205 | int Rectangle::getCenterY() |
maxint | 0:3d0db4e183ee | 206 | { |
maxint | 0:3d0db4e183ee | 207 | return y1 + (y2-y1)/2; |
maxint | 0:3d0db4e183ee | 208 | } |
maxint | 0:3d0db4e183ee | 209 | |
maxint | 0:3d0db4e183ee | 210 | Point Rectangle::getCenter() |
maxint | 0:3d0db4e183ee | 211 | { |
maxint | 0:3d0db4e183ee | 212 | return(Point(x1 + (x2-x1)/2, y1 + (y2-y1)/2)); |
maxint | 0:3d0db4e183ee | 213 | } |
maxint | 0:3d0db4e183ee | 214 | |
maxint | 0:3d0db4e183ee | 215 | void Rectangle::set(Rectangle rNew) |
maxint | 0:3d0db4e183ee | 216 | { |
maxint | 0:3d0db4e183ee | 217 | x1=rNew.getX1(); |
maxint | 0:3d0db4e183ee | 218 | y1=rNew.getY1(); |
maxint | 0:3d0db4e183ee | 219 | x2=rNew.getX2(); |
maxint | 0:3d0db4e183ee | 220 | y2=rNew.getY2(); |
maxint | 0:3d0db4e183ee | 221 | } |
maxint | 0:3d0db4e183ee | 222 | |
maxint | 4:f421e34313d3 | 223 | bool Rectangle::isHorizontal() |
maxint | 4:f421e34313d3 | 224 | { |
maxint | 4:f421e34313d3 | 225 | return(x2-x1 > y2-y1); |
maxint | 4:f421e34313d3 | 226 | } |
maxint | 4:f421e34313d3 | 227 | |
maxint | 4:f421e34313d3 | 228 | |
maxint | 0:3d0db4e183ee | 229 | void Rectangle::move(Vector v) |
maxint | 0:3d0db4e183ee | 230 | { |
maxint | 0:3d0db4e183ee | 231 | x1+=rint(v.x); |
maxint | 0:3d0db4e183ee | 232 | y1+=rint(v.y); |
maxint | 0:3d0db4e183ee | 233 | x2+=rint(v.x); |
maxint | 0:3d0db4e183ee | 234 | y2+=rint(v.y); |
maxint | 0:3d0db4e183ee | 235 | } |
maxint | 0:3d0db4e183ee | 236 | |
maxint | 0:3d0db4e183ee | 237 | |
maxint | 0:3d0db4e183ee | 238 | /////////////////// |
maxint | 0:3d0db4e183ee | 239 | // CIRCLE |
maxint | 0:3d0db4e183ee | 240 | /////////////////// |
maxint | 0:3d0db4e183ee | 241 | Circle::Circle(int x,int y, int r) |
maxint | 0:3d0db4e183ee | 242 | { |
maxint | 0:3d0db4e183ee | 243 | x1=x; |
maxint | 0:3d0db4e183ee | 244 | y1=y; |
maxint | 0:3d0db4e183ee | 245 | r1=r; |
maxint | 0:3d0db4e183ee | 246 | } |
maxint | 0:3d0db4e183ee | 247 | |
maxint | 0:3d0db4e183ee | 248 | Point Circle::getCenter() |
maxint | 0:3d0db4e183ee | 249 | { |
maxint | 0:3d0db4e183ee | 250 | return(Point(x1, y1)); |
maxint | 0:3d0db4e183ee | 251 | } |
maxint | 0:3d0db4e183ee | 252 | |
maxint | 0:3d0db4e183ee | 253 | int Circle::getRadius() |
maxint | 0:3d0db4e183ee | 254 | { |
maxint | 0:3d0db4e183ee | 255 | return(r1); |
maxint | 0:3d0db4e183ee | 256 | } |
maxint | 0:3d0db4e183ee | 257 | |
maxint | 0:3d0db4e183ee | 258 | int Circle::getX() |
maxint | 0:3d0db4e183ee | 259 | { |
maxint | 0:3d0db4e183ee | 260 | return(x1); |
maxint | 0:3d0db4e183ee | 261 | } |
maxint | 0:3d0db4e183ee | 262 | |
maxint | 0:3d0db4e183ee | 263 | int Circle::getY() |
maxint | 0:3d0db4e183ee | 264 | { |
maxint | 0:3d0db4e183ee | 265 | return(y1); |
maxint | 0:3d0db4e183ee | 266 | } |
maxint | 0:3d0db4e183ee | 267 | |
maxint | 0:3d0db4e183ee | 268 | void Circle::setX(int x) |
maxint | 0:3d0db4e183ee | 269 | { |
maxint | 0:3d0db4e183ee | 270 | x1=x; |
maxint | 0:3d0db4e183ee | 271 | } |
maxint | 0:3d0db4e183ee | 272 | |
maxint | 0:3d0db4e183ee | 273 | void Circle::setY(int y) |
maxint | 0:3d0db4e183ee | 274 | { |
maxint | 0:3d0db4e183ee | 275 | y1=y; |
maxint | 0:3d0db4e183ee | 276 | } |
maxint | 0:3d0db4e183ee | 277 | |
maxint | 0:3d0db4e183ee | 278 | void Circle::setXY(int x, int y) |
maxint | 0:3d0db4e183ee | 279 | { |
maxint | 0:3d0db4e183ee | 280 | x1=x; |
maxint | 0:3d0db4e183ee | 281 | y1=y; |
maxint | 0:3d0db4e183ee | 282 | } |
maxint | 0:3d0db4e183ee | 283 | |
maxint | 0:3d0db4e183ee | 284 | void Circle::move(int x, int y) |
maxint | 0:3d0db4e183ee | 285 | { |
maxint | 0:3d0db4e183ee | 286 | x1+=x; |
maxint | 0:3d0db4e183ee | 287 | y1+=y; |
maxint | 0:3d0db4e183ee | 288 | } |
maxint | 0:3d0db4e183ee | 289 | |
maxint | 0:3d0db4e183ee | 290 | void Circle::move(Vector v) |
maxint | 0:3d0db4e183ee | 291 | { |
maxint | 0:3d0db4e183ee | 292 | x1+=rint(v.x); |
maxint | 0:3d0db4e183ee | 293 | y1+=rint(v.y); |
maxint | 0:3d0db4e183ee | 294 | } |
maxint | 0:3d0db4e183ee | 295 | |
maxint | 0:3d0db4e183ee | 296 | |
maxint | 0:3d0db4e183ee | 297 | Rectangle Circle::getBoundingRectangle() |
maxint | 0:3d0db4e183ee | 298 | { |
maxint | 0:3d0db4e183ee | 299 | return(Rectangle(x1-r1, y1-r1, x1+r1, y1+r1)); |
maxint | 0:3d0db4e183ee | 300 | } |
maxint | 3:441dc90d10ce | 301 | |
maxint | 3:441dc90d10ce | 302 | bool Circle::collides(Line ln) |
maxint | 3:441dc90d10ce | 303 | { // see if a circle collides with a line |
maxint | 3:441dc90d10ce | 304 | // check 1: first check using bounding rectangles |
maxint | 3:441dc90d10ce | 305 | // check 2: distance of line-ends to center or circle |
maxint | 3:441dc90d10ce | 306 | // check 3: check if distance to line is smaller than radius |
maxint | 3:441dc90d10ce | 307 | |
maxint | 3:441dc90d10ce | 308 | // Simple check 1: using bounding rectangles |
maxint | 3:441dc90d10ce | 309 | Rectangle rCircle=getBoundingRectangle(); |
maxint | 3:441dc90d10ce | 310 | if(!rCircle.collides(ln.getBoundingRectangle())) |
maxint | 3:441dc90d10ce | 311 | return(false); |
maxint | 3:441dc90d10ce | 312 | |
maxint | 3:441dc90d10ce | 313 | // Simple check 2: distance of line-ends to center or circle |
maxint | 3:441dc90d10ce | 314 | Point ptCenter=getCenter(); |
maxint | 3:441dc90d10ce | 315 | Line lnA(ptCenter, ln.get1()); |
maxint | 3:441dc90d10ce | 316 | if(lnA.getSize()<=r1) |
maxint | 3:441dc90d10ce | 317 | return(true); |
maxint | 3:441dc90d10ce | 318 | Line lnB(ptCenter, ln.get2()); |
maxint | 3:441dc90d10ce | 319 | if(lnB.getSize()<=r1) |
maxint | 3:441dc90d10ce | 320 | return(true); |
maxint | 3:441dc90d10ce | 321 | |
maxint | 3:441dc90d10ce | 322 | // check 3: check if distance to line is smaller than radius |
maxint | 3:441dc90d10ce | 323 | if(ln.getDistance(ptCenter) <= r1) |
maxint | 3:441dc90d10ce | 324 | return(true); |
maxint | 3:441dc90d10ce | 325 | |
maxint | 3:441dc90d10ce | 326 | return(false); |
maxint | 3:441dc90d10ce | 327 | } |