ELASTICPATHING/SOURCECODE/UbiComp2014/Elastic_Pathing_Code/path_decisions.rb
def turnAngle(prev, cur, nex)
  adj = prev.distance(cur)
  opp = cur.distance(nex)
  tan = prev.distance(nex)
  #Now apply the law of cosines to find the turn angle
  tan_opp = (adj*adj + opp*opp - tan*tan)/(2 * adj * opp)
  #Convert to degrees
  #(take the real part to avoid strange errors when an imaginary part is generated)
  angle = (180.0 - 180.0 * Math.acos(tan_opp) / Math::PI).real
  #Now determine if this is a left or right turn using a cross product of the
  #two vectors. See http://en.wikipedia.org/wiki/Graham_scan
  #The equation is (x2-x1)(y3-y1)-(y2-y1)(x3-x1)
  #0 => linear, positive => left turn, negative => left turn
  #longitudes are the x value, latitudes are the y value
  #Need to multiply the longitude by -1 because this is from North America
  turn = (cur.lon - prev.lon)*(nex.lat - prev.lat) - (cur.lat - prev.lat)*(nex.lon - prev.lon)
  if (0 < turn)
    return -1 * angle
  else
    return angle
  end
end
#Find the angle of turn given the previous node, current node, and next node
#A negative angle indicates a left turn.
Find the angle of turn given the previous node, current node, and next node
A negative angle indicates a left turn.
Find the angle of turn given the previous node current node and next node A negative angle indicates a left turn .
def turnAngle(prev, cur, nex)
  adj = prev.distance(cur)
  opp = cur.distance(nex)
  tan = prev.distance(nex)
  tan_opp = (adj*adj + opp*opp - tan*tan)/(2 * adj * opp)
  angle = (180.0 - 180.0 * Math.acos(tan_opp) / Math::PI).real
  turn = (cur.lon - prev.lon)*(nex.lat - prev.lat) - (cur.lat - prev.lat)*(nex.lon - prev.lon)
  if (0 < turn)
    return -1 * angle
  else
    return angle
  end
end
def turnAngle ( prev , cur , nex ) adj = prev . distance ( cur ) opp = cur . distance ( nex ) tan = prev . distance ( nex ) tan_opp = ( adj * adj + opp * opp - tan * tan ) / ( 2 * adj * opp ) angle = ( 180.0 - 180.0 * Math . acos ( tan_opp ) / Math :: PI ) . real turn = ( cur . lon - prev . lon ) * ( nex . lat - prev . lat ) - ( cur . lat - prev . lat ) * ( nex . lon - prev . lon ) if ( 0 < turn ) return - 1 * angle else return angle end end
Find the angle of turn given the previous node, current node, and next node
A negative angle indicates a left turn.
Find the angle of turn given the previous node current node and next node A negative angle indicates a left turn .
#Now apply the law of cosines to find the turn angle #Convert to degrees #(take the real part to avoid strange errors when an imaginary part is generated) #Now determine if this is a left or right turn using a cross product of the #two vectors. See http://en.wikipedia.org/wiki/Graham_scan #The equation is (x2-x1)(y3-y1)-(y2-y1)(x3-x1) #0 => linear, positive => left turn, negative => left turn #longitudes are the x value, latitudes are the y value #Need to multiply the longitude by -1 because this is from North America
prev
cur
nex