1,需求
旋转角度(CW/CCW)的累加计算
2,代码实现
class AngleProcess:def __init__(self, is_cw, period=360):self.is_cw = is_cwself.period = periodself.half_period = period / 2self.init_angle_0_and_circle_num()def check_if_add_one_rotation(self, is_cw, pre_angle, cur_angle, half_period=180):"""如果是 cw :pre_angle 到 360, cur_angle 到 0。如何表达这个?意思是下降了 180+如果是 ccw :pre_angle 到 0, cur_angle 到 360。如何表达这个?意思是上升了 180+"""if is_cw:if pre_angle - cur_angle > half_period:is_add_one_rotation = Trueelse:is_add_one_rotation = Falseelse:if cur_angle - pre_angle > half_period:is_add_one_rotation = Trueelse:is_add_one_rotation = Falsereturn is_add_one_rotationdef init_angle_0_and_circle_num(self):self.angle_0 = Noneself.circle_num = 0def get_angle_cumulatived(self, cur_angle, pre_angle=None):my_angle = Noneif self.angle_0 is None:self.angle_0 = cur_angleif pre_angle is not None:is_add_one_rotation = self.check_if_add_one_rotation(self.is_cw, pre_angle, cur_angle)if is_add_one_rotation:self.circle_num += 1print(f"self.circle_num: {self.circle_num}")if self.is_cw:my_angle = (cur_angle - self.angle_0) + self.period * self.circle_numelse:my_angle = (self.angle_0 - cur_angle) + self.period * self.circle_numreturn my_angledef main():is_cw = Falsexx = list(range(360, 1, -10)) + list(range(360, 1, -10)) + list(range(360, 1, -10))# is_cw = True# xx = list(range(1, 360, 10)) + list(range(1, 360, 10)) + list(range(1, 360, 10))ap_obj = AngleProcess(is_cw=is_cw, period=360)ap_obj.init_angle_0_and_circle_num()pre_angle = None# ap_obj.set_angle_0(cur_angle)my_angle_list = list()for cur_angle in xx:# print(f"cur_angle-{cur_angle}")my_angle = ap_obj.get_angle_cumulatived(cur_angle=cur_angle, pre_angle=pre_angle)my_angle_list.append(my_angle)pre_angle = cur_angleimport matplotlib.pyplot as pltplt.plot(my_angle_list)plt.show()if __name__ == '__main__':main()
3,结果
CW/CCW都是从0开始累加,图形一致。这个对CW/CCW画到一张图有好处