Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

198
Visualizações
Find the angle of two connected line segments based upon a random point

I have a 3 pairs of x, y coordinates (A, B, C) that form two connected line segments.

enter image description here

I want to calculate the angle of the line segment based upon a random point (x, y coordinates) that can fall either side of the line segments.

For example, if the random point was D, I would want to calculate the green angle, or if the random point was E, I would want to calculate the red angle.

Here is the function signature I'm looking to complete:

function angle(segment_1, segment_2, random_point) {

}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

In your function, you could create an intermediate segment between your random point and the base and then add these 2 angles

angle(BA,BE) + angle(BE,BC);
about 4 years ago · Juan Pablo Isaza Relatório

0

You need some basic functions like scalar product and cross product to achieve this ;

import math
from collections import namedtuple
Point = namedtuple('Point', 'x y')

# define nom of a vector
def norm(v):
    n = math.sqrt(v.x*v.x + v.y*v.y)
    return n

# normalise a vector
def normedVec(v):
    n = norm(v)
    nv = Point(v.x/n, v.y/n)
    return nv

# print angle in degrees
def printAngle(name,rad):
    deg = rad * 180.0 / math.pi
    print("{}={}".format(name,deg))

# scalar (aka dot)product of 2 vectors 
def dotProduct(v1,v2):
    d = v1.x*v2.x +  v1.y*v2.y
    print("dot={}".format(d))
    return d

# vectorial (aka cross) product
def crossProd(v1,v2):
    c = v1.x*v2.y - v1.y*v2.x
    print("c={}".format(c))
    return c

# compute angle between 2 vectoris
def angle(s1,s2):
    n1 = normedVec(s1)
    n2 = normedVec(s2)
    cosAngle = dotProduct(n1,n2)
    d = crossProd(n1,n2)
    angle = math.acos(cosAngle)
    if d <= 0:
        angle = - angle
    printAngle("angle",angle)
    return angle

# compute angle between 2 vectors using point orientation 
def angleOriented(AB,AC,AX):
    # Compute angle between 2 vectors
    ABC = angle(AB, AC)
    # Compute angle to random point
    ABX = angle(AB, AX)
    # if angle is negative then change angle sign 
    if ABX < 0:
        ABC = - ABC
    # put angle between 0 and 360
    while ABC < 0:
        ABC += 2*math.pi
    printAngle("Oriented",ABC)

# test
AB = Point(1,0)
AC = Point(-1,1)
AD = Point(0,-1)
AE = Point(0,1)

ABE = angleOriented(AB,AC,AE)
print("-----")
ABD = angleOriented(AB,AC,AD)
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda