Question about -- rs.VectorCompare

what is VectorCompare comparing?

if i do:

import rhinoscriptsyntax as rs

vec1 = (1,0,0)
vec2 = (0,1,0)

lenvec1 = rs.VectorLength(vec1)
lenvec2 = rs.VectorLength(vec2)

print lenvec1
print lenvec2


comp = rs.VectorCompare(vec1, vec2)
print comp

both are length = 1

but then the compare returns 1 — vec1 is greater than vec2.

i thought it should return 0 since the two vectors are equal length.

?

From the RhinoCommon source:

public int CompareTo(Vector3d other)
{
  if (m_x < other.m_x)
    return -1;
  if (m_x > other.m_x)
    return 1;

  if (m_y < other.m_y)
    return -1;
  if (m_y > other.m_y)
    return 1;

  if (m_z < other.m_z)
    return -1;
  if (m_z > other.m_z)
    return 1;

  return 0;
}

Does this help?

1 Like

Hmmm …

rhinoscriptsyntax docs do not help so much …

VectorCompare
Compares two 3-D vectors.

Syntax
rhinoscriptsyntax.VectorCompare (vector1, vector2)

rhinoscript.pointvector.VectorCompare (vector1, vector2)

Parameters
vector1
Required. List of 3 numbers, Point3d, or Vector3d. The first 3-D vector to compare.

vector2
Required. List of 3 numbers, Point3d, or Vector3d. The second 3-D vector to compare.

Returns
-1
If vector1 is less than vector2.

0
If vector1 is equal to vector2.

1
If vector1 is greater than vector2.

None
On error.

… but looking into pointvector.py, we cab see that …

def VectorCompare(vector1, vector2):
    """Compares two 3D vectors
    Parameters:
      vector1, vector2 = the vectors to compare
    Returns:
      -1 if vector1 is less than vector2
      0 if vector1 is equal to vector2
      1 if vector1 is greater than vector2
    """
    vector1 = rhutil.coerce3dvector(vector1, True)
    vector2 = rhutil.coerce3dvector(vector2, True)
    return vector1.CompareTo(vector2)

… the method used here is
Rhino.Geometry.Vector3d.CompareTo().
… And looking at RhinoCommon docs we can finally understand …

Visual Basic

Public Function CompareTo ( _
other As Vector3d _
) As Integer

Parameters

other
Type: Rhino.Geometry.Vector3d
The other Vector3d to use in comparison.

Return Value

0: if this is identical to other

-1: if this.X < other.X

-1: if this.X == other.X and this.Y < other.Y

-1: if this.X == other.X and this.Y == other.Y and this.Z < other.Z

+1: otherwise.

… what that code is doing. :smiley:

Definitely not the best documented rhinoscriptsyntax method … :wink:

1 Like

thanks fellas.

I see now :eyeglasses:

The vector is the same length, but not the same direction… The method is comparing both… So the Help file appears to be completely misleading. I would just check vector3d.Length on both and compare… Don’t forget to allow for fuzzy floating point math (use a tolerance value)…

–Mitch

yes, i think i might need to do that.

is tolerance an optional argument i can use with rs.VectorLength ?

all i see is this:

[quote]
VectorLength

Returns the length of a 3-D vector.
Syntax
rhinoscriptsyntax.VectorLength (vector)
rhinoscript.pointvector.VectorLength (vector)
Parameters
vector
Required. List of 3 numbers or Vector3d. The 3-D vector.
Returns
Double
The length of the vector, if successful.
None
On error.[/quote]

also, if no tolerance is given, does is use the model’s tolerance? or is that something different in this case?

In general that means instead of checking for equality between two values as in:

if a==b: do something

(which will likely fail), check that the difference between the two values is smaller than a given acceptable tolerance:

if abs(a-b)<tolerance: do something

Depending on what you’re doing you can use the document tolerance

rs.UnitAbsoluteTolerance()

or some much smaller value - Rhino.RhinoMath gives you a couple of possibilities:

Rhino.RhinoMath.SqrtEpsilon and Rhino.RhinoMath.ZeroTolerance

or you can use the EpsilonEquals methods available with a number of objects like points and vectors to compare things to within a given tolerance.

–Mitch

great. thanks.

sometimes it takes seeing an answer for me to realize my questions were sort of silly* in the first place :smile:
glad you guys are open to answering anyway as it really helps out a lot…

(* in this case- wondering if there’s a tolerance argument for finding vector length)