Angle

Angle提供一些列函数或成员来操作与空间角度相关的操作

源码

@requires jsts/algorithm/CGAlgorithms.js

#构造函数

jsts.algorithm.Angle = function() {};

成员-- 2π

jsts.algorithm.Angle.PI_TIMES_2 = 2.0 * Math.PI;

成员-- π/2

jsts.algorithm.Angle.PI_OVER_2 = Math.PI / 2.0;

成员--π/4

jsts.algorithm.Angle.PI_OVER_4 = Math.PI / 4.0;

顺时针

jsts.algorithm.Angle.COUNTERCLOCKWISE = jsts.algorithm.CGAlgorithms.COUNTERCLOCKWISE;

逆时针

jsts.algorithm.Angle.CLOCKWISE = jsts.algorithm.CGAlgorithms.CLOCKWISE;

无方向性角度

jsts.algorithm.Angle.NONE = jsts.algorithm.CGAlgorithms.COLLINEAR;

弧度转度

jsts.algorithm.Angle.toDegrees = function(radians) {
  return (radians * 180) / Math.PI;
};

度转弧度

jsts.algorithm.Angle.toRadians = function(angleDegrees) {
  return (angleDegrees * Math.PI) / 180.0;
};

计算角度

返回给定点的角度,如果只有一个坐标,则返回该点和原点连线的角度,如果参数有两个或多个,返回坐标直接的角度

jsts.algorithm.Angle.angle = function() {
  if (arguments.length === 1) {
    return jsts.algorithm.Angle.angleFromOrigo(arguments[0]);
  }else {
    return jsts.algorithm.Angle.angleBetweenCoords(arguments[0], arguments[1]);
  }
};

返回p0到p1向量的角度信息

/**
 * Returns the angle of the vector from p0 to p1,
 * relative to the positive X-axis.
 * The angle is normalized to be in the range [ -Pi, Pi ].
 *
 * @param {jsts.geom.Coordinate}     p0 a coordinate.
 * @param {jsts.geom.Coordinate}     p1 a coordinate.
 * @return {Number}
 *         the normalized angle (in radians) that p0-p1 makes with the positive        x-axis.
 */
jsts.algorithm.Angle.angleBetweenCoords = function(p0, p1) {
  var dx, dy;
  dx = p1.x - p0.x;
  dy = p1.y - p0.y;
  return Math.atan2(dy, dx);
};

返回坐标原点到P点的向量角度

/**
 * Returns the angle that the vector from (0,0) to p,
 * relative to the positive X-axis.
 * The angle is normalized to be in the range ( -Pi, Pi ].
 *
 * @param {jsts.geom.Coordinate}     p a coordinate.
 * @return {Number}     the normalized angle (in radians) that p makes with the positive   x-axis.
 */
jsts.algorithm.Angle.angleFromOrigo = function(p) {
  return Math.atan2(p.y, p.x);
};

返回p0 ->p1->p2构成的角度是否为锐角

/**
 * Tests whether the angle between p0-p1-p2 is acute.
 * An angle is acute if it is less than 90 degrees.
 * <p>
 * Note: this implementation is not precise (determistic) for angles very close to 90 degrees.
 *
 * @param {jsts.geom.Coordinate}    p0 an endpoint of the angle.
 * @param {jsts.geom.Coordinate}    p1 the base of the angle.
 * @param {jsts.geom.Coordinate}    p2 the other endpoint of the angle.
 * @return {Boolean}
 *         true if the angle is acute.
 */
jsts.algorithm.Angle.isAcute = function(p0, p1, p2) {
  var dx0, dy0, dx1, dy1, dotprod;
  //relies on fact that A dot B is positive if A ang B is acute
  dx0 = p0.x - p1.x;
  dy0 = p0.y - p1.y;
  dx1 = p2.x - p1.x;
  dy1 = p2.y - p1.y;
  dotprod = dx0 * dx1 + dy0 * dy1;
  return dotprod > 0;
};

返回p0->p1->p2直接的角度是否为钝角

/**
 * Tests whether the angle between p0-p1-p2 is obtuse.
 * An angle is obtuse if it is greater than 90 degrees.
 * <p>
* 当角度非常接近90度的时候结果可能不是真精确
 *
 * @param {jsts.geom.Coordinate}      p0 an endpoint of the angle.
 * @param {jsts.geom.Coordinate}      p1 the base of the angle.
 * @param {jsts.geom.Coordinate}      p2 the other endpoint of the angle.
 * @return {Boolean}      true if the angle is obtuse.
 */
jsts.algorithm.Angle.isObtuse = function(p0, p1, p2) {
  var dx0, dy0, dx1, dy1, dotprod;
  //relies on fact that A dot B is negative iff A ang B is obtuse
  dx0 = p0.x - p1.x;
  dy0 = p0.y - p1.y;
  dx1 = p2.x - p1.x;
  dy1 = p2.y - p1.y;
  dotprod = dx0 * dx1 + dy0 * dy1;
  return dotprod < 0;
};

返回两个向量之间的最近小角度,无方向性的,值再[0,Pi)之间

/**
 * Returns the unoriented smallest angle between two vectors.
 * The computed angle will be in the range [0, Pi).
  * @param {jsts.geom.Coordinate}      tip1 the tip of one vector.
 * @param {jsts.geom.Coordinate}       tail the tail of each vector.
 * @param {jsts.geom.Coordinate}       tip2 the tip of the other vector.
 * @return {Number}     the angle between tail-tip1 and tail-tip2.
 */
jsts.algorithm.Angle.angleBetween = function(tip1, tail, tip2) {
  var a1, a2;
  a1 = jsts.algorithm.Angle.angle(tail, tip1);
  a2 = jsts.algorithm.Angle.angle(tail, tip2);
  return jsts.algorithm.Angle.diff(a1, a2);
};

返回两个向量之间的角度(有方向性 V1->V2),值应该在(-Pi,Pi]之间

/**
 * Returns the oriented smallest angle between two vectors.
 * The computed angle will be in the range (-Pi, Pi].
 * A positive result corresponds to a counterclockwise rotation
 * from v1 to v2;
 * a negative result corresponds to a clockwise rotation.
 *
 * @param {jsts.geom.Coordinate}    tip1 the tip of v1.
 * @param {jsts.geom.Coordinate}    tail the tail of each vector.
 * @param {jsts.geom.Coordinate}     tip2 the tip of v2.
 * @return {Number}     the angle between v1 and v2, relative to v1.
 */
jsts.algorithm.Angle.angleBetweenOriented = function(tip1, tail, tip2) {
  var a1, a2, angDel;
  a1 = jsts.algorithm.Angle.angle(tail, tip1);
  a2 = jsts.algorithm.Angle.angle(tail, tip2);
  angDel = a2 - a1;
  // normalize, maintaining orientation
  if (angDel <= -Math.PI) {
    return angDel + jsts.algorithm.Angle.PI_TIMES_2;
  }
  if (angDel > Math.PI) {
    return angDel - jsts.algorithm.Angle.PI_TIMES_2;
  }
  return angDel;
};

计算环Ring内部两条线段的角度 值再[0,2Pi]之间

/**
 * Computes the interior angle between two segments of a ring. The ring is
 * assumed to be oriented in a clockwise direction. The computed angle will be
 * in the range [0, 2Pi]
 *
 * @param {jsts.geom.Coordinate}    p0 a point of the ring.
 * @param {jsts.geom.Coordinate}    p1 the next point of the ring.
 * @param {jsts.geom.Coordinate}   p2 the next point of the ring.
 * @return {Number}   the interior angle based at <code>p1.</code>
 */
jsts.algorithm.Angle.interiorAngle = function(p0, p1, p2) {
  var anglePrev, angleNext;
  anglePrev = jsts.algorithm.Angle.angle(p1, p0);
  angleNext = jsts.algorithm.Angle.angle(p1, p2);
  return Math.abs(angleNext - anglePrev);
};

判断两个角度直接是否为顺时针还是逆时针

/**
 * Returns whether an angle must turn clockwise or counterclockwise  to overlap another angle.

 * @param {Number}     ang1 an angle (in radians).
 * @param {Number}     ang2 an angle (in radians).
 * @return {Number}     whether a1 must turn CLOCKWISE, COUNTERCLOCKWISE or NONE to  overlap a2.
 */
jsts.algorithm.Angle.getTurn = function(ang1, ang2) {
  var crossproduct = Math.sin(ang2 - ang1);
  if (crossproduct > 0) {
    return jsts.algorithm.Angle.COUNTERCLOCKWISE;
  }
  if (crossproduct < 0) {
    return jsts.algorithm.Angle.CLOCKWISE;
  }
  return jsts.algorithm.Angle.NONE;
};

/**
 * Computes the normalized value of an angle, which is the
 * equivalent angle in the range ( -Pi, Pi ].
 * @param {Number}     angle the angle to normalize.
 * @return {Number}     an equivalent angle in the range (-Pi, Pi].
 */ 
jsts.algorithm.Angle.normalize = function(angle) {
  while (angle > Math.PI) {
    angle -= jsts.algorithm.Angle.PI_TIMES_2;
  }
  while (angle <= -Math.PI) {
    angle += jsts.algorithm.Angle.PI_TIMES_2;
  }
  return angle;
};

规则化一个正角度值,使值在[0, 2Pi]之间

/**
 * Computes the normalized positive value of an angle, which is the
 * equivalent angle in the range [ 0, 2*Pi ).
 * E.g.:
 * <ul>
 * <li>normalizePositive(0.0) = 0.0
 * <li>normalizePositive(-PI) = PI
 * <li>normalizePositive(-2PI) = 0.0
 * <li>normalizePositive(-3PI) = PI
 * <li>normalizePositive(-4PI) = 0
 * <li>normalizePositive(PI) = PI
 * <li>normalizePositive(2PI) = 0.0
 * <li>normalizePositive(3PI) = PI
 * <li>normalizePositive(4PI) = 0.0
 * </ul>
 *
 * @param {Number}
 *        angle the angle to normalize, in radians.
 * @return {Number}
 *         an equivalent positive angle.
 */
jsts.algorithm.Angle.normalizePositive = function(angle) {
  if (angle < 0.0) {
    while (angle < 0.0) {
      angle += jsts.algorithm.Angle.PI_TIMES_2;
    }
    // in case round-off error bumps the value over
    if (angle >= jsts.algorithm.Angle.PI_TIMES_2) {
      angle = 0.0;
     }
  }
  else {
    while (angle >= jsts.algorithm.Angle.PI_TIMES_2) {
      angle -= jsts.algorithm.Angle.PI_TIMES_2;
    }
    // in case round-off error bumps the value under
    if (angle < 0.0) {
      angle = 0.0;
    }
  }
  return angle;
};

计算两个无向角度之间的最小差值

/**
 * Computes the unoriented smallest difference between two angles.
 * The angles are assumed to be normalized to the range [-Pi, Pi].
 * The result will be in the range [0, Pi].
 *
 * @param {Number}   ang1 the angle of one vector (in [-Pi, Pi] ).
 * @param {Number}   ang2 the angle of the  other vector (in range [-Pi, Pi] ).
 * @return {Number}   the angle (in radians) between the two vectors (in range [0, Pi] ).
 */
jsts.algorithm.Angle.diff = function(ang1, ang2) {
  var delAngle;
  if (ang1 < ang2) {
    delAngle = ang2 - ang1;
  } else {
    delAngle = ang1 - ang2;
  }
  if (delAngle > Math.PI) {
    delAngle = (2 * Math.PI) - delAngle;
  }
  return delAngle;
};
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 213,864评论 6 494
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,175评论 3 387
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 159,401评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,170评论 1 286
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,276评论 6 385
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,364评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,401评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,179评论 0 269
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,604评论 1 306
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,902评论 2 328
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,070评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,751评论 4 337
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,380评论 3 319
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,077评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,312评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,924评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,957评论 2 351

推荐阅读更多精彩内容