Last active
December 25, 2017 00:50
-
-
Save koyo-miyamura/08e14f58aa1f9e69c82fe51102835f39 to your computer and use it in GitHub Desktop.
角度平均を求めるプログラム(-180.0 ~ 180.0)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 構造体使用ver | |
import java.lang.Math; | |
public class Angle_average{ | |
public static void main(String[] args){ | |
final class vector{ | |
double x; | |
double y; | |
} | |
System.out.println("角度の平均"); | |
// 変数宣言・初期化 | |
double ang_avg; | |
// -----ここに平均を求めたい角度を入力----- | |
double[] angles = {350.0, 20.0}; | |
int len = angles.length; | |
double[] angles_rad = new double[len]; | |
vector[] vectors = new vector[len]; | |
vector vector_avg = new vector(); | |
for (int i = 0; i < vectors.length; i++) { | |
vectors[i] = new vector(); | |
} | |
for (int i = 0; i < angles.length; i++) { | |
//angles_rad[i] = angles[i] * (Math.PI / 180.0); | |
angles_rad[i] = Math.toRadians(angles[i]); | |
vectors[i].x = Math.cos(angles_rad[i]); | |
vectors[i].y = Math.sin(angles_rad[i]); | |
} | |
// 平均を求める | |
vector_avg.x = 0.0; | |
vector_avg.y = 0.0; | |
for (int i = 0; i < vectors.length; i++) { | |
vector_avg.x += vectors[i].x; | |
vector_avg.y += vectors[i].y; | |
} | |
vector_avg.x = vector_avg.x / (double)vectors.length; | |
vector_avg.y = vector_avg.y / (double)vectors.length; | |
// ベクトルから角度に変換 | |
ang_avg = Math.atan2(vector_avg.y, vector_avg.x); | |
// 計算誤差の補正 | |
if(Math.abs(ang_avg) < Math.pow(10, -8)){ | |
ang_avg = 0.0; | |
} | |
System.out.println(Double.toString(Math.toDegrees(ang_avg)) + "\n"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 構造体未使用ver | |
import java.lang.Math; | |
public class Angle_average_back{ | |
public static void main(String[] args){ | |
System.out.println("角度の平均"); | |
// 変数宣言・初期化 | |
double ang_avg; | |
// -----ここに平均を求めたい角度を入力----- | |
double[] angles = {10.0, 350.0}; | |
int len = angles.length; | |
double[] angles_rad = new double[len]; | |
double[] vectors_x = new double[len]; | |
double[] vectors_y = new double[len]; | |
double vector_avg_x = 0.0; | |
double vector_avg_y = 0.0; | |
for (int i = 0; i < angles.length; i++) { | |
//angles_rad[i] = angles[i] * (Math.PI / 180.0); | |
angles_rad[i] = Math.toRadians(angles[i]); | |
vectors_x[i] = Math.cos(angles_rad[i]); | |
vectors_y[i] = Math.sin(angles_rad[i]); | |
} | |
// 平均を求める | |
for (int i = 0; i < len; i++) { | |
vector_avg_x += vectors_x[i]; | |
vector_avg_y += vectors_y[i]; | |
} | |
vector_avg_x = vector_avg_x / (double)len; | |
vector_avg_y = vector_avg_y / (double)len; | |
// ベクトルから角度に変換 | |
ang_avg = Math.atan2(vector_avg_y, vector_avg_x); | |
// 計算誤差の補正 | |
if(Math.abs(ang_avg) < Math.pow(10, -8)){ | |
ang_avg = 0.0; | |
} | |
System.out.println(Double.toString(Math.toDegrees(ang_avg)) + "\n"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://qiita.com/koyo-miyamura/items/37d555ae150047ebefde