Skip to content

Instantly share code, notes, and snippets.

@sdkfz181tiger
Created October 27, 2013 11:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sdkfz181tiger/7180581 to your computer and use it in GitHub Desktop.
Save sdkfz181tiger/7180581 to your computer and use it in GitHub Desktop.
2点のなす角度を計算する
//
// UtilMath.cpp
// HelloCpp
//
// Created by Shimeji Ozaki on 2013-10-06.
//
//
#include "UtilMath.h"
// PI
float UtilMath::getPI(){
return PI;
}
// RadToDeg
float UtilMath::getRadToDeg(){
return RadToDeg;
}
// DegToRad
float UtilMath::getDegToRad(){
return DegToRad;
}
// distance
float UtilMath::calcDistance2D(const Point& pointA, const Point& pointB){
float distance = sqrt(powf(pointA.x - pointB.x, 2) + powf(pointA.y - pointB.y, 2));
return distance;
}
// angle(1Point)
float UtilMath::calcAngle2D(const Point& pointCenter, const Point& pointA){
// SamePoint
if(pointCenter.x == pointA.x && pointCenter.y == pointA.y){
return 0.0f;
}
// Horizontal
if(pointCenter.x == pointA.x){
if(pointCenter.y < pointA.y){
return +90.0f;
}else{
return -90.0f;
}
}
// Vertical
if(pointCenter.y == pointA.y){
if(pointCenter.x < pointA.x){
return 0.0f;
}else{
return 180.0f;
}
}
float angle = (float)atan((pointA.y - pointCenter.y) / (pointA.x - pointCenter.x)) * RadToDeg;
if(pointA.y > pointCenter.y && pointA.x > pointCenter.x){
// 1div
return angle;
}else if(pointA.y > pointCenter.y && pointA.x < pointCenter.x){
// 2div
return angle + 180;
}else if(pointA.y < pointCenter.y && pointA.x < pointCenter.x){
// 3div
return angle + 180;
}else{
// 4div
return angle + 360;
}
}
// angle(2Point)
float UtilMath::calcAngle2P2D(const Point& pointCenter, const Point& pointA, const Point& pointB){
//TODO +-
float angleA = calcAngle2D(pointCenter, pointA);
float angleB = calcAngle2D(pointCenter, pointB);
float angleDistance = fabs(angleA - angleB);
if(angleDistance > 180){
angleDistance = 360 - angleDistance;
}
if(angleA)
return angleDistance;
}
//
// UtilMath.h
// HelloCpp
//
// Created by Shimeji Ozaki on 2013-10-06.
//
//
#ifndef __HelloCpp__UtilMath__
#define __HelloCpp__UtilMath__
#define PI 3.141592653f
#define RadToDeg 57.29577951f
#define DegToRad 0.017453293f
#include "cocos2d.h"
USING_NS_CC;
class UtilMath{
public:
static float getPI();
static float getRadToDeg();
static float getDegToRad();
static float calcDistance2D(const Point& pointA, const Point& pointB);
// centerとのなす角
static float calcAngle2D(const Point& pointCenter, const Point& pointA);
// centerから見た、pointAとpointBの角度差
static float calcAngle2P2D(const Point& pointCenter, const Point& pointA, const Point& pointB);
};
#endif /* defined(__HelloCpp__UtilMath__) */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment