可能重复:
如何计算一组角度的平均值?
我有两个角度,a = 20度,b = 350度.这两个角度的平均值是185度.但是,如果我们认为最大角度是360度并且允许环绕,则可以看到5度是更接近的平均值.
在计算平均值时,我遇到了一个很好的公式来解决这个问题.有人有任何提示吗?
还是我在这里拍脚?这被认为是数学中的"坏习惯"吗?
我有一个代码将一个numpy数组切成一个圆圈.我希望仅从圆圈中恢复包含在特定角度范围内的值并掩盖数组.例如:屏蔽原始数组,其中(x,y)位置包含在圆圈的0到45度之间.
有这样做的pythonic方式吗?
这是我的(简化)原始代码:
import numpy as np
matrix = np.zeros((500,500))
x = 240
y = 280
radius = 10
mask=np.ogrid[x-radius:x+radius+1,y-radius:y+radius+1]
matrix[mask]
Run Code Online (Sandbox Code Playgroud)
提前致谢
编辑:我省略了半径可以变化.
我使用UIBezierPath和CAShapeLayer在iOS中编码了半圈.
clockWiseLayer = [[CAShapeLayer alloc] init];
CGFloat startAngle = -M_PI_2;
CGFloat endAngle = M_PI + M_PI_2;
CGFloat width = CGRectGetWidth(imageView.frame)/2.0f + 30;
CGFloat height = CGRectGetHeight(imageView.frame)/2.0f +50;
CGPoint centerPoint = CGPointMake(width, height);
float radius = CGRectGetWidth(imageView.frame)/2+10;
clockWiseLayer.path = [UIBezierPath bezierPathWithArcCenter:centerPoint
radius:radius
startAngle:startAngle
endAngle:endAngle
clockwise:YES].CGPath;
clockWiseLayer.fillColor = [UIColor clearColor].CGColor;
clockWiseLayer.strokeColor = [UIColor blueColor].CGColor;
clockWiseLayer.borderColor = [UIColor greenColor].CGColor;
clockWiseLayer.backgroundColor = [UIColor redColor].CGColor;
clockWiseLayer.strokeStart = 0.0f;
clockWiseLayer.strokeEnd = 0.5f;
clockWiseLayer.lineWidth = 2.0f;
clockWiseLayer.borderWidth = 5.0f;
clockWiseLayer.shouldRasterize = NO;
[self.layer …Run Code Online (Sandbox Code Playgroud) 我一直试图找到2之间的区别,但没有运气减去这个
两个表示之间的主要差异在于四元数的旋转轴由半旋转角的正弦缩放,而不是将角度存储在矢量的第四个分量中,我们存储半角的余弦.
我不知道是什么
半角旋转的正弦
要么
半角的余弦
手段?
可能重复:
如何计算两点相对于水平轴的角度?
我一直在寻找这个年龄,这真的很烦我,所以我决定只问...
提供我有两个点(即X1,Y1,和X2,Y2),我想计算这两个点之间的角度,假设的是,当Y1 == y 2和X1> X2的角度为180度...
我有以下代码,我一直在使用(使用高中的知识),我似乎无法产生预期的结果.
float xDiff = x1 - x2;
float yDiff = y1 - y2;
return (float)Math.Atan2(yDiff, xDiff) * (float)(180 / Math.PI);
Run Code Online (Sandbox Code Playgroud)
提前谢谢,我很沮丧......
所以我正在使用罗盘角度(以度为单位)进行应用.我已经设法确定角度均值的计算,使用以下内容(可在http://en.wikipedia.org/wiki/Directional_statistics#The_fundamental_difference_between_linear_and_circular_statistics中找到):
double calcMean(ArrayList<Double> angles){
double sin = 0;
double cos = 0;
for(int i = 0; i < angles.size(); i++){
sin += Math.sin(angles.get(i) * (Math.PI/180.0));
cos += Math.cos(angles.get(i) * (Math.PI/180.0));
}
sin /= angles.size();
cos /= angles.size();
double result =Math.atan2(sin,cos)*(180/Math.PI);
if(cos > 0 && sin < 0) result += 360;
else if(cos < 0) result += 180;
return result;
}
Run Code Online (Sandbox Code Playgroud)
所以我正确地得到了我的平均值/平均值,但是我无法获得正确的方差/ stddev值.我很确定我正在计算我的方差不正确,但想不出正确的方法.
这是我如何计算方差:
double calcVariance(ArrayList<Double> angles){
//THIS IS WHERE I DON'T KNOW WHAT TO PUT
ArrayList<Double> normalizedList = …Run Code Online (Sandbox Code Playgroud) 我正在用pygame制作小游戏,我制作了一支围绕其中心旋转的枪.我的问题是我希望枪自己向敌方方向旋转,但是我不能这样做因为我找不到枪和敌人之间的角度让枪旋转到它我已经搜索过了我发现我必须使用atan2但我没有找到任何正常工作的代码所以我希望有人可以帮助我.
这是我的代码:
import pygame
from pygame.locals import*
pygame.init()
height=650
width=650
screen=pygame.display.set_mode((height,width))
clock=pygame.time.Clock()
gun=pygame.image.load("m2.png").convert_alpha()
gun=pygame.transform.smoothscale(gun,(200,200)).convert_alpha()
angle=0
angle_change=0
RED=(255,0,0)
x=525
y=155
while True :
screen.fill((150,150,150))
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
quit()
if event.type==KEYDOWN:
if event.key==K_a:
angle_change=+1
if event.key==K_d:
angle_change=-1
elif event.type==KEYUP:
angle_change=0
angle+=angle_change
if angle>360:
angle=0
if angle<0:
angle=360
pygame.draw.rect(screen,RED,(x,y,64,64))
position = (height/2,width/2)
gun_rotate=pygame.transform.rotate(gun,angle)
rotate_rect = gun_rotate.get_rect()
rotate_rect.center = position
screen.blit(gun_rotate, rotate_rect)
pygame.display.update()
clock.tick(60)
Run Code Online (Sandbox Code Playgroud)
这是一张试图说清楚的图片:
谢谢.
我想这并不难,但我已经坚持了一段时间.
我有一个可以旋转两个方向的关节.传感器给出了-pi和+ pi范围内关节的角度.
我想在-infinity和+ infinity范围内转换它.意味着如果关节永远顺时针旋转,角度将从0开始,然后增加到无穷大.在matlab中,unwrap函数非常好用:
newAngle = unwrap([previousAngle newAngle]);
previousAngle = newAngle;
Run Code Online (Sandbox Code Playgroud)
注意:假设角度没有大跳跃,肯定没有优于PI.
注意:在问之前我真的很努力
谢谢 !
我需要计算两点之间的角度,其中一个固定点通过一条线与给定的两点相连.
这是一张图片,说明了我的需求:

这是我到目前为止所尝试的:
public static float GetAngleOfLineBetweenTwoPoints(float x1, float x2, float y1, float y2) {
float xDiff = x2 - x1;
float yDiff = y2 - y1;
return (float) (Math.atan2(yDiff, xDiff) * (180 / Math.PI));
}
Run Code Online (Sandbox Code Playgroud)
说它没有提供正确答案毫无意义.