MathVector.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. using System;
  2. namespace CommonUI.Gemo
  3. {
  4. public class MathVector
  5. {
  6. /**
  7. * 移动指定偏移
  8. * @param v
  9. * @param dx x距离
  10. * @param dy y距离
  11. */
  12. public static void move(Point2D v, float dx, float dy){
  13. v.x += (dx);
  14. v.y += (dy);
  15. }
  16. /**
  17. * 通过极坐标来移动
  18. * @param v
  19. * @param degree 弧度
  20. * @param distance 距离
  21. */
  22. public static void movePolar(Point2D v, float degree, float distance){
  23. float dx = (float) Math.Cos(degree) * distance;
  24. float dy = (float) Math.Sin(degree) * distance;
  25. move(v, dx, dy);
  26. }
  27. /**
  28. * 通过极坐标来移动
  29. * @param v
  30. * @param degree 弧度
  31. * @param speed 速度 (单位距离/秒)
  32. * @param interval_ms 毫秒时间
  33. */
  34. public static void movePolar(Point2D v, float degree, float speed, int interval_ms) {
  35. float distance = getDistance(speed, interval_ms);
  36. movePolar(v, degree, distance);
  37. }
  38. /**
  39. * 向目标移动
  40. * @param v
  41. * @param x 目标x
  42. * @param y 目标y
  43. * @return 是否到达目的地
  44. */
  45. public static bool moveTo(Point2D v, float x, float y, float distance){
  46. float ddx = x - v.x;
  47. float ddy = y - v.y;
  48. if (Math.Abs(ddx) < distance && Math.Abs(ddy) < distance) {
  49. v.x = (x);
  50. v.y = (y);
  51. return true;
  52. } else {
  53. float angle = (float)Math.Atan2(ddy, ddx);
  54. movePolar(v, angle, distance);
  55. return false;
  56. }
  57. }
  58. public static bool moveToX(Point2D v, float x, float distance){
  59. float ddx = x - v.x;
  60. if (Math.Abs(ddx) < distance) {
  61. v.x = (x);
  62. return true;
  63. } else {
  64. if (ddx > 0) {
  65. v.x += (distance);
  66. } else {
  67. v.x += (-distance);
  68. }
  69. return false;
  70. }
  71. }
  72. public static bool moveToY(Point2D v, float y, float distance){
  73. float ddy = y - v.y;
  74. if (Math.Abs(ddy) < distance) {
  75. v.y = (y);
  76. return true;
  77. } else {
  78. if (ddy > 0) {
  79. v.y += (distance);
  80. } else {
  81. v.y += (-distance);
  82. }
  83. return false;
  84. }
  85. }
  86. /**
  87. * 向量缩放
  88. * @param v
  89. * @param scale
  90. */
  91. public static void scale(Point2D v, float scale){
  92. v.x = (v.x * scale);
  93. v.y = (v.y * scale);
  94. }
  95. /**
  96. * 向量缩放
  97. * @param v
  98. * @param scale
  99. */
  100. public static void scale(Point2D v, float scale_x, float scale_y){
  101. v.x = (v.x * scale_x);
  102. v.y = (v.y * scale_y);
  103. }
  104. /**
  105. * 向量按照{0,0}点旋转
  106. * @param v
  107. * @param degree 弧度
  108. */
  109. public static void rotate(Point2D v, float degree)
  110. {
  111. float cos_v = (float)Math.Cos(degree);
  112. float sin_v = (float)Math.Sin(degree);
  113. float x = (v.x) * cos_v - (v.y) * sin_v;
  114. float y = (v.y) * cos_v + (v.x) * sin_v;
  115. v.x = (x);
  116. v.y = (y);
  117. }
  118. /**
  119. * 向量按照p0点旋转
  120. * @param v
  121. * @param p0
  122. * @param degree 弧度
  123. */
  124. public static void rotate(Point2D v, Point2D p0, float degree)
  125. {
  126. float dx = v.x - p0.x;
  127. float dy = v.y - p0.y;
  128. float cos_v = (float)Math.Cos(degree);
  129. float sin_v = (float)Math.Sin(degree);
  130. float x = p0.x + dx * cos_v - dy * sin_v;
  131. float y = p0.y + dy * cos_v + dx * sin_v;
  132. v.x = (x);
  133. v.y = (y);
  134. }
  135. /**
  136. * 向量按照p0点旋转
  137. * @param v
  138. * @param p0
  139. * @param degree 弧度
  140. */
  141. public static void rotate(Point2D v, float px, float py, float degree) {
  142. float dx = v.x - px;
  143. float dy = v.y - py;
  144. float cos_v = (float)Math.Cos(degree);
  145. float sin_v = (float)Math.Sin(degree);
  146. float x = px + dx * cos_v - dy * sin_v;
  147. float y = py + dy * cos_v + dx * sin_v;
  148. v.x = (x);
  149. v.y = (y);
  150. }
  151. /**
  152. * 得到速度和时间产生的距离
  153. * @param speed 速度 (单位距离/秒)
  154. * @param interval_ms 毫秒时间
  155. * @return
  156. */
  157. public static float getDistance(float speed, int interval_ms){
  158. float rate = interval_ms / 1000f ;
  159. return speed * rate;
  160. }
  161. public static float getDistance(Point2D v) {
  162. return getDistance(0, 0, v.x, v.y);
  163. }
  164. public static float getDistance(float x1, float y1, float x2, float y2){
  165. float r1 = x1-x2;
  166. float r2 = y1-y2;
  167. return (float)Math.Sqrt(r1*r1+r2*r2);
  168. }
  169. /**
  170. * 得到弧度
  171. * @param dx x向量
  172. * @param dy y向量
  173. * @return
  174. */
  175. public static float getDegree(float dx, float dy)
  176. {
  177. return (float)Math.Atan2(dy, dx);
  178. }
  179. /**
  180. * 得到弧度
  181. * @param v 向量
  182. * @return
  183. */
  184. public static float getDegree(Point2D v)
  185. {
  186. return (float)Math.Atan2(v.y, v.x);
  187. }
  188. /**
  189. * 将2个向量相加得到一个新的向量
  190. * @param a
  191. * @param b
  192. * @return
  193. */
  194. public static Point2D vectorAdd(Point2D a, Point2D b){
  195. Point2D v = new Point2D();
  196. v.x = (a.x + b.x);
  197. v.y = (a.y + b.y);
  198. return v;
  199. }
  200. /**
  201. * 将2个向量相减得到一个新的向量
  202. * @param a
  203. * @param b
  204. * @return
  205. */
  206. public static Point2D vectorSub(Point2D a, Point2D b){
  207. Point2D v = new Point2D();
  208. v.x = (a.x - b.x);
  209. v.y = (a.y - b.y);
  210. return v;
  211. }
  212. /**
  213. * 将一个向量加上新的向量,得到一个新的向量
  214. * @param a
  215. * @param degree
  216. * @param distance
  217. * @return
  218. */
  219. public static Point2D vectorAdd(Point2D a, float degree, float distance){
  220. Point2D v = new Point2D();
  221. v.x = (a.x);
  222. v.y = (a.y);
  223. movePolar(v, degree, distance);
  224. return v;
  225. }
  226. /**
  227. * 把一个向量向自己本身的方向相加,得到一个新的向量
  228. * @param a
  229. * @param distance
  230. * @return
  231. */
  232. public static Point2D vectorAdd(Point2D a, float distance){
  233. Point2D v = new Point2D();
  234. v.x = (a.x);
  235. v.y = (a.y);
  236. movePolar(v, getDegree(v), distance);
  237. return v;
  238. }
  239. /**
  240. * 将一个向量缩放一定比率后,得到一个新的向量
  241. * @param a
  242. * @param scale
  243. * @return
  244. */
  245. public static Point2D vectorScale(Point2D a, float scale){
  246. Point2D v = new Point2D();
  247. v.x = (a.x * scale);
  248. v.y = (a.y * scale);
  249. return v;
  250. }
  251. }
  252. }