PageRenderTime 60ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/docs/Line.js.html

https://gitlab.com/lobsterhands/phaser
HTML | 1008 lines | 760 code | 245 blank | 3 comment | 0 complexity | 5447e06c7f76fea99e13b834417e3eb6 MD5 | raw file
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Phaser Source: geom/Line.js</title>
  6. <!--[if lt IE 9]>
  7. <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  8. <![endif]-->
  9. <link type="text/css" rel="stylesheet" href="styles/sunlight.default.css">
  10. <link type="text/css" rel="stylesheet" href="styles/site.cerulean.css">
  11. </head>
  12. <body>
  13. <div class="container-fluid">
  14. <div class="navbar navbar-fixed-top navbar-inverse">
  15. <div class="navbar-inner">
  16. <a class="brand" href="index.html">Phaser</a>
  17. <ul class="nav">
  18. <li class="dropdown">
  19. <a href="namespaces.list.html" class="dropdown-toggle" data-toggle="dropdown">Namespaces<b
  20. class="caret"></b></a>
  21. <ul class="dropdown-menu ">
  22. <li class="class-depth-0">
  23. <a href="Phaser.html">Phaser</a>
  24. </li>
  25. </ul>
  26. </li>
  27. <li class="dropdown">
  28. <a href="classes.list.html" class="dropdown-toggle" data-toggle="dropdown">Classes<b
  29. class="caret"></b></a>
  30. <ul class="dropdown-menu ">
  31. <li class="class-depth-1">
  32. <a href="Phaser.Animation.html">Animation</a>
  33. </li>
  34. <li class="class-depth-1">
  35. <a href="Phaser.AnimationManager.html">AnimationManager</a>
  36. </li>
  37. <li class="class-depth-1">
  38. <a href="Phaser.AnimationParser.html">AnimationParser</a>
  39. </li>
  40. <li class="class-depth-1">
  41. <a href="Phaser.BitmapData.html">BitmapData</a>
  42. </li>
  43. <li class="class-depth-1">
  44. <a href="Phaser.BitmapText.html">BitmapText</a>
  45. </li>
  46. <li class="class-depth-1">
  47. <a href="Phaser.Button.html">Button</a>
  48. </li>
  49. <li class="class-depth-1">
  50. <a href="Phaser.Cache.html">Cache</a>
  51. </li>
  52. <li class="class-depth-1">
  53. <a href="Phaser.Camera.html">Camera</a>
  54. </li>
  55. <li class="class-depth-1">
  56. <a href="Phaser.Canvas.html">Canvas</a>
  57. </li>
  58. <li class="class-depth-1">
  59. <a href="Phaser.Circle.html">Circle</a>
  60. </li>
  61. <li class="class-depth-1">
  62. <a href="Phaser.Color.html">Color</a>
  63. </li>
  64. <li class="class-depth-1">
  65. <a href="Phaser.Device.html">Device</a>
  66. </li>
  67. <li class="class-depth-1">
  68. <a href="Phaser.Easing.html">Easing</a>
  69. </li>
  70. <li class="class-depth-2">
  71. <a href="Phaser.Easing.Back.html">Back</a>
  72. </li>
  73. <li class="class-depth-2">
  74. <a href="Phaser.Easing.Bounce.html">Bounce</a>
  75. </li>
  76. <li class="class-depth-2">
  77. <a href="Phaser.Easing.Circular.html">Circular</a>
  78. </li>
  79. <li class="class-depth-2">
  80. <a href="Phaser.Easing.Cubic.html">Cubic</a>
  81. </li>
  82. <li class="class-depth-2">
  83. <a href="Phaser.Easing.Elastic.html">Elastic</a>
  84. </li>
  85. <li class="class-depth-2">
  86. <a href="Phaser.Easing.Exponential.html">Exponential</a>
  87. </li>
  88. <li class="class-depth-2">
  89. <a href="Phaser.Easing.Linear.html">Linear</a>
  90. </li>
  91. <li class="class-depth-2">
  92. <a href="Phaser.Easing.Quadratic.html">Quadratic</a>
  93. </li>
  94. <li class="class-depth-2">
  95. <a href="Phaser.Easing.Quartic.html">Quartic</a>
  96. </li>
  97. <li class="class-depth-2">
  98. <a href="Phaser.Easing.Quintic.html">Quintic</a>
  99. </li>
  100. <li class="class-depth-2">
  101. <a href="Phaser.Easing.Sinusoidal.html">Sinusoidal</a>
  102. </li>
  103. <li class="class-depth-1">
  104. <a href="Phaser.Ellipse.html">Ellipse</a>
  105. </li>
  106. <li class="class-depth-1">
  107. <a href="Phaser.Events.html">Events</a>
  108. </li>
  109. <li class="class-depth-1">
  110. <a href="Phaser.Filter.html">Filter</a>
  111. </li>
  112. <li class="class-depth-1">
  113. <a href="Phaser.Frame.html">Frame</a>
  114. </li>
  115. <li class="class-depth-1">
  116. <a href="Phaser.FrameData.html">FrameData</a>
  117. </li>
  118. <li class="class-depth-1">
  119. <a href="Phaser.Game.html">Game</a>
  120. </li>
  121. <li class="class-depth-1">
  122. <a href="Phaser.GameObjectCreator.html">GameObjectCreator</a>
  123. </li>
  124. <li class="class-depth-1">
  125. <a href="Phaser.GameObjectFactory.html">GameObjectFactory</a>
  126. </li>
  127. <li class="class-depth-1">
  128. <a href="Phaser.Gamepad.html">Gamepad</a>
  129. </li>
  130. <li class="class-depth-1">
  131. <a href="Phaser.GamepadButton.html">GamepadButton</a>
  132. </li>
  133. <li class="class-depth-1">
  134. <a href="Phaser.Graphics.html">Graphics</a>
  135. </li>
  136. <li class="class-depth-1">
  137. <a href="Phaser.Group.html">Group</a>
  138. </li>
  139. <li class="class-depth-1">
  140. <a href="Phaser.Image.html">Image</a>
  141. </li>
  142. <li class="class-depth-1">
  143. <a href="Phaser.Input.html">Input</a>
  144. </li>
  145. <li class="class-depth-1">
  146. <a href="Phaser.InputHandler.html">InputHandler</a>
  147. </li>
  148. <li class="class-depth-1">
  149. <a href="Phaser.Key.html">Key</a>
  150. </li>
  151. <li class="class-depth-1">
  152. <a href="Phaser.Keyboard.html">Keyboard</a>
  153. </li>
  154. <li class="class-depth-1">
  155. <a href="Phaser.Line.html">Line</a>
  156. </li>
  157. <li class="class-depth-1">
  158. <a href="Phaser.LinkedList.html">LinkedList</a>
  159. </li>
  160. <li class="class-depth-1">
  161. <a href="Phaser.Loader.html">Loader</a>
  162. </li>
  163. <li class="class-depth-1">
  164. <a href="Phaser.LoaderParser.html">LoaderParser</a>
  165. </li>
  166. <li class="class-depth-1">
  167. <a href="Phaser.Math.html">Math</a>
  168. </li>
  169. <li class="class-depth-1">
  170. <a href="Phaser.Mouse.html">Mouse</a>
  171. </li>
  172. <li class="class-depth-1">
  173. <a href="Phaser.MSPointer.html">MSPointer</a>
  174. </li>
  175. <li class="class-depth-1">
  176. <a href="Phaser.Net.html">Net</a>
  177. </li>
  178. <li class="class-depth-1">
  179. <a href="Phaser.Particle.html">Particle</a>
  180. </li>
  181. <li class="class-depth-1">
  182. <a href="Phaser.Particles.html">Particles</a>
  183. </li>
  184. <li class="class-depth-3">
  185. <a href="Phaser.Particles.Arcade.Emitter.html">Emitter</a>
  186. </li>
  187. <li class="class-depth-1">
  188. <a href="Phaser.Physics.html">Physics</a>
  189. </li>
  190. <li class="class-depth-2">
  191. <a href="Phaser.Physics.Arcade.html">Arcade</a>
  192. </li>
  193. <li class="class-depth-3">
  194. <a href="Phaser.Physics.Arcade.Body.html">Body</a>
  195. </li>
  196. <li class="class-depth-2">
  197. <a href="Phaser.Physics.Ninja.html">Ninja</a>
  198. </li>
  199. <li class="class-depth-3">
  200. <a href="Phaser.Physics.Ninja.AABB.html">AABB</a>
  201. </li>
  202. <li class="class-depth-3">
  203. <a href="Phaser.Physics.Ninja.Body.html">Body</a>
  204. </li>
  205. <li class="class-depth-3">
  206. <a href="Phaser.Physics.Ninja.Circle.html">Circle</a>
  207. </li>
  208. <li class="class-depth-3">
  209. <a href="Phaser.Physics.Ninja.Tile.html">Tile</a>
  210. </li>
  211. <li class="class-depth-2">
  212. <a href="Phaser.Physics.P2.html">P2</a>
  213. </li>
  214. <li class="class-depth-3">
  215. <a href="Phaser.Physics.P2.Body.html">Body</a>
  216. </li>
  217. <li class="class-depth-3">
  218. <a href="Phaser.Physics.P2.BodyDebug.html">BodyDebug</a>
  219. </li>
  220. <li class="class-depth-3">
  221. <a href="Phaser.Physics.P2.CollisionGroup.html">CollisionGroup</a>
  222. </li>
  223. <li class="class-depth-3">
  224. <a href="Phaser.Physics.P2.ContactMaterial.html">ContactMaterial</a>
  225. </li>
  226. <li class="class-depth-3">
  227. <a href="Phaser.Physics.P2.DistanceConstraint.html">DistanceConstraint</a>
  228. </li>
  229. <li class="class-depth-3">
  230. <a href="Phaser.Physics.P2.FixtureList.html">FixtureList</a>
  231. </li>
  232. <li class="class-depth-3">
  233. <a href="Phaser.Physics.P2.GearConstraint.html">GearConstraint</a>
  234. </li>
  235. <li class="class-depth-3">
  236. <a href="Phaser.Physics.P2.InversePointProxy.html">InversePointProxy</a>
  237. </li>
  238. <li class="class-depth-3">
  239. <a href="Phaser.Physics.P2.LockConstraint.html">LockConstraint</a>
  240. </li>
  241. <li class="class-depth-3">
  242. <a href="Phaser.Physics.P2.Material.html">Material</a>
  243. </li>
  244. <li class="class-depth-3">
  245. <a href="Phaser.Physics.P2.PointProxy.html">PointProxy</a>
  246. </li>
  247. <li class="class-depth-3">
  248. <a href="Phaser.Physics.P2.PrismaticConstraint.html">PrismaticConstraint</a>
  249. </li>
  250. <li class="class-depth-3">
  251. <a href="Phaser.Physics.P2.RevoluteConstraint.html">RevoluteConstraint</a>
  252. </li>
  253. <li class="class-depth-3">
  254. <a href="Phaser.Physics.P2.Spring.html">Spring</a>
  255. </li>
  256. <li class="class-depth-1">
  257. <a href="Phaser.Plugin.html">Plugin</a>
  258. </li>
  259. <li class="class-depth-1">
  260. <a href="Phaser.PluginManager.html">PluginManager</a>
  261. </li>
  262. <li class="class-depth-1">
  263. <a href="Phaser.Point.html">Point</a>
  264. </li>
  265. <li class="class-depth-1">
  266. <a href="Phaser.Pointer.html">Pointer</a>
  267. </li>
  268. <li class="class-depth-1">
  269. <a href="Phaser.Polygon.html">Polygon</a>
  270. </li>
  271. <li class="class-depth-1">
  272. <a href="Phaser.QuadTree.html">QuadTree</a>
  273. </li>
  274. <li class="class-depth-1">
  275. <a href="Phaser.RandomDataGenerator.html">RandomDataGenerator</a>
  276. </li>
  277. <li class="class-depth-1">
  278. <a href="Phaser.Rectangle.html">Rectangle</a>
  279. </li>
  280. <li class="class-depth-1">
  281. <a href="Phaser.RenderTexture.html">RenderTexture</a>
  282. </li>
  283. <li class="class-depth-1">
  284. <a href="Phaser.RequestAnimationFrame.html">RequestAnimationFrame</a>
  285. </li>
  286. <li class="class-depth-1">
  287. <a href="Phaser.RetroFont.html">RetroFont</a>
  288. </li>
  289. <li class="class-depth-1">
  290. <a href="Phaser.ScaleManager.html">ScaleManager</a>
  291. </li>
  292. <li class="class-depth-1">
  293. <a href="Phaser.Signal.html">Signal</a>
  294. </li>
  295. <li class="class-depth-1">
  296. <a href="Phaser.SignalBinding.html">SignalBinding</a>
  297. </li>
  298. <li class="class-depth-1">
  299. <a href="Phaser.SinglePad.html">SinglePad</a>
  300. </li>
  301. <li class="class-depth-1">
  302. <a href="Phaser.Sound.html">Sound</a>
  303. </li>
  304. <li class="class-depth-1">
  305. <a href="Phaser.SoundManager.html">SoundManager</a>
  306. </li>
  307. <li class="class-depth-1">
  308. <a href="Phaser.Sprite.html">Sprite</a>
  309. </li>
  310. <li class="class-depth-1">
  311. <a href="Phaser.SpriteBatch.html">SpriteBatch</a>
  312. </li>
  313. <li class="class-depth-1">
  314. <a href="Phaser.Stage.html">Stage</a>
  315. </li>
  316. <li class="class-depth-1">
  317. <a href="Phaser.State.html">State</a>
  318. </li>
  319. <li class="class-depth-1">
  320. <a href="Phaser.StateManager.html">StateManager</a>
  321. </li>
  322. <li class="class-depth-1">
  323. <a href="Phaser.Text.html">Text</a>
  324. </li>
  325. <li class="class-depth-1">
  326. <a href="Phaser.Tile.html">Tile</a>
  327. </li>
  328. <li class="class-depth-1">
  329. <a href="Phaser.Tilemap.html">Tilemap</a>
  330. </li>
  331. <li class="class-depth-1">
  332. <a href="Phaser.TilemapLayer.html">TilemapLayer</a>
  333. </li>
  334. <li class="class-depth-1">
  335. <a href="Phaser.TilemapParser.html">TilemapParser</a>
  336. </li>
  337. <li class="class-depth-1">
  338. <a href="Phaser.Tileset.html">Tileset</a>
  339. </li>
  340. <li class="class-depth-1">
  341. <a href="Phaser.TileSprite.html">TileSprite</a>
  342. </li>
  343. <li class="class-depth-1">
  344. <a href="Phaser.Time.html">Time</a>
  345. </li>
  346. <li class="class-depth-1">
  347. <a href="Phaser.Timer.html">Timer</a>
  348. </li>
  349. <li class="class-depth-1">
  350. <a href="Phaser.TimerEvent.html">TimerEvent</a>
  351. </li>
  352. <li class="class-depth-1">
  353. <a href="Phaser.Touch.html">Touch</a>
  354. </li>
  355. <li class="class-depth-1">
  356. <a href="Phaser.Tween.html">Tween</a>
  357. </li>
  358. <li class="class-depth-1">
  359. <a href="Phaser.TweenManager.html">TweenManager</a>
  360. </li>
  361. <li class="class-depth-1">
  362. <a href="Phaser.Utils.html">Utils</a>
  363. </li>
  364. <li class="class-depth-2">
  365. <a href="Phaser.Utils.Debug.html">Debug</a>
  366. </li>
  367. <li class="class-depth-1">
  368. <a href="Phaser.World.html">World</a>
  369. </li>
  370. </ul>
  371. </li>
  372. </ul>
  373. </div>
  374. </div>
  375. <div class="row-fluid">
  376. <div class="span12">
  377. <div id="main">
  378. <h1 class="page-title">Source: geom/Line.js</h1>
  379. <section>
  380. <article>
  381. <pre class="sunlight-highlight-javascript linenums">/**
  382. * @author Richard Davey &lt;rich@photonstorm.com>
  383. * @copyright 2014 Photon Storm Ltd.
  384. * @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
  385. */
  386. /**
  387. * Creates a new Line object with a start and an end point.
  388. * @class Line
  389. * @classdesc Phaser - Line
  390. * @constructor
  391. * @param {number} [x1=0] - The x coordinate of the start of the line.
  392. * @param {number} [y1=0] - The y coordinate of the start of the line.
  393. * @param {number} [x2=0] - The x coordinate of the end of the line.
  394. * @param {number} [y2=0] - The y coordinate of the end of the line.
  395. * @return {Phaser.Line} This line object
  396. */
  397. Phaser.Line = function (x1, y1, x2, y2) {
  398. x1 = x1 || 0;
  399. y1 = y1 || 0;
  400. x2 = x2 || 0;
  401. y2 = y2 || 0;
  402. /**
  403. * @property {Phaser.Point} start - The start point of the line.
  404. */
  405. this.start = new Phaser.Point(x1, y1);
  406. /**
  407. * @property {Phaser.Point} end - The end point of the line.
  408. */
  409. this.end = new Phaser.Point(x2, y2);
  410. };
  411. Phaser.Line.prototype = {
  412. /**
  413. * Sets the components of the Line to the specified values.
  414. * @method Phaser.Line#setTo
  415. * @param {number} [x1=0] - The x coordinate of the start of the line.
  416. * @param {number} [y1=0] - The y coordinate of the start of the line.
  417. * @param {number} [x2=0] - The x coordinate of the end of the line.
  418. * @param {number} [y2=0] - The y coordinate of the end of the line.
  419. * @return {Phaser.Line} This line object
  420. */
  421. setTo: function (x1, y1, x2, y2) {
  422. this.start.setTo(x1, y1);
  423. this.end.setTo(x2, y2);
  424. return this;
  425. },
  426. /**
  427. * Sets the line to match the x/y coordinates of the two given sprites.
  428. * Can optionally be calculated from their center coordinates.
  429. * @method Phaser.Line#fromSprite
  430. * @param {Phaser.Sprite} startSprite - The coordinates of this Sprite will be set to the Line.start point.
  431. * @param {Phaser.Sprite} endSprite - The coordinates of this Sprite will be set to the Line.start point.
  432. * @param {boolean} [useCenter=false] - If true it will use startSprite.center.x, if false startSprite.x. Note that Sprites don't have a center property by default, so only enable if you've over-ridden your Sprite with a custom class.
  433. * @return {Phaser.Line} This line object
  434. */
  435. fromSprite: function (startSprite, endSprite, useCenter) {
  436. if (typeof useCenter === 'undefined') { useCenter = false; }
  437. if (useCenter)
  438. {
  439. return this.setTo(startSprite.center.x, startSprite.center.y, endSprite.center.x, endSprite.center.y);
  440. }
  441. else
  442. {
  443. return this.setTo(startSprite.x, startSprite.y, endSprite.x, endSprite.y);
  444. }
  445. },
  446. /**
  447. * Checks for intersection between this line and another Line.
  448. * If asSegment is true it will check for segment intersection. If asSegment is false it will check for line intersection.
  449. * Returns the intersection segment of AB and EF as a Point, or null if there is no intersection.
  450. *
  451. * @method Phaser.Line#intersects
  452. * @param {Phaser.Line} line - The line to check against this one.
  453. * @param {boolean} [asSegment=true] - If true it will check for segment intersection, otherwise full line intersection.
  454. * @param {Phaser.Point} [result] - A Point object to store the result in, if not given a new one will be created.
  455. * @return {Phaser.Point} The intersection segment of the two lines as a Point, or null if there is no intersection.
  456. */
  457. intersects: function (line, asSegment, result) {
  458. return Phaser.Line.intersectsPoints(this.start, this.end, line.start, line.end, asSegment, result);
  459. },
  460. /**
  461. * Tests if the given coordinates fall on this line. See pointOnSegment to test against just the line segment.
  462. * @method Phaser.Line#pointOnLine
  463. * @param {number} x - The line to check against this one.
  464. * @param {number} y - The line to check against this one.
  465. * @return {boolean} True if the point is on the line, false if not.
  466. */
  467. pointOnLine: function (x, y) {
  468. return ((x - this.start.x) * (this.end.y - this.end.y) === (this.end.x - this.start.x) * (y - this.end.y));
  469. },
  470. /**
  471. * Tests if the given coordinates fall on this line and within the segment. See pointOnLine to test against just the line.
  472. * @method Phaser.Line#pointOnSegment
  473. * @param {number} x - The line to check against this one.
  474. * @param {number} y - The line to check against this one.
  475. * @return {boolean} True if the point is on the line and segment, false if not.
  476. */
  477. pointOnSegment: function (x, y) {
  478. var xMin = Math.min(this.start.x, this.end.x);
  479. var xMax = Math.max(this.start.x, this.end.x);
  480. var yMin = Math.min(this.start.y, this.end.y);
  481. var yMax = Math.max(this.start.y, this.end.y);
  482. return (this.pointOnLine(x, y) && (x >= xMin && x &lt;= xMax) && (y >= yMin && y &lt;= yMax));
  483. },
  484. /**
  485. * Using Bresenham's line algorithm this will return an array of all coordinates on this line.
  486. * The start and end points are rounded before this runs as the algorithm works on integers.
  487. *
  488. * @method Phaser.Line#coordinatesOnLine
  489. * @param {number} [stepRate=1] - How many steps will we return? 1 = every coordinate on the line, 2 = every other coordinate, etc.
  490. * @param {array} [results] - The array to store the results in. If not provided a new one will be generated.
  491. * @return {array} An array of coordinates.
  492. */
  493. coordinatesOnLine: function (stepRate, results) {
  494. if (typeof stepRate === 'undefined') { stepRate = 1; }
  495. if (typeof results === 'undefined') { results = []; }
  496. var x1 = Math.round(this.start.x);
  497. var y1 = Math.round(this.start.y);
  498. var x2 = Math.round(this.end.x);
  499. var y2 = Math.round(this.end.y);
  500. var dx = Math.abs(x2 - x1);
  501. var dy = Math.abs(y2 - y1);
  502. var sx = (x1 &lt; x2) ? 1 : -1;
  503. var sy = (y1 &lt; y2) ? 1 : -1;
  504. var err = dx - dy;
  505. results.push([x1, y1]);
  506. var i = 1;
  507. while (!((x1 == x2) && (y1 == y2)))
  508. {
  509. var e2 = err &lt;&lt; 1;
  510. if (e2 > -dy)
  511. {
  512. err -= dy;
  513. x1 += sx;
  514. }
  515. if (e2 &lt; dx)
  516. {
  517. err += dx;
  518. y1 += sy;
  519. }
  520. if (i % stepRate === 0)
  521. {
  522. results.push([x1, y1]);
  523. }
  524. i++;
  525. }
  526. return results;
  527. }
  528. };
  529. /**
  530. * @name Phaser.Line#length
  531. * @property {number} length - Gets the length of the line segment.
  532. * @readonly
  533. */
  534. Object.defineProperty(Phaser.Line.prototype, "length", {
  535. get: function () {
  536. return Math.sqrt((this.end.x - this.start.x) * (this.end.x - this.start.x) + (this.end.y - this.start.y) * (this.end.y - this.start.y));
  537. }
  538. });
  539. /**
  540. * @name Phaser.Line#angle
  541. * @property {number} angle - Gets the angle of the line.
  542. * @readonly
  543. */
  544. Object.defineProperty(Phaser.Line.prototype, "angle", {
  545. get: function () {
  546. return Math.atan2(this.end.x - this.start.x, this.end.y - this.start.y);
  547. }
  548. });
  549. /**
  550. * @name Phaser.Line#slope
  551. * @property {number} slope - Gets the slope of the line (y/x).
  552. * @readonly
  553. */
  554. Object.defineProperty(Phaser.Line.prototype, "slope", {
  555. get: function () {
  556. return (this.end.y - this.start.y) / (this.end.x - this.start.x);
  557. }
  558. });
  559. /**
  560. * @name Phaser.Line#perpSlope
  561. * @property {number} perpSlope - Gets the perpendicular slope of the line (x/y).
  562. * @readonly
  563. */
  564. Object.defineProperty(Phaser.Line.prototype, "perpSlope", {
  565. get: function () {
  566. return -((this.end.x - this.start.x) / (this.end.y - this.start.y));
  567. }
  568. });
  569. /**
  570. * @name Phaser.Line#x
  571. * @property {number} x - Gets the x coordinate of the top left of the bounds around this line.
  572. * @readonly
  573. */
  574. Object.defineProperty(Phaser.Line.prototype, "x", {
  575. get: function () {
  576. return Math.min(this.start.x, this.end.x);
  577. }
  578. });
  579. /**
  580. * @name Phaser.Line#y
  581. * @property {number} y - Gets the y coordinate of the top left of the bounds around this line.
  582. * @readonly
  583. */
  584. Object.defineProperty(Phaser.Line.prototype, "y", {
  585. get: function () {
  586. return Math.min(this.start.y, this.end.y);
  587. }
  588. });
  589. /**
  590. * @name Phaser.Line#left
  591. * @property {number} left - Gets the left-most point of this line.
  592. * @readonly
  593. */
  594. Object.defineProperty(Phaser.Line.prototype, "left", {
  595. get: function () {
  596. return Math.min(this.start.x, this.end.x);
  597. }
  598. });
  599. /**
  600. * @name Phaser.Line#right
  601. * @property {number} right - Gets the right-most point of this line.
  602. * @readonly
  603. */
  604. Object.defineProperty(Phaser.Line.prototype, "right", {
  605. get: function () {
  606. return Math.max(this.start.x, this.end.x);
  607. }
  608. });
  609. /**
  610. * @name Phaser.Line#top
  611. * @property {number} top - Gets the top-most point of this line.
  612. * @readonly
  613. */
  614. Object.defineProperty(Phaser.Line.prototype, "top", {
  615. get: function () {
  616. return Math.min(this.start.y, this.end.y);
  617. }
  618. });
  619. /**
  620. * @name Phaser.Line#bottom
  621. * @property {number} bottom - Gets the bottom-most point of this line.
  622. * @readonly
  623. */
  624. Object.defineProperty(Phaser.Line.prototype, "bottom", {
  625. get: function () {
  626. return Math.max(this.start.y, this.end.y);
  627. }
  628. });
  629. /**
  630. * @name Phaser.Line#width
  631. * @property {number} width - Gets the width of this bounds of this line.
  632. * @readonly
  633. */
  634. Object.defineProperty(Phaser.Line.prototype, "width", {
  635. get: function () {
  636. return Math.abs(this.start.x - this.end.x);
  637. }
  638. });
  639. /**
  640. * @name Phaser.Line#height
  641. * @property {number} height - Gets the height of this bounds of this line.
  642. * @readonly
  643. */
  644. Object.defineProperty(Phaser.Line.prototype, "height", {
  645. get: function () {
  646. return Math.abs(this.start.y - this.end.y);
  647. }
  648. });
  649. /**
  650. * Checks for intersection between two lines as defined by the given start and end points.
  651. * If asSegment is true it will check for line segment intersection. If asSegment is false it will check for line intersection.
  652. * Returns the intersection segment of AB and EF as a Point, or null if there is no intersection.
  653. * Adapted from code by Keith Hair
  654. *
  655. * @method Phaser.Line.intersectsPoints
  656. * @param {Phaser.Point} a - The start of the first Line to be checked.
  657. * @param {Phaser.Point} b - The end of the first line to be checked.
  658. * @param {Phaser.Point} e - The start of the second Line to be checked.
  659. * @param {Phaser.Point} f - The end of the second line to be checked.
  660. * @param {boolean} [asSegment=true] - If true it will check for segment intersection, otherwise full line intersection.
  661. * @param {Phaser.Point} [result] - A Point object to store the result in, if not given a new one will be created.
  662. * @return {Phaser.Point} The intersection segment of the two lines as a Point, or null if there is no intersection.
  663. */
  664. Phaser.Line.intersectsPoints = function (a, b, e, f, asSegment, result) {
  665. if (typeof asSegment === 'undefined') { asSegment = true; }
  666. if (typeof result === 'undefined') { result = new Phaser.Point(); }
  667. var a1 = b.y - a.y;
  668. var a2 = f.y - e.y;
  669. var b1 = a.x - b.x;
  670. var b2 = e.x - f.x;
  671. var c1 = (b.x * a.y) - (a.x * b.y);
  672. var c2 = (f.x * e.y) - (e.x * f.y);
  673. var denom = (a1 * b2) - (a2 * b1);
  674. if (denom === 0)
  675. {
  676. return null;
  677. }
  678. result.x = ((b1 * c2) - (b2 * c1)) / denom;
  679. result.y = ((a2 * c1) - (a1 * c2)) / denom;
  680. if (asSegment)
  681. {
  682. if (Math.pow((result.x - b.x) + (result.y - b.y), 2) > Math.pow((a.x - b.x) + (a.y - b.y), 2))
  683. {
  684. return null;
  685. }
  686. if (Math.pow((result.x - a.x) + (result.y - a.y), 2) > Math.pow((a.x - b.x) + (a.y - b.y), 2))
  687. {
  688. return null;
  689. }
  690. if (Math.pow((result.x - f.x) + (result.y - f.y), 2) > Math.pow((e.x - f.x) + (e.y - f.y), 2))
  691. {
  692. return null;
  693. }
  694. if (Math.pow((result.x - e.x) + (result.y - e.y), 2) > Math.pow((e.x - f.x) + (e.y - f.y), 2))
  695. {
  696. return null;
  697. }
  698. }
  699. return result;
  700. };
  701. /**
  702. * Checks for intersection between two lines.
  703. * If asSegment is true it will check for segment intersection.
  704. * If asSegment is false it will check for line intersection.
  705. * Returns the intersection segment of AB and EF as a Point, or null if there is no intersection.
  706. * Adapted from code by Keith Hair
  707. *
  708. * @method Phaser.Line.intersects
  709. * @param {Phaser.Line} a - The first Line to be checked.
  710. * @param {Phaser.Line} b - The second Line to be checked.
  711. * @param {boolean} [asSegment=true] - If true it will check for segment intersection, otherwise full line intersection.
  712. * @param {Phaser.Point} [result] - A Point object to store the result in, if not given a new one will be created.
  713. * @return {Phaser.Point} The intersection segment of the two lines as a Point, or null if there is no intersection.
  714. */
  715. Phaser.Line.intersects = function (a, b, asSegment, result) {
  716. return Phaser.Line.intersectsPoints(a.start, a.end, b.start, b.end, asSegment, result);
  717. };
  718. </pre>
  719. </article>
  720. </section>
  721. </div>
  722. <div class="clearfix"></div>
  723. <footer>
  724. <span class="copyright">
  725. Phaser Copyright © 2012-2014 Photon Storm Ltd.
  726. </span>
  727. <br />
  728. <span class="jsdoc-message">
  729. Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.0-dev</a>
  730. on Fri Apr 11 2014 13:25:03 GMT+0100 (BST) using the <a href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
  731. </span>
  732. </footer>
  733. </div>
  734. <br clear="both">
  735. </div>
  736. </div>
  737. <script src="scripts/sunlight.js"></script>
  738. <script src="scripts/sunlight.javascript.js"></script>
  739. <script src="scripts/sunlight-plugin.doclinks.js"></script>
  740. <script src="scripts/sunlight-plugin.linenumbers.js"></script>
  741. <script src="scripts/sunlight-plugin.menu.js"></script>
  742. <script src="scripts/jquery.min.js"></script>
  743. <script src="scripts/jquery.scrollTo.js"></script>
  744. <script src="scripts/jquery.localScroll.js"></script>
  745. <script src="scripts/bootstrap-dropdown.js"></script>
  746. <script src="scripts/toc.js"></script>
  747. <script> Sunlight.highlightAll({lineNumbers:true, showMenu: true, enableDoclinks :true}); </script>
  748. <script>
  749. $( function () {
  750. $( "#toc" ).toc( {
  751. anchorName : function(i, heading, prefix) {
  752. return $(heading).attr("id") || ( prefix + i );
  753. },
  754. selectors : "h1,h2,h3,h4",
  755. showAndHide : false,
  756. scrollTo : 60
  757. } );
  758. $( "#toc>ul" ).addClass( "nav nav-pills nav-stacked" );
  759. $( "#main span[id^='toc']" ).addClass( "toc-shim" );
  760. } );
  761. </script>
  762. </body>
  763. </html>