Last active
July 11, 2018 05:46
-
-
Save Najaran/0306f5005b0581c46fbfc97e1fcb85c2 to your computer and use it in GitHub Desktop.
2D上での矩形衝突検知を大小比較のみで行ってみた(C++サンプル付き) ref: https://qiita.com/Najaran/items/779286014607f0f23e54
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
typedef struct _pos { | |
double x; // X-axis | |
double y; // Y-axis | |
}pos; | |
int main() { | |
pos a1, a2, b1, b2; | |
cout << "RectA: "; | |
cin >> a1.x >> a1.y >> a2.x >> a2.y; | |
cout << "RectB: "; | |
cin >> b1.x >> b1.y >> b2.x >> b2.y; | |
if(a2.x < b1.x || b2.x < a1.x || a2.y < b1.y || b2.y < a1.y) | |
cout << "no collision" << endl; // 衝突していないと判定 | |
else | |
cout << "collision" << endl; // 衝突していると判定 | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
\left(x_{a2}<x_{b1}\right)\vee\left(x_{b2}<x_{a1}\right)\vee\left(y_{a2}<y_{b1}\right)\vee\left(y_{b2}<y_{a1}\right) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(5<0)\vee(6<1)\vee(5<1)\vee(4<0)=F \vee F \vee F \vee F=F |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
\begin{eqnarray} | |
x成分&:&(x_{a1} < x_{a2} < x_{b1} < x_{b2}) &\vee& (x_{b1} < x_{b2} < x_{a1} < x_{a2})\\ | |
y成分&:&(y_{a1} < y_{a2} < y_{b1} < y_{b2}) &\vee& (y_{b1} < y_{b2} < y_{a1} < y_{a2}) | |
\end{eqnarray} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
\begin{eqnarray} | |
(x_{a1} < x_{a2} < x_{b1} < x_{b2}) &\vee& (x_{b1} < x_{b2} < x_{a1} < x_{a2})\\ | |
\ \ &\vee&(y_{a1} < y_{a2} < y_{b1} < y_{b2})\\ | |
\ \ &\vee&(y_{b1} < y_{b2} < y_{a1} < y_{a2}) | |
\end{eqnarray} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(x_{a1} < x_{a2}),\ \ (x_{b1} < x_{b2}),\ \ (y_{a1} < y_{a2}),\ \ (y_{b1} < y_{b2}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
\left(x_{a2}<x_{b1}\right)\vee\left(x_{b2}<x_{a1}\right)\vee\left(y_{a2}<y_{b1}\right)\vee\left(y_{b2}<y_{a1}\right) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment