| 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 |
|
| 16 |
/** |
| 17 |
* @fileoverview Factories for common path types. |
| 18 |
* @author [email protected] (Nick Santos) |
| 19 |
*/ |
| 20 |
|
| 21 |
|
| 22 |
goog.provide('goog.graphics.paths'); |
| 23 |
|
| 24 |
goog.require('goog.graphics.Path'); |
| 25 |
goog.require('goog.math.Coordinate'); |
| 26 |
|
| 27 |
|
| 28 |
/** |
| 29 |
* Defines a regular n-gon by specifing the center, a vertex, and the total |
| 30 |
* number of vertices. |
| 31 |
* @param {goog.math.Coordinate} center The center point. |
| 32 |
* @param {goog.math.Coordinate} vertex The vertex, which implicitly defines |
| 33 |
* a radius as well. |
| 34 |
* @param {number} n The number of vertices. |
| 35 |
* @return {!goog.graphics.Path} The path. |
| 36 |
*/ |
| 37 |
goog.graphics.paths.createRegularNGon = function(center, vertex, n) { |
| 38 |
var path = new goog.graphics.Path(); |
| 39 |
path.moveTo(vertex.x, vertex.y); |
| 40 |
|
| 41 |
var startAngle = Math.atan2(vertex.y - center.y, vertex.x - center.x); |
| 42 |
var radius = goog.math.Coordinate.distance(center, vertex); |
| 43 |
for (var i = 1; i < n; i++) { |
| 44 |
var angle = startAngle + 2 * Math.PI * (i / n); |
| 45 |
path.lineTo( |
| 46 |
center.x + radius * Math.cos(angle), |
| 47 |
center.y + radius * Math.sin(angle)); |
| 48 |
} |
| 49 |
path.close(); |
| 50 |
return path; |
| 51 |
}; |
| 52 |
|
| 53 |
|
| 54 |
/** |
| 55 |
* Defines an arrow. |
| 56 |
* @param {goog.math.Coordinate} a Point A. |
| 57 |
* @param {goog.math.Coordinate} b Point B. |
| 58 |
* @param {?number} aHead The size of the arrow head at point A. |
| 59 |
* 0 omits the head. |
| 60 |
* @param {?number} bHead The size of the arrow head at point B. |
| 61 |
* 0 omits the head. |
| 62 |
* @return {!goog.graphics.Path} The path. |
| 63 |
*/ |
| 64 |
goog.graphics.paths.createArrow = function(a, b, aHead, bHead) { |
| 65 |
var path = new goog.graphics.Path(); |
| 66 |
path.moveTo(a.x, a.y); |
| 67 |
path.lineTo(b.x, b.y); |
| 68 |
|
| 69 |
var angle = Math.atan2(b.y - a.y, b.x - a.x); |
| 70 |
if (aHead) { |
| 71 |
path.appendPath( |
| 72 |
goog.graphics.paths.createRegularNGon( |
| 73 |
new goog.math.Coordinate( |
| 74 |
a.x + aHead * Math.cos(angle), a.y + aHead * Math.sin(angle)), |
| 75 |
a, 3)); |
| 76 |
} |
| 77 |
if (bHead) { |
| 78 |
path.appendPath( |
| 79 |
goog.graphics.paths.createRegularNGon( |
| 80 |
new goog.math.Coordinate( |
| 81 |
b.x + bHead * Math.cos(angle + Math.PI), |
| 82 |
b.y + bHead * Math.sin(angle + Math.PI)), |
| 83 |
b, 3)); |
| 84 |
} |
| 85 |
return path; |
| 86 |
}; |