0

If I have 4 nodes on a line, for example:

  • Main line is formed by nodes A(0,0) and B(5,0)
  • and there is a line on this main line which is formed by C(2,0) and D(4,0)

It is clear that vector AB has the same direction with vector CD, or in other words, vector BA has the same direction with vector DC.

I know it is simple, that in this case we just calculate delta x and then divided by each length.

Is there any common technique and in a "straight-way fashion"?

For example by comparing the gradients (which is in this case, it would be still valid, since both gradients are zero, but if the coordinates are conversed, the computation will be error, since the gradients cannot be computed by dividing by zero).

Thanks in advance.

bestrong
  • 151
  • 10
  • 1
    See [cross product of vectors](http://mathworld.wolfram.com/CrossProduct.html). – Matsmath Aug 19 '16 at 17:03
  • Or just do as you have been doing, and compare the gradients, just with the special case you noted, handling a division by 0. (But even so, if both sides would cause division by zero, they have the same direction.) – Scott Mermelstein Aug 19 '16 at 17:04
  • @Matsmath: Thanks. But I think doing the cross product is not the solution, since this is a coincident line case. Or am I wrong? – bestrong Aug 19 '16 at 17:38
  • @ScottMermelstein: Thanks. Yes you're right, but actually I am doing a programming stuff, so I just try to avoid adding `if-else` statement. – bestrong Aug 19 '16 at 17:40
  • It's kinda weird - every programming language has an `if-else` structure. It must be there for a reason... While it's a good principle to avoid special casing, a check for division by 0 is not that kind of special case; it's part of handling division. (Special cases get dangerous when you keep adding more and more of them. In a case of a division by 0 check, there won't be any other special cases.) The cross product idea suggested by Matsmath will work without the special casing (but will likely have a few more arithmetic operations than the simple gradient check). – Scott Mermelstein Aug 19 '16 at 17:44
  • I think it's not weird. I wish to avoid `if-else` construction for this simple case, since I just want to ensure my code will be successfully _vectorized_ by the compiler. That's why I am searching for the mathematical equation which can satisfy it. If I use `if-else` statement, my code will not get _vectorized_. So for example, I refer to a case where we should find an intersection of two lines. Of course, it is also possible using `if-else` statement, but calculating the _determinant_ will be better, even though it requires more arithmetic operators on the code. Anyway, thanks. – bestrong Aug 19 '16 at 18:13

1 Answers1

1

If you want to know whether AB has the same orientation as CD, compute the dot product (B-A)*(D-C). It will be positive if they point in the same direction, negative if they point in opposite directions, and zero if one of the vectors is zero (or otherwise perpendicular to the other, but you assumed collinear points so that can't happen).

MvG
  • 57,380
  • 22
  • 148
  • 276
  • Thanks. So it means that dot product can't be used in my case, since the result of dot product (_which is equal to determinant for this case_) is always zero. – bestrong Aug 20 '16 at 07:32
  • I don't follow you, @bob. In your example, B-A=(5,0) and D-C=(2,0). The dot product is 5*2+0*0=10. The determinant is 0. Why would the dot product be equal to the determinant? – MvG Aug 20 '16 at 08:49
  • Sorry my mistake, I read it wrongly. I mean _cross product_ NOT _dot product_. In this case, you're right. Thanks. – bestrong Aug 20 '16 at 12:46