Unit vector help, please

new topic     » topic index » view thread      » older message » newer message

I'd like to draw a unit arrow to point from source to target.  I've got 
the following routine that works but seems overcomplicated to me.  Can 
it be simplified?

procedure drawArrow( sequence source, sequence target )
    integer x1, y1, x2, y2, xdiff
    atom slope, ang
  
    setPenColor( Win, Black )
    xdiff = target[X]-source[X]
    if xdiff then -- no worries about divide by zero
      slope = (target[Y]-source[Y])/xdiff
      ang = arctan( slope )
      x1 = floor(20*cos(ang))
      y1 = floor(20*sin(ang))
      x2 = -x1
      y2 = -y1
      drawLine( Win, 30+x1, 30+y1, 30+x2, 30+y2 )
      if source[X] > target[X] then -- simple arrow head for now
        drawEllipse( Win, w32True, 27+x2, 27+y2, x2+33, y2+33 )
      else
        drawEllipse( Win, w32True, 27+x1, 27+y1, x1+33, y1+33 )
      end if
    else -- direction is up or down
      drawLine( Win, 30, 10, 30, 50 )
      if source[Y] > target[Y] then -- simple arrow head for now
        drawEllipse( Win, w32True, 27, 7, 33, 13 )
      else
        drawEllipse( Win, w32True, 27, 47, 33, 53 )
      end if
    end if
end procedure


Thanks in advance,
-- Brian

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu