Brian Schott wrote:
>       Two objects in 2 dimensional space are facing the
> same direction, say D degrees. How does one determine which
> object is on the left if the positions of the two objects A
> and B in 2D, are given as A and B, for example as follows?
> 
> 'A B' =: 2 1;1 _1
> D =: 90  NB. facing to the East
> result: A is on the left
[etc...]
>       How does one compute such results?

My first reaction when I heard the problem description was
"use cross product". 

Cross product requires two vectors.  Here, one of the vectors
needs to point in the "D" direction.  Another vector might be
the vector from A to B.

Here, the B vector is B-A and the D vector is
1 2 o.(90 - D)%180p_1.

There's simplified ways of doing this, but I'm
most comfortable working with the 3d cross product:

X=: (1&|[EMAIL PROTECTED] * _1&|[EMAIL PROTECTED]) - _1&|[EMAIL PROTECTED] * 
1&|[EMAIL PROTECTED]

Here's your three examples:

   (1 2 o.(90 - D)%180p_1.) X&(,&0) B-A
0 0 1
   'A B' =: _1 0;1 1 [ D=:0
   (1 2 o.(90 - D)%180p_1.) X&(,&0) B-A
0 0 1
   'A B' =: _1 0;_1 0 [ D=:0
   (1 2 o.(90 - D)%180p_1.) X&(,&0) B-A
0 0 0

That third value is positive if A is on the left, and
negative if A is on the right.

Possible simplifications:

[1] Since the original problem only used two dimensions,
you don't need the fully general cross product.  You only
need the third value (the other two numbers will always
be zero).

[2] Depending on your context, you might have a simpler
way of specifying the vector indicated by D.  You might
also alter the frame of reference (possibly flipping
the sign of the result) from what I've used.

-- 
Raul

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to