Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: RETRO_BallsAndPaddle RETRO_BallAndHoles
Shapes.cpp
00001 #include "mbed.h" 00002 #include "Shapes.h" 00003 00004 00005 00006 /////////////////// 00007 // POINT 00008 /////////////////// 00009 Point::Point() 00010 { // constructor 00011 x1=0; 00012 y1=0; 00013 } 00014 00015 Point::Point(int x, int y) 00016 { // constructor 00017 x1=x; 00018 y1=y; 00019 } 00020 00021 int Point::getX() 00022 { 00023 return x1; 00024 } 00025 00026 int Point::getY() 00027 { 00028 return y1; 00029 } 00030 00031 void Point::set(int x, int y) 00032 { 00033 x1=x; 00034 y1=y; 00035 } 00036 00037 00038 /////////////////// 00039 // LINE 00040 /////////////////// 00041 00042 Line::Line(int x,int y, int xx, int yy) 00043 { 00044 x1 = x; 00045 x2 = xx; 00046 y1 = y; 00047 y2 = yy; 00048 } 00049 00050 Line::Line(Point p1, Point p2) 00051 { 00052 x1 = p1.getX(); 00053 x2 = p2.getX(); 00054 y1 = p1.getY(); 00055 y2 = p2.getY(); 00056 } 00057 00058 Point Line::get1() 00059 { 00060 return(Point(x1, y1)); 00061 } 00062 00063 Point Line::get2() 00064 { 00065 return(Point(x2, y2)); 00066 } 00067 00068 00069 float Line::getSizeFloat() 00070 { // get the size of the vector 00071 return(sqrt((float)(x2-x1)*(float)(x2-x1)+(float)(y2-y1)*(float)(y2-y1))); 00072 } 00073 00074 int Line::getSize() 00075 { // get the size of the vector 00076 return((int)getSizeFloat()); 00077 } 00078 00079 Rectangle Line::getBoundingRectangle() 00080 { 00081 return(Rectangle(x1, y1, x2, y2)); 00082 } 00083 00084 int Line::getDistance(Point pt) 00085 { // get the distance of a line to a point 00086 // TODO: more precise calculation, currently it's a kind-of lame approximation 00087 Line ln1(get1(), pt); 00088 Line ln2(get2(), pt); 00089 int nDist1=ln1.getSize(); 00090 int nDist2=ln2.getSize(); 00091 int nDistApprox=nDist1+nDist2-getSize(); 00092 00093 // simple distance calculation: 00094 // if distance to either line-end combined equal to the line-length, the point is on the line 00095 // if the point is nearby, the difference might be good approximation of the distance 00096 if(nDistApprox<nDist1 && nDistApprox<nDist2) 00097 return(nDistApprox); 00098 00099 return(min(nDist1,nDist2)); 00100 } 00101 00102 00103 00104 00105 /////////////////// 00106 // RECTANGLE 00107 /////////////////// 00108 00109 Rectangle::Rectangle(int x,int y, int xx, int yy) 00110 { 00111 x1 = x; 00112 x2 = xx; 00113 y1 = y; 00114 y2 = yy; 00115 } 00116 00117 Rectangle::Rectangle(Point pt1, Point pt2) 00118 { 00119 x1 = pt1.getX(); 00120 x2 = pt2.getX(); 00121 y1 = pt1.getY(); 00122 y2 = pt2.getY(); 00123 } 00124 00125 bool Rectangle::collides(Point pt) 00126 { 00127 if(pt.getX() >= x1 && pt.getX() <= x2) { 00128 if(pt.getY() >= y1 && pt.getY() <= y2) { 00129 return true; 00130 } 00131 } 00132 return false; 00133 } 00134 00135 bool Rectangle::collides(Rectangle r) 00136 { // check if two rectangles collide 00137 // method: compare rectangle sides 00138 // see http://stackoverflow.com/questions/7610129/simple-collision-detection-android 00139 if ((getX2() < r.getX1()) || // A is to the left of B 00140 (r.getX2() < getX1()) || // B is to the left of A 00141 (getY2() < r.getY1()) || // A is above B 00142 (r.getY2() < getY1())) // B is above A 00143 return(false); 00144 return(true); 00145 } 00146 00147 Point Rectangle::get1() 00148 { 00149 return(Point(x1, y1)); 00150 } 00151 00152 Point Rectangle::get2() 00153 { 00154 return(Point(x2, y2)); 00155 } 00156 00157 Point Rectangle::get3() 00158 { 00159 return(Point(x2, y1)); 00160 } 00161 00162 Point Rectangle::get4() 00163 { 00164 return(Point(x1, y2)); 00165 } 00166 00167 00168 00169 int Rectangle::getX1() 00170 { 00171 return x1; 00172 } 00173 00174 int Rectangle::getX2() 00175 { 00176 return x2; 00177 } 00178 00179 int Rectangle::getY1() 00180 { 00181 return y1; 00182 } 00183 00184 int Rectangle::getY2() 00185 { 00186 return y2; 00187 } 00188 00189 int Rectangle::getCenterX() 00190 { 00191 return x1 + (x2-x1)/2; 00192 } 00193 00194 int Rectangle::getCenterY() 00195 { 00196 return y1 + (y2-y1)/2; 00197 } 00198 00199 Point Rectangle::getCenter() 00200 { 00201 return(Point(x1 + (x2-x1)/2, y1 + (y2-y1)/2)); 00202 } 00203 00204 void Rectangle::set(Rectangle rNew) 00205 { 00206 x1=rNew.getX1(); 00207 y1=rNew.getY1(); 00208 x2=rNew.getX2(); 00209 y2=rNew.getY2(); 00210 } 00211 00212 bool Rectangle::isHorizontal() 00213 { 00214 return(x2-x1 > y2-y1); 00215 } 00216 00217 00218 void Rectangle::move(Vector v) 00219 { 00220 x1+=rint(v.x); 00221 y1+=rint(v.y); 00222 x2+=rint(v.x); 00223 y2+=rint(v.y); 00224 } 00225 00226 00227 /////////////////// 00228 // CIRCLE 00229 /////////////////// 00230 Circle::Circle(int x,int y, int r) 00231 { 00232 x1=x; 00233 y1=y; 00234 r1=r; 00235 } 00236 00237 Point Circle::getCenter() 00238 { 00239 return(Point(x1, y1)); 00240 } 00241 00242 int Circle::getRadius() 00243 { 00244 return(r1); 00245 } 00246 00247 int Circle::getX() 00248 { 00249 return(x1); 00250 } 00251 00252 int Circle::getY() 00253 { 00254 return(y1); 00255 } 00256 00257 void Circle::setX(int x) 00258 { 00259 x1=x; 00260 } 00261 00262 void Circle::setY(int y) 00263 { 00264 y1=y; 00265 } 00266 00267 void Circle::setXY(int x, int y) 00268 { 00269 x1=x; 00270 y1=y; 00271 } 00272 00273 void Circle::move(int x, int y) 00274 { 00275 x1+=x; 00276 y1+=y; 00277 } 00278 00279 void Circle::move(Vector v) 00280 { 00281 x1+=rint(v.x); 00282 y1+=rint(v.y); 00283 } 00284 00285 00286 Rectangle Circle::getBoundingRectangle() 00287 { 00288 return(Rectangle(x1-r1, y1-r1, x1+r1, y1+r1)); 00289 } 00290 00291 bool Circle::collides(Line ln) 00292 { // see if a circle collides with a line 00293 // check 1: first check using bounding rectangles 00294 // check 2: distance of line-ends to center or circle 00295 // check 3: check if distance to line is smaller than radius 00296 00297 // Simple check 1: using bounding rectangles 00298 Rectangle rCircle=getBoundingRectangle(); 00299 if(!rCircle.collides(ln.getBoundingRectangle())) 00300 return(false); 00301 00302 // Simple check 2: distance of line-ends to center or circle 00303 Point ptCenter=getCenter(); 00304 Line lnA(ptCenter, ln.get1()); 00305 if(lnA.getSize()<=r1) 00306 return(true); 00307 Line lnB(ptCenter, ln.get2()); 00308 if(lnB.getSize()<=r1) 00309 return(true); 00310 00311 // check 3: check if distance to line is smaller than radius 00312 if(ln.getDistance(ptCenter) <= r1) 00313 return(true); 00314 00315 return(false); 00316 }
Generated on Thu Jul 14 2022 10:55:45 by
1.7.2