文章出處

一, 笛卡爾坐標系                        

笛卡爾坐標系是數學中的坐標系,而計算機中則采用屏幕坐標系統.

而三維坐標系則沒有一個工業標準,分別有 Y軸向上(y-up)的坐標系, Z軸向上(z-up)的坐標系, 右手坐標系(right-handed coordinate system), 左手坐標系(left-handed coordinate system).

下面的是y-up left-handed coordinate system

  數學中通常以括號加住的方式,如P(x,y,z)來表示點, 而程序中通常使用p<x,y,z>或p[x,y,z]表示點.代碼片段:

function Point2D(x, y){
  this.x = x
  this.y = y
}

function Point3D(x, y, z){
  this.x = x
  this.y = y
  this.z = z
}

 

二, 線                                              

  1. 求兩點之間的距離

    勾股定理: a2 + b2 = c2

     2D場景中, 假設要獲取P1(x1, y1)與P2(x2, y2)之間的距離d, 計算公式:

    

/* 
 *@param {Point2D} p1
 *@param {Point2D} p2
 *@return {number}
 */
function distance2D(p1, p2){
  var dX = p1.x - p2.x
    , dY = p1.y - p2.y
  return Math.sqrt(Math.pow(dX, 2) + Math.pow(dY, 2))
}

 

      3D場景中,假設要獲取P1(x1, y1, z1)與P2(x2, y2, z2)之間的距離d, 計算公式:

     

/* 
 *@param {Point3D} p1
 *@param {Point3D} p2
 *@return {number}
 */
function distance3D(p1, p2){
  var dX = p1.x - p2.x
    , dY = p1.y - p2.y
    , dZ = p1.z - p2.z
  return Math.sqrt(Math.pow(dX, 2) + Math.pow(dY, 2) + Math.pow(dZ, 2))
}

   2. 斜率

   數學公式:, slope為正數則直線橫穿第一和第三像限, 為負數則直線橫穿第二和第四像限.  

   直線公式:  y = slope * x + b , b為直線與Y軸交點離原點的偏移量.

   假設直線A為y1 = slope1 * x1 + b1, 而直線B為y2 = slope2 * x2 + b2

if (slope1 === slope2){
  console.log("A與B平行")
 
  if (b1 === b2)
     console.log("A與B重合")
}
else{
  console.log("A與B相交")

  if (-1 === slope1 * slope2)
    console.log("A與B相互垂直")
}

 

三, 拋物線                             

  拋物線分為4個方向

假設拋物線定點P(x1,y1)

Y軸對稱的拋物線: y = a(x - x1)2 + y1 , a為正數則開口向上, a為負數則開口向下.

X軸對稱的拋物線: x = a(y - y1)2 + x1, a為正數則開口向右, a為負數則開口向左.

 

四, 圓                            

   假設圓心坐標P(x1, y1), 半徑為r.

   公式: (x - x1)2 + (y - y1)2 = r2

 

/*
 * @constructor
 * @param {Point2D} point 圓心坐標
 * @param {number} r 半徑
 */
function Circle(point, r){
  this.point = point
  this.r = r
}

  碰撞算法基礎: 兩圓相切或相交

  公式:

/*
 *@param {Circle} c1
 *@param {Circle} c2
 *@return {boolean}
 */
function isHit(c1, c2){
  var dCenter = Math.sqrt(Math.pow(c1.center.x - c2.center.x, 2) + Math.pow(c1.center.y - c2.center.y, 2))
  var totalRadius = c1.r + c2.r

  return dCenter <= totalRadius
}

 

五, 球(3D)                          

  假設球心坐標P(x1,y1,z1), 半徑為r

  公式: (x - x1)2 + (y - y1)2 + (z - z1)2 = r2

/*
 *@param {Point3D} center
 *@param {number} r
 */
function Sphere(center, r){
  this.center = center
  this.radius = r
}

   碰撞算法基礎: 兩球相切或相交

/*
 *@param {Sphere} c1
 *@param {Sphere} c2
 *@return {boolean}
 */
function isHit(c1, c2){
  var dCenter = Math.sqrt(Math.pow(c1.center.x - c2.center.x, 2) + Math.pow(c1.center.y - c2.center.y, 2) + Math.pow(c1.center.z - c2.center.z, 2))
  var totalRadius = c1.r + c2.r

  return dCenter <= totalRadius
}

 

六, 弧度和角度                        

  以笛卡爾坐標系的P(0,0)作為角點. 初始邊為X軸的正半軸, 終邊與初始邊構成一個夾角.

  初始邊逆時針旋轉得到的夾角度數為正值, 順時針旋轉得到的夾角度數為負值.

   π≈3.141592654

    角度:degree=radian*180

    弧度:radian=degree*π/180

 

function getDegree(radian){
  return radian * 180 / Math.PI
}

function getRadian(degree){
  return degree * Math.PI / 180
}

 

 

 

 參考: http://www.cnblogs.com/HelloCG/ 


文章列表




Avast logo

Avast 防毒軟體已檢查此封電子郵件的病毒。
www.avast.com


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()