如何在Python中应用顺时针和逆时针的思想?

2024-04-28 11:39:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python程序的新手,下面的图片有两个角度(-18,27)。这些角的符号是根据顺时针和逆时针的概念来分配的。这样的想法怎么能用Python翻译呢?你知道吗

我是说顺时针和逆时针的概念。任何能说明这个观点的例子。你知道吗

我如何在python中设置顺时针和逆时针的条件?你知道吗

clockwise and counterclockwise

**This description below for the above picture:**

我们现在讨论角度范围的关键概念。AP Theta*为每个顶点s保留两个附加值,即顶点s的角度下限lb(s)和顶点s的角度上限ub(s),这两个值共同构成顶点s的角度范围[lb(s),ub(s)]。角度边界对应于源自顶点s的父级的光线的方向(以度为单位)。光线的方向从顶点s的父顶点到顶点s是零度。如果(但不一定仅当)从顶点s的父节点到顶点s的可见相邻节点的光线方向包含在顶点s的角度范围内,则保证顶点s的可见相邻节点与顶点s的父节点有视线。图4.14显示了一个示例,其中顶点C3与父节点A4具有相同的角度 角度范围[−18,27]。因此,红色区域中顶点C3的所有可见邻居都保证与顶点C3的父节点有视线。例如,顶点C4保证与顶点C3的父级有视线,但顶点B2没有。因此,AP Theta*假设顶点B2与顶点C3的父顶点没有视线。你知道吗

我们现在更正式地定义角度范围的概念。angle(s, p, s′) ∈ [−90, 90], which gives AP Theta* its name, is the angle (measured in degrees) between the ray from vertex p to vertex s and the ray from vertex p to vertex s′. It is positive if the ray from vertex p to vertex s is clockwise from the ray from vertex p to vertex s′, zero if the ray from vertex p to vertex s has the same heading as the ray from vertex p to vertex s′, and negative if the ray from vertex p to vertex s is counterclockwise from the ray from vertex p to vertex s′.图4.14显示了一个示例,其中角度(C3,A4,C4)=27度,角度(C3,A4,B3)=18度。当(但不一定仅当)角(s,parent(s),s′)∈[lb(s),ub(s)](可见性性质)时,保证顶点s的可见邻域s′与顶点s的父节点有视线。你知道吗


Tags: thetofrom概念节点isap角度
2条回答

我建议implementing an ^{}来定义这个。似乎是最完美的选择。简单示例:

from enum import Enum


class ClockMotion(Enum):

    Clockwise = 1
    CounterClockwise = -1

    @classmethod
    def check_motion(cls, angle1, angle2):
        if angle2 - angle1 > 0:
            return cls.Clockwise
        else:
            return cls.CounterClockwise

测试:

>>>ClockMotion.check_motion(-30, 60)
ClockMotion.Clockwise

对于顶点,请使用一个简单的类,因为它将具有实例值:

class Vertex:

    def __init__(self, lb, ub):
        self.lb = lb
        self.ub = ub

我想这就是你要找的

import numpy as np

def unit_vector(vector):
    """ Returns the unit vector of the vector.  """
    return vector / np.linalg.norm(vector)

def angle_between(v1, v2):
    """ Returns the angle in radians between vectors 'v1' and 'v2'::
    """
    v1_u = unit_vector(v1)
    v2_u = unit_vector(v2)
    return np.rad2deg(np.arccos(np.clip(np.dot(v1_u, v2_u), -1.0, 1.0)))

a4 = np.array([0,3])
c3 = np.array([2,2])
b3 = np.array([1,2])
c4 = np.array([2,3])

c4_a4 = c4 - a4
c3_a4 = c3 - a4
b3_a4 = b3 - a4


angle_b3_c3 = angle_between(c3_a4, b3_a4)
angle_c3_c4 = angle_between(c3_a4, c4_a4)

print angle_b3_c3  
print angle_c3_c4

输出:

18.4349488229
26.5650511771

注意:这里我假设每个框都有一个单位宽度和高度的网格,如下所示,enter image description here。因此,例如a1 = {0,0}, a2 = {0,1}, a3={0,2}, a4={0,3}, a5={0,4}, b1 = {1,0}, b2 = {1,1}, b3={1,2}等等;在这里,行对应于a、b、c、d,而冒号对应于行的索引(例如,a1、a2等)

根据方向,你必须乘以-1得到顺时针或逆时针方向

相关问题 更多 >