Elements used in the Balls and Things games for the RETRO.
Dependents: RETRO_BallsAndPaddle RETRO_BallAndHoles
Shapes.cpp@8:19dd2a538cbe, 2015-03-02 (annotated)
- Committer:
- maxint
- Date:
- Mon Mar 02 09:58:53 2015 +0000
- Revision:
- 8:19dd2a538cbe
- Parent:
- 7:4fa3edaa1201
more clean-up
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 | 7:4fa3edaa1201 | 86 | // TODO: more precise calculation, currently it's a kind-of lame approximation |
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 | 8:19dd2a538cbe | 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 | bool Rectangle::collides(Point pt) |
maxint | 0:3d0db4e183ee | 126 | { |
maxint | 0:3d0db4e183ee | 127 | if(pt.getX() >= x1 && pt.getX() <= x2) { |
maxint | 0:3d0db4e183ee | 128 | if(pt.getY() >= y1 && pt.getY() <= y2) { |
maxint | 0:3d0db4e183ee | 129 | return true; |
maxint | 0:3d0db4e183ee | 130 | } |
maxint | 0:3d0db4e183ee | 131 | } |
maxint | 0:3d0db4e183ee | 132 | return false; |
maxint | 0:3d0db4e183ee | 133 | } |
maxint | 0:3d0db4e183ee | 134 | |
maxint | 0:3d0db4e183ee | 135 | bool Rectangle::collides(Rectangle r) |
maxint | 0:3d0db4e183ee | 136 | { // check if two rectangles collide |
maxint | 7:4fa3edaa1201 | 137 | // method: compare rectangle sides |
maxint | 7:4fa3edaa1201 | 138 | // see http://stackoverflow.com/questions/7610129/simple-collision-detection-android |
maxint | 7:4fa3edaa1201 | 139 | if ((getX2() < r.getX1()) || // A is to the left of B |
maxint | 7:4fa3edaa1201 | 140 | (r.getX2() < getX1()) || // B is to the left of A |
maxint | 7:4fa3edaa1201 | 141 | (getY2() < r.getY1()) || // A is above B |
maxint | 7:4fa3edaa1201 | 142 | (r.getY2() < getY1())) // B is above A |
maxint | 7:4fa3edaa1201 | 143 | return(false); |
maxint | 7:4fa3edaa1201 | 144 | return(true); |
maxint | 0:3d0db4e183ee | 145 | } |
maxint | 0:3d0db4e183ee | 146 | |
maxint | 0:3d0db4e183ee | 147 | Point Rectangle::get1() |
maxint | 0:3d0db4e183ee | 148 | { |
maxint | 0:3d0db4e183ee | 149 | return(Point(x1, y1)); |
maxint | 0:3d0db4e183ee | 150 | } |
maxint | 0:3d0db4e183ee | 151 | |
maxint | 0:3d0db4e183ee | 152 | Point Rectangle::get2() |
maxint | 0:3d0db4e183ee | 153 | { |
maxint | 0:3d0db4e183ee | 154 | return(Point(x2, y2)); |
maxint | 0:3d0db4e183ee | 155 | } |
maxint | 0:3d0db4e183ee | 156 | |
maxint | 0:3d0db4e183ee | 157 | Point Rectangle::get3() |
maxint | 0:3d0db4e183ee | 158 | { |
maxint | 0:3d0db4e183ee | 159 | return(Point(x2, y1)); |
maxint | 0:3d0db4e183ee | 160 | } |
maxint | 0:3d0db4e183ee | 161 | |
maxint | 0:3d0db4e183ee | 162 | Point Rectangle::get4() |
maxint | 0:3d0db4e183ee | 163 | { |
maxint | 0:3d0db4e183ee | 164 | return(Point(x1, y2)); |
maxint | 0:3d0db4e183ee | 165 | } |
maxint | 0:3d0db4e183ee | 166 | |
maxint | 0:3d0db4e183ee | 167 | |
maxint | 0:3d0db4e183ee | 168 | |
maxint | 0:3d0db4e183ee | 169 | int Rectangle::getX1() |
maxint | 0:3d0db4e183ee | 170 | { |
maxint | 0:3d0db4e183ee | 171 | return x1; |
maxint | 0:3d0db4e183ee | 172 | } |
maxint | 0:3d0db4e183ee | 173 | |
maxint | 0:3d0db4e183ee | 174 | int Rectangle::getX2() |
maxint | 0:3d0db4e183ee | 175 | { |
maxint | 0:3d0db4e183ee | 176 | return x2; |
maxint | 0:3d0db4e183ee | 177 | } |
maxint | 0:3d0db4e183ee | 178 | |
maxint | 0:3d0db4e183ee | 179 | int Rectangle::getY1() |
maxint | 0:3d0db4e183ee | 180 | { |
maxint | 0:3d0db4e183ee | 181 | return y1; |
maxint | 0:3d0db4e183ee | 182 | } |
maxint | 0:3d0db4e183ee | 183 | |
maxint | 0:3d0db4e183ee | 184 | int Rectangle::getY2() |
maxint | 0:3d0db4e183ee | 185 | { |
maxint | 0:3d0db4e183ee | 186 | return y2; |
maxint | 0:3d0db4e183ee | 187 | } |
maxint | 0:3d0db4e183ee | 188 | |
maxint | 0:3d0db4e183ee | 189 | int Rectangle::getCenterX() |
maxint | 0:3d0db4e183ee | 190 | { |
maxint | 0:3d0db4e183ee | 191 | return x1 + (x2-x1)/2; |
maxint | 0:3d0db4e183ee | 192 | } |
maxint | 0:3d0db4e183ee | 193 | |
maxint | 0:3d0db4e183ee | 194 | int Rectangle::getCenterY() |
maxint | 0:3d0db4e183ee | 195 | { |
maxint | 0:3d0db4e183ee | 196 | return y1 + (y2-y1)/2; |
maxint | 0:3d0db4e183ee | 197 | } |
maxint | 0:3d0db4e183ee | 198 | |
maxint | 0:3d0db4e183ee | 199 | Point Rectangle::getCenter() |
maxint | 0:3d0db4e183ee | 200 | { |
maxint | 0:3d0db4e183ee | 201 | return(Point(x1 + (x2-x1)/2, y1 + (y2-y1)/2)); |
maxint | 0:3d0db4e183ee | 202 | } |
maxint | 0:3d0db4e183ee | 203 | |
maxint | 0:3d0db4e183ee | 204 | void Rectangle::set(Rectangle rNew) |
maxint | 0:3d0db4e183ee | 205 | { |
maxint | 0:3d0db4e183ee | 206 | x1=rNew.getX1(); |
maxint | 0:3d0db4e183ee | 207 | y1=rNew.getY1(); |
maxint | 0:3d0db4e183ee | 208 | x2=rNew.getX2(); |
maxint | 0:3d0db4e183ee | 209 | y2=rNew.getY2(); |
maxint | 0:3d0db4e183ee | 210 | } |
maxint | 0:3d0db4e183ee | 211 | |
maxint | 4:f421e34313d3 | 212 | bool Rectangle::isHorizontal() |
maxint | 4:f421e34313d3 | 213 | { |
maxint | 4:f421e34313d3 | 214 | return(x2-x1 > y2-y1); |
maxint | 4:f421e34313d3 | 215 | } |
maxint | 4:f421e34313d3 | 216 | |
maxint | 4:f421e34313d3 | 217 | |
maxint | 0:3d0db4e183ee | 218 | void Rectangle::move(Vector v) |
maxint | 0:3d0db4e183ee | 219 | { |
maxint | 0:3d0db4e183ee | 220 | x1+=rint(v.x); |
maxint | 0:3d0db4e183ee | 221 | y1+=rint(v.y); |
maxint | 0:3d0db4e183ee | 222 | x2+=rint(v.x); |
maxint | 0:3d0db4e183ee | 223 | y2+=rint(v.y); |
maxint | 0:3d0db4e183ee | 224 | } |
maxint | 0:3d0db4e183ee | 225 | |
maxint | 0:3d0db4e183ee | 226 | |
maxint | 0:3d0db4e183ee | 227 | /////////////////// |
maxint | 0:3d0db4e183ee | 228 | // CIRCLE |
maxint | 0:3d0db4e183ee | 229 | /////////////////// |
maxint | 0:3d0db4e183ee | 230 | Circle::Circle(int x,int y, int r) |
maxint | 0:3d0db4e183ee | 231 | { |
maxint | 0:3d0db4e183ee | 232 | x1=x; |
maxint | 0:3d0db4e183ee | 233 | y1=y; |
maxint | 0:3d0db4e183ee | 234 | r1=r; |
maxint | 0:3d0db4e183ee | 235 | } |
maxint | 0:3d0db4e183ee | 236 | |
maxint | 0:3d0db4e183ee | 237 | Point Circle::getCenter() |
maxint | 0:3d0db4e183ee | 238 | { |
maxint | 0:3d0db4e183ee | 239 | return(Point(x1, y1)); |
maxint | 0:3d0db4e183ee | 240 | } |
maxint | 0:3d0db4e183ee | 241 | |
maxint | 0:3d0db4e183ee | 242 | int Circle::getRadius() |
maxint | 0:3d0db4e183ee | 243 | { |
maxint | 0:3d0db4e183ee | 244 | return(r1); |
maxint | 0:3d0db4e183ee | 245 | } |
maxint | 0:3d0db4e183ee | 246 | |
maxint | 0:3d0db4e183ee | 247 | int Circle::getX() |
maxint | 0:3d0db4e183ee | 248 | { |
maxint | 0:3d0db4e183ee | 249 | return(x1); |
maxint | 0:3d0db4e183ee | 250 | } |
maxint | 0:3d0db4e183ee | 251 | |
maxint | 0:3d0db4e183ee | 252 | int Circle::getY() |
maxint | 0:3d0db4e183ee | 253 | { |
maxint | 0:3d0db4e183ee | 254 | return(y1); |
maxint | 0:3d0db4e183ee | 255 | } |
maxint | 0:3d0db4e183ee | 256 | |
maxint | 0:3d0db4e183ee | 257 | void Circle::setX(int x) |
maxint | 0:3d0db4e183ee | 258 | { |
maxint | 0:3d0db4e183ee | 259 | x1=x; |
maxint | 0:3d0db4e183ee | 260 | } |
maxint | 0:3d0db4e183ee | 261 | |
maxint | 0:3d0db4e183ee | 262 | void Circle::setY(int y) |
maxint | 0:3d0db4e183ee | 263 | { |
maxint | 0:3d0db4e183ee | 264 | y1=y; |
maxint | 0:3d0db4e183ee | 265 | } |
maxint | 0:3d0db4e183ee | 266 | |
maxint | 0:3d0db4e183ee | 267 | void Circle::setXY(int x, int y) |
maxint | 0:3d0db4e183ee | 268 | { |
maxint | 0:3d0db4e183ee | 269 | x1=x; |
maxint | 0:3d0db4e183ee | 270 | y1=y; |
maxint | 0:3d0db4e183ee | 271 | } |
maxint | 0:3d0db4e183ee | 272 | |
maxint | 0:3d0db4e183ee | 273 | void Circle::move(int x, int y) |
maxint | 0:3d0db4e183ee | 274 | { |
maxint | 0:3d0db4e183ee | 275 | x1+=x; |
maxint | 0:3d0db4e183ee | 276 | y1+=y; |
maxint | 0:3d0db4e183ee | 277 | } |
maxint | 0:3d0db4e183ee | 278 | |
maxint | 0:3d0db4e183ee | 279 | void Circle::move(Vector v) |
maxint | 0:3d0db4e183ee | 280 | { |
maxint | 0:3d0db4e183ee | 281 | x1+=rint(v.x); |
maxint | 0:3d0db4e183ee | 282 | y1+=rint(v.y); |
maxint | 0:3d0db4e183ee | 283 | } |
maxint | 0:3d0db4e183ee | 284 | |
maxint | 0:3d0db4e183ee | 285 | |
maxint | 0:3d0db4e183ee | 286 | Rectangle Circle::getBoundingRectangle() |
maxint | 0:3d0db4e183ee | 287 | { |
maxint | 0:3d0db4e183ee | 288 | return(Rectangle(x1-r1, y1-r1, x1+r1, y1+r1)); |
maxint | 0:3d0db4e183ee | 289 | } |
maxint | 3:441dc90d10ce | 290 | |
maxint | 3:441dc90d10ce | 291 | bool Circle::collides(Line ln) |
maxint | 3:441dc90d10ce | 292 | { // see if a circle collides with a line |
maxint | 3:441dc90d10ce | 293 | // check 1: first check using bounding rectangles |
maxint | 3:441dc90d10ce | 294 | // check 2: distance of line-ends to center or circle |
maxint | 3:441dc90d10ce | 295 | // check 3: check if distance to line is smaller than radius |
maxint | 3:441dc90d10ce | 296 | |
maxint | 3:441dc90d10ce | 297 | // Simple check 1: using bounding rectangles |
maxint | 3:441dc90d10ce | 298 | Rectangle rCircle=getBoundingRectangle(); |
maxint | 3:441dc90d10ce | 299 | if(!rCircle.collides(ln.getBoundingRectangle())) |
maxint | 3:441dc90d10ce | 300 | return(false); |
maxint | 3:441dc90d10ce | 301 | |
maxint | 3:441dc90d10ce | 302 | // Simple check 2: distance of line-ends to center or circle |
maxint | 3:441dc90d10ce | 303 | Point ptCenter=getCenter(); |
maxint | 3:441dc90d10ce | 304 | Line lnA(ptCenter, ln.get1()); |
maxint | 3:441dc90d10ce | 305 | if(lnA.getSize()<=r1) |
maxint | 3:441dc90d10ce | 306 | return(true); |
maxint | 3:441dc90d10ce | 307 | Line lnB(ptCenter, ln.get2()); |
maxint | 3:441dc90d10ce | 308 | if(lnB.getSize()<=r1) |
maxint | 3:441dc90d10ce | 309 | return(true); |
maxint | 3:441dc90d10ce | 310 | |
maxint | 3:441dc90d10ce | 311 | // check 3: check if distance to line is smaller than radius |
maxint | 3:441dc90d10ce | 312 | if(ln.getDistance(ptCenter) <= r1) |
maxint | 3:441dc90d10ce | 313 | return(true); |
maxint | 3:441dc90d10ce | 314 | |
maxint | 3:441dc90d10ce | 315 | return(false); |
maxint | 3:441dc90d10ce | 316 | } |