Path: blob/trunk/third_party/closure/goog/math/paths.js
2868 views
// Copyright 2010 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.131415/**16* @fileoverview Factories for common path types.17* @author [email protected] (Nick Santos)18*/192021goog.provide('goog.math.paths');2223goog.require('goog.math.Coordinate');24goog.require('goog.math.Path');252627/**28* Defines a regular n-gon by specifing the center, a vertex, and the total29* number of vertices.30* @param {goog.math.Coordinate} center The center point.31* @param {goog.math.Coordinate} vertex The vertex, which implicitly defines32* a radius as well.33* @param {number} n The number of vertices.34* @return {!goog.math.Path} The path.35*/36goog.math.paths.createRegularNGon = function(center, vertex, n) {37var path = new goog.math.Path();38path.moveTo(vertex.x, vertex.y);3940var startAngle = Math.atan2(vertex.y - center.y, vertex.x - center.x);41var radius = goog.math.Coordinate.distance(center, vertex);42for (var i = 1; i < n; i++) {43var angle = startAngle + 2 * Math.PI * (i / n);44path.lineTo(45center.x + radius * Math.cos(angle),46center.y + radius * Math.sin(angle));47}48path.close();49return path;50};515253/**54* Defines an arrow.55* @param {goog.math.Coordinate} a Point A.56* @param {goog.math.Coordinate} b Point B.57* @param {?number} aHead The size of the arrow head at point A.58* 0 omits the head.59* @param {?number} bHead The size of the arrow head at point B.60* 0 omits the head.61* @return {!goog.math.Path} The path.62*/63goog.math.paths.createArrow = function(a, b, aHead, bHead) {64var path = new goog.math.Path();65path.moveTo(a.x, a.y);66path.lineTo(b.x, b.y);6768var angle = Math.atan2(b.y - a.y, b.x - a.x);69if (aHead) {70path.appendPath(71goog.math.paths.createRegularNGon(72new goog.math.Coordinate(73a.x + aHead * Math.cos(angle), a.y + aHead * Math.sin(angle)),74a, 3));75}76if (bHead) {77path.appendPath(78goog.math.paths.createRegularNGon(79new goog.math.Coordinate(80b.x + bHead * Math.cos(angle + Math.PI),81b.y + bHead * Math.sin(angle + Math.PI)),82b, 3));83}84return path;85};868788