paths.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright 2010 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Factories for common path types.
  16. * @author nicksantos@google.com (Nick Santos)
  17. */
  18. goog.provide('goog.graphics.paths');
  19. goog.require('goog.graphics.Path');
  20. goog.require('goog.math.Coordinate');
  21. /**
  22. * Defines a regular n-gon by specifing the center, a vertex, and the total
  23. * number of vertices.
  24. * @param {goog.math.Coordinate} center The center point.
  25. * @param {goog.math.Coordinate} vertex The vertex, which implicitly defines
  26. * a radius as well.
  27. * @param {number} n The number of vertices.
  28. * @return {!goog.graphics.Path} The path.
  29. */
  30. goog.graphics.paths.createRegularNGon = function(center, vertex, n) {
  31. var path = new goog.graphics.Path();
  32. path.moveTo(vertex.x, vertex.y);
  33. var startAngle = Math.atan2(vertex.y - center.y, vertex.x - center.x);
  34. var radius = goog.math.Coordinate.distance(center, vertex);
  35. for (var i = 1; i < n; i++) {
  36. var angle = startAngle + 2 * Math.PI * (i / n);
  37. path.lineTo(
  38. center.x + radius * Math.cos(angle),
  39. center.y + radius * Math.sin(angle));
  40. }
  41. path.close();
  42. return path;
  43. };
  44. /**
  45. * Defines an arrow.
  46. * @param {goog.math.Coordinate} a Point A.
  47. * @param {goog.math.Coordinate} b Point B.
  48. * @param {?number} aHead The size of the arrow head at point A.
  49. * 0 omits the head.
  50. * @param {?number} bHead The size of the arrow head at point B.
  51. * 0 omits the head.
  52. * @return {!goog.graphics.Path} The path.
  53. */
  54. goog.graphics.paths.createArrow = function(a, b, aHead, bHead) {
  55. var path = new goog.graphics.Path();
  56. path.moveTo(a.x, a.y);
  57. path.lineTo(b.x, b.y);
  58. var angle = Math.atan2(b.y - a.y, b.x - a.x);
  59. if (aHead) {
  60. path.appendPath(
  61. goog.graphics.paths.createRegularNGon(
  62. new goog.math.Coordinate(
  63. a.x + aHead * Math.cos(angle), a.y + aHead * Math.sin(angle)),
  64. a, 3));
  65. }
  66. if (bHead) {
  67. path.appendPath(
  68. goog.graphics.paths.createRegularNGon(
  69. new goog.math.Coordinate(
  70. b.x + bHead * Math.cos(angle + Math.PI),
  71. b.y + bHead * Math.sin(angle + Math.PI)),
  72. b, 3));
  73. }
  74. return path;
  75. };