Table of Contents

Struct GeoVector2D

Namespace
CADability
Assembly
CADability.dll

A two dimensional vector with double x and y components. Keep in mind that this is a value type. Passing a value type as a (non ref) parameter and changing it's value inside the invoked method leaves the original unchanged.

[JsonVersion(serializeAsStruct = true, version = 1)]
public struct GeoVector2D : ISerializable, IJsonSerialize
Implements
Inherited Members

Constructors

GeoVector2D(Angle)

Constructs the unit vector pointing in the given direction. The angle is measured counterclockwise from the x-axis, so the angle 0 yields XAxis.

public GeoVector2D(Angle dir)

Parameters

dir Angle

direction of the vector

GeoVector2D(GeoVector)

Constructs a new GeoVector2D from the x- and y-components of a 3-dimensional vector, i.e. its projection onto the x/y plane. The z-component is dropped.

public GeoVector2D(GeoVector v3)

Parameters

v3 GeoVector

the 3-dimensional vector

GeoVector2D(IJsonReadStruct)

Constructor for the JSON deserialization (IJsonSerialize): reads the two components in the order GetObjectData(IJsonWriteData) wrote them. It is found by reflection, there is no need to call it directly.

public GeoVector2D(IJsonReadStruct data)

Parameters

data IJsonReadStruct

supplies the values

GeoVector2D(double, double)

Constructs a new GeoVector2D with the given components.

public GeoVector2D(double x, double y)

Parameters

x double

x-component

y double

y-component

Fields

x

x-component

public double x

Field Value

double

y

y-component

public double y

Field Value

double

Properties

Angle

The direction of this vector as an angle, measured counterclockwise from the x-axis in the range [0, 2*pi) (0 for the nullvector). Setting it rotates the vector to that direction, keeping its length.

public Angle Angle { get; set; }

Property Value

Angle

IsMoreHorizontal

Simple test, whether the vector is more horizontal than vertical

public bool IsMoreHorizontal { get; }

Property Value

bool

IsValid

Returns true, when both components are ordinary numbers. A vector that failed to be computed carries NaN or infinity, and every comparison against such a value is false, which lets a broken value travel through an iteration unnoticed instead of stopping it.

public bool IsValid { get; }

Property Value

bool

Length

The length of this vector. Setting it scales the vector to the new length, keeping its direction; setting the length of the nullvector yields NaN components.

public double Length { get; set; }

Property Value

double

Normalized

A copy of this vector scaled to Length 1.0; this vector itself remains unchanged (see Norm() to normalize in place). Throws a GeoVectorException if this vector is the nullvector.

public GeoVector2D Normalized { get; }

Property Value

GeoVector2D

NullVector

The nullvector, same as new GeoVector2D(0.0, 0.0).

public static GeoVector2D NullVector { get; }

Property Value

GeoVector2D

TaxicabLength

The taxicab (Manhattan) length of this vector, i.e. the sum of the absolute values of the components. A cheap estimate of the size of the vector for tolerance checks, not the euclidean Length.

public double TaxicabLength { get; }

Property Value

double

XAxis

The x-axis, same as new GeoVector2D(1.0, 0.0).

public static GeoVector2D XAxis { get; }

Property Value

GeoVector2D

YAxis

The y-axis, same as new GeoVector2D(0.0, 1.0).

public static GeoVector2D YAxis { get; }

Property Value

GeoVector2D

Methods

Area(GeoVector2D, GeoVector2D)

Returns the area of the parallelogram defined by the two vectors. The result will be positive if the sweep-direction from from to to is counterclockwise otherwise it will be negative. If you need the angle of the triangle divide the result by 2.0

public static double Area(GeoVector2D from, GeoVector2D to)

Parameters

from GeoVector2D

First vector

to GeoVector2D

Second vector

Returns

double

the area

Cos(GeoVector2D, GeoVector2D)

Returns the cosine of the angle between the two vectors. Throws an arithmetic exception if any of the vectors is the nullvector.

public static double Cos(GeoVector2D v1, GeoVector2D v2)

Parameters

v1 GeoVector2D

First vector

v2 GeoVector2D

Second vector

Returns

double

Cosine of the inner angle

FromAngle(double)

Returns the unit vector pointing in the given direction. The angle is given in radians, measured counterclockwise from the x-axis.

public static GeoVector2D FromAngle(double angle)

Parameters

angle double

direction in radians

Returns

GeoVector2D

the unit vector in that direction

GetObjectData(IJsonWriteData)

Writes the two components for the JSON serialization (IJsonSerialize). The struct is serialized inline as a plain list of values and read back by the constructor taking an IJsonReadStruct.

public void GetObjectData(IJsonWriteData data)

Parameters

data IJsonWriteData

receives the values

GetObjectData(SerializationInfo, StreamingContext)

Writes the data of this object for the legacy binary serialization (ISerializable). Files are written in the JSON format (IJsonSerialize) now; the binary format is still read.

public void GetObjectData(SerializationInfo info, StreamingContext context)

Parameters

info SerializationInfo

receives the data

context StreamingContext

the destination of the serialization

IsNullVector()

Returns true, if this vector is exactly the nullvector. Use IsNullVector(GeoVector2D) if you need more control over precision.

public bool IsNullVector()

Returns

bool

true if nullvector

Norm()

Normalizes this vector. After this operation the vector will have the Length 1.0. Throws a GeoVectorException if the vector is the nullvector.

public void Norm()

Opposite()

Returns the reversed vector, same as the unary minus operator.

public GeoVector2D Opposite()

Returns

GeoVector2D

the reversed vector

Orientation(GeoVector2D, GeoVector2D)

Returns a positive value if the second vector turns to the left relative to the direction of the first vector, a negative value if it turns to the right and 0.0 if the vectors are parallel. The value is the length of the crossproduct of the two vectors in 3D or the area of the parallelogram build by the two vectors.

public static double Orientation(GeoVector2D v1, GeoVector2D v2)

Parameters

v1 GeoVector2D

First vector

v2 GeoVector2D

Second vector

Returns

double

The orientation

SetObjectData(IJsonReadData)

Does nothing. Value types serialized inline are restored by the constructor taking an IJsonReadStruct; this method only exists to satisfy IJsonSerialize.

public void SetObjectData(IJsonReadData data)

Parameters

data IJsonReadData

not used

ToLeft()

Returns this vector rotated by 90 degrees counterclockwise, i.e. the perpendicular vector pointing to the left of this one. The length is unchanged.

public GeoVector2D ToLeft()

Returns

GeoVector2D

the vector rotated to the left

ToRight()

Returns this vector rotated by 90 degrees clockwise, i.e. the perpendicular vector pointing to the right of this one. The length is unchanged.

public GeoVector2D ToRight()

Returns

GeoVector2D

the vector rotated to the right

Operators

operator +(GeoVector2D, GeoVector2D)

Adds the two vectors.

public static GeoVector2D operator +(GeoVector2D v1, GeoVector2D v2)

Parameters

v1 GeoVector2D

first vector

v2 GeoVector2D

second vector

Returns

GeoVector2D

sum

implicit operator double[](GeoVector2D)

Converts the vector to an array of its components, e.g. to pass it to numerical routines.

public static implicit operator double[](GeoVector2D p)

Parameters

p GeoVector2D

the vector

Returns

double[]

the array {x, y}

operator *(GeoVector2D, GeoVector2D)

Calculates the scalar product (dot product, inner product) of the two given vectors.

public static double operator *(GeoVector2D v1, GeoVector2D v2)

Parameters

v1 GeoVector2D

first vector

v2 GeoVector2D

second vector

Returns

double

the scalar product

operator *(double, GeoVector2D)

Scales the given vector by the given factor.

public static GeoVector2D operator *(double d, GeoVector2D v)

Parameters

d double

factor

v GeoVector2D

vector

Returns

GeoVector2D

scaled vector

operator -(GeoVector2D, GeoVector2D)

Subtracts the second vector from the first vector.

public static GeoVector2D operator -(GeoVector2D v1, GeoVector2D v2)

Parameters

v1 GeoVector2D

first vector

v2 GeoVector2D

second vector

Returns

GeoVector2D

difference

operator -(GeoVector2D)

Reverses the direction of the given vector.

public static GeoVector2D operator -(GeoVector2D v1)

Parameters

v1 GeoVector2D

vector to reverse

Returns

GeoVector2D

reversed vector