Angle of Vector

Hello,

I am trying to do something with exact angle performance between two edges, so far I handle it with the code as below

Although I know the with_angle function in EdgePairs class, but I still prefer exact angle value, not sure there is some mystic function ?

Somehow the calculation noise will take place ( out of domian - acos ) , but it's not problem, and can be solved by simple rounding

include RBA

v1 = RBA::Vector.new(-17,-17)
v2 = RBA::Vector.new(-17,-17)

dot = (v1.x * v2.x + v1.y * v2.y)/(v1.abs*v2.abs)
dot1 = dot.round(5) # remove calculate noise

p dot 
p dot1

arc1 = Math::acos(dot1)
arc = Math::acos(dot)

Thanks and Good Day
Vincent

Comments

  • Hi Vincent,

    Floating-point arithmetics is never exactly accurate and errors accumulate. In side KLayout I am doing something like this:

    angle_with_noise = ...
    
    # for example, test if the angle is 45 degree:
    if (angle_with_noise - 45.0).abs < 1e-10
       ...
    end
    

    The 1e-10 value is somewhat arbitrary - it should be large enough not to be noise sensitive, but small enough to be "precise".

    There is a rough estimate for the minimum angle between two edges which are not parallel: as the database is made from 32bit integer coordinates, the smallest possible angle is 1 DBU / 2^32 DBU which is about 2.5e-10 (rad). In degree units that is 1.5e-8 degree. The noise is about 1e-16 relative. For an angle value of 0..360 degree, this translates into 3.6e-14 max noise. So 1e-10 is somewhere in between and a good choice for differentiating noise from real angle differences.

    To compute the angle between two vectors, I'd propose this code:

    v1 = RBA::Vector::new(0, 1000)
    v2 = RBA::Vector::new(1234, 1234)
    
    vp = v1.vprod(v2)
    sp = v1.sprod(v2)
    
    angle = Math::atan2(vp, sp) * 180.0 / Math::PI
    

    This computes the angle between v1 and v2, the angles counted in counter-clockwise direction.

    Matthias

  • Hello Matthias,

    Thanks for detail explanation, it's helpful to learn more about K-layout :)

    Good Day
    Vincent

Sign In or Register to comment.