Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**/ package nape.geom;
  2. /**/ import nape.mixin.Pool;
  3. /**/ import nape.mixin.Names;
  4. /**/ import nape.mixin.MixList;
  5. /**/ import nape.mixin.Algorithm;
  6. /**/ import nape.geom.MacMath;
  7. /**/ import nape.util.FastMath;
  8. /**/ import nape.Const;
  9. /**/ #if flash
  10. /**/ import flash.geom.Point;
  11. /**/ #end
  12. /**/ class Vec2 {
  13. /**/     static public var znape_pool:Vec2;
  14. /**/     public var znape_next:Vec2;
  15. /**/     public var znape_npv2:NpV2;
  16. /**/     public function new() {}
  17. /**/     public static inline function get(x:Float,y:Float) {
  18. /**/         var ret = {
  19. /**/             if(Vec2.znape_pool==null) new Vec2();
  20. /**/             else {
  21. /**/                 var ret = Vec2.znape_pool;
  22. /**/                 Vec2.znape_pool = ret.znape_next;
  23. /**/                 ret;
  24. /**/             }
  25. /**/         };
  26. /**/         ret.znape_npv2 = NpV2.get(x,y);
  27. /**/         return ret;
  28. /**/     }
  29. /**/     public inline function free () {
  30. /**/         this.znape_next = Vec2.znape_pool;
  31. /**/         Vec2.znape_pool = this;
  32. /**/         this.znape_free();
  33. /**/     }
  34. /**/     public inline function znape_free() {
  35. /**/         znape_npv2.free();
  36. /**/         znape_npv2 = null;
  37. /**/     }
  38. /**/     #if flash
  39. /**/     public static inline function fromPoint(point:Point) {
  40. /**/         return get(point.x,point.y);
  41. /**/     }
  42. /**/     #end
  43. /**/     public inline function clone() return get(x(),y())
  44. /**/     public inline function x() return znape_npv2.x
  45. /**/     public inline function y() return znape_npv2.y
  46. /**/     public inline function setX(x:Float) znape_npv2.x = x
  47. /**/     public inline function setY(y:Float) znape_npv2.y = y
  48. /**/     public inline function set(x:Float,y:Float) {
  49. /**/         setX(x);
  50. /**/         setY(y);
  51. /**/     }
  52. /**/     public inline function setvec(v:Vec2) set(v.x(),v.y())
  53. /**/     #if flash
  54. /**/     public inline function getPoint() return new Point(x(),y())
  55. /**/     public inline function setPoint(point:Point) set(point.x,point.y)
  56. /**/     #end
  57. /**/     public inline function dotProduct (v:Vec2) return znape_npv2.dot (v.znape_npv2)
  58. /**/     public inline function crossProduct(v:Vec2) return znape_npv2.cross(v.znape_npv2)
  59. /**/     public inline function length () return znape_npv2.length()
  60. /**/     public inline function squaredLength() return znape_npv2.lsq ()
  61. /**/     public inline function normalise () znape_npv2.normalise()
  62. /**/     public inline function negate() znape_npv2.negate()
  63. /**/     public inline function toString() return znape_npv2.toString()
  64. /**/ }
  65. /**/ class NpV2 {
  66. /**/     static public var znape_pool:NpV2;
  67. /**/     public var znape_next:NpV2;
  68. /**/     public inline function begin() return znape_next
  69. /**/     public inline function znp_setbeg(i:NpV2) znape_next=i
  70. /**/     public inline function znp_new (o:NpV2) return o
  71. /**/     public inline function znp_delete(i:NpV2) {}
  72. /**/     public inline function znp_delelt(o:NpV2) {
  73. /**/         o.znape_next = NpV2.znape_pool;
  74. /**/         NpV2.znape_pool = o;
  75. /**/         o.znape_free();
  76. /**/     }
  77. /**/     public inline function znp_clear ( ) return true
  78. /**/     public inline function elem() return this
  79. /**/     public inline function next() return znape_next
  80. /**/     public inline function add(o:NpV2) {
  81. /**/         var temp = znp_new(o);
  82. /**/         temp.znape_next = begin();
  83. /**/         znp_setbeg(temp);
  84. /**/     }
  85. /**/     public inline function addAll(list: NpV2) {
  86. /**/         {
  87. /**/             var npiterator = list.begin();
  88. /**/             while(npiterator != null) {
  89. /**/                 var i = npiterator.elem();
  90. /**/                 {
  91. /**/                     add(i);
  92. /**/                 }
  93. /**/                 npiterator = npiterator.znape_next;
  94. /**/             }
  95. /**/         };
  96. /**/     }
  97. /**/     public inline function pop():Void {
  98. /**/         var ret = begin();
  99. /**/         znp_setbeg(ret.znape_next);
  100. /**/         znp_delelt(ret.elem());
  101. /**/         znp_delete(ret);
  102. /**/     }
  103. /**/     public inline function pop_unsafe():NpV2 {
  104. /**/         var ret = front();
  105. /**/         pop();
  106. /**/         return ret;
  107. /**/     }
  108. /**/     public inline function remove(obj:NpV2):Bool {
  109. /**/         var pre = null;
  110. /**/         var cur = begin();
  111. /**/         var ret = false;
  112. /**/         while(cur!=null) {
  113. /**/             if(cur.elem()==obj) {
  114. /**/                 erase(pre,cur);
  115. /**/                 ret = true;
  116. /**/                 break;
  117. /**/             }
  118. /**/             pre = cur;
  119. /**/             cur = cur.znape_next;
  120. /**/         }
  121. /**/         return ret;
  122. /**/     }
  123. /**/     public inline function erase(pre:NpV2,cur:NpV2):NpV2 {
  124. /**/         var old = cur; cur = cur.znape_next;
  125. /**/         if(pre==null) znp_setbeg (cur);
  126. /**/         else pre.znape_next = cur;
  127. /**/         znp_delelt(old.elem());
  128. /**/         znp_delete(old);
  129. /**/         return cur;
  130. /**/     }
  131. /**/     public inline function splice(pre:NpV2,cur:NpV2,n:Int):NpV2 {
  132. /**/         while(n-->0 && cur!=null)
  133. /**/         cur = erase(pre,cur);
  134. /**/         return cur;
  135. /**/     }
  136. /**/     public inline function clear() {
  137. /**/         if(znp_clear()) {
  138. /**/             while(!empty()) {
  139. /**/                 var old = begin();
  140. /**/                 znp_setbeg(old.znape_next);
  141. /**/                 znp_delelt(old.elem());
  142. /**/                 znp_delete(old);
  143. /**/             }
  144. /**/         }else znp_setbeg(null);
  145. /**/     }
  146. /**/     public inline function reverse() {
  147. /**/         var cur = begin();
  148. /**/         var pre = null;
  149. /**/         while(cur!=null) {
  150. /**/             var nx = cur.znape_next;
  151. /**/             cur.znape_next = pre;
  152. /**/             znp_setbeg(cur);
  153. /**/             pre = cur;
  154. /**/             cur = nx;
  155. /**/         }
  156. /**/     }
  157. /**/     public inline function empty():Bool return begin()==null
  158. /**/     public inline function size():Int {
  159. /**/         var cnt = 0;
  160. /**/         var cur = begin();
  161. /**/         while(cur!=null) { cnt++; cur=cur.znape_next; }
  162. /**/         return cnt;
  163. /**/     }
  164. /**/     public inline function has(obj:NpV2) return ({
  165. /**/         var ret = false;
  166. /**/         {
  167. /**/             var npiterator = this.begin();
  168. /**/             while(npiterator != null) {
  169. /**/                 var npite = npiterator.elem();
  170. /**/                 {
  171. /**/                     {
  172. /**/                         if(npite==obj) {
  173. /**/                             ret = true;
  174. /**/                             break;
  175. /**/                         }
  176. /**/                     };
  177. /**/                 }
  178. /**/                 npiterator = npiterator.znape_next;
  179. /**/             }
  180. /**/         };
  181. /**/         ret;
  182. /**/     })
  183. /**/     public inline function front() return begin().elem()
  184. /**/     public inline function back() {
  185. /**/         var ret = begin();
  186. /**/         var cur = ret;
  187. /**/         while(cur!=null) { ret = cur; cur = cur.znape_next; }
  188. /**/         return ret.elem();
  189. /**/     }
  190. /**/     public inline function at(ind:Int) return iterator_at(ind).elem()
  191. /**/     public inline function iterator_at(ind:Int) {
  192. /**/         var ret = begin();
  193. /**/         while(ind-->0) ret = ret.znape_next;
  194. /**/         return ret;
  195. /**/     }
  196. /**/     public inline function insert(cur:NpV2,o:NpV2) {
  197. /**/         if(cur==null) { add(o); return begin(); }
  198. /**/         else {
  199. /**/             var temp = znp_new(o);
  200. /**/             temp.znape_next = cur.znape_next;
  201. /**/             cur.znape_next = temp;
  202. /**/             return temp;
  203. /**/         }
  204. /**/     }
  205. /**/     public var x:Float; public var y:Float ;
  206. /**/     public function new() {}
  207. /**/     public static inline function get(x:Float, y:Float) {
  208. /**/         var ret = {
  209. /**/             if(NpV2.znape_pool==null) new NpV2();
  210. /**/             else {
  211. /**/                 var ret = NpV2.znape_pool;
  212. /**/                 NpV2.znape_pool = ret.znape_next;
  213. /**/                 ret;
  214. /**/             }
  215. /**/         };
  216. /**/         ret.set(x,y);
  217. /**/         return ret;
  218. /**/     }
  219. /**/     public inline function free() {
  220. /**/         this.znape_next = NpV2.znape_pool;
  221. /**/         NpV2.znape_pool = this;
  222. /**/         this.znape_free();
  223. /**/     }
  224. /**/     public inline function znape_free() {}
  225. /**/     public inline function set (x:Float, y:Float) { this.x = x; this.y = y; }
  226. /**/     public inline function setvec(v:NpV2) { this.x = v.x; this.y = v.y; }
  227. /**/     public inline function clone() return get(x,y)
  228. /**/     public inline function negate() {
  229. /**/         this.x = -this.x;
  230. /**/         this.y = -this.y;
  231. /**/     }
  232. /**/     public inline function dot (v:NpV2) return (this.x*v.x + this.y*v.y)
  233. /**/     public inline function cross(v:NpV2) return (this.x*v.y - this.y*v.x)
  234. /**/     public inline function lsq () return (this.x*this.x + this.y*this.y)
  235. /**/     public inline function length() return FastMath.sqrt( (this.x*this.x + this.y*this.y) )
  236. /**/     public inline function normalise() {
  237. /**/         var d = (this.x*this.x + this.y*this.y) ;
  238. /**/         var imag = if(d<Const.EPSILON) 0 else FastMath.invsqrt(d);
  239. /**/         this.x *= imag;
  240. /**/         this.y *= imag;
  241. /**/     }
  242. /**/     public inline function toString() return "<"+x+","+y+">"
  243. /**/ }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement