# GEOMETRIC FUNCTIONS

## Length

``````float length(float x)
float length(float2 x)
float length(float3 x)
float length(float4 x)``````

The function returns the length of a vector defined by the Euclidean norm, i.e. the square root of the sum of the squared components. The input parameter can be a float scalar or a float vector. In case of a float scalar the length function is trivial and returns the absolute value.

## Length squared

``````float length_squared(float x)
float length_squared(float2 x)
float length_squared(float3 x)
float length_squared(float4 x)``````

The function returns the square of the length of a vector, i.e. the sum of the squared components. The input parameter can be a float scalar or a float vector. In case of a float scalar the length function is trivial and returns the absolute value.

## Distance

``````float distance(float p0, float p1)
float distance(float2 p0, float2 p1)
float distance(float3 p0, float3 p1)
float distance(float4 p0, float4 p1)``````

The function returns the distance between two points. The distance of two points is the length of the vector d = p1 − p0, that starts at p0 and points to p1. The input parameters can be float scalars or float vectors. In case of float scalars the distance function is trivial and returns the absolute value of d.

## Distance squared

``````float distance_squared(float p0, float p1)
float distance_squared(float2 p0, float2 p1)
float distance_squared(float3 p0, float3 p1)
float distance_squared(float4 p0, float4 p1)``````

The function returns the square of the distance between two points. The distance of two points is the length of the vector d = p1 − p0, that starts at p0 and points to p1. The input parameters can be float scalars or float vectors. In case of float scalars the distance function is trivial and returns the square of d.

## Dot product

``````float dot(float x, float y)
float dot(float2 x, float2 y)
float dot(float3 x, float3 y)
float dot(float4 x, float4 y)``````

The function returns the dot product of the two input parameters, i.e. the sum of the component-wise products. If x and y are the same the square root of the dot product is equivalent to the length of the vector. The input parameters can be float scalars or float vectors. In case of float scalars the dot function is trivial and returns the product of x and y.

## Cross product

``float3 cross(float3 x, float3 y)  ``

The function returns the cross product of the two input parameters, i.e. a vector that is perpendicular to the plane containing x and y and has a magnitude that is equal to the area of the parallelogram that x and y span. The input parameters can only be 3-component floating vectors. The cross product is equivalent to the product of the length of the vectors times the sinus of the (smaller) angle between x and y.

## Normalize

``````float normalize(float x)
float2 normalize(float2 x)
float3 normalize(float3 x)
float4 normalize(float4 x)``````

The function returns a vector with length 1.0 that is parallel to x, i.e. x divided by its length. The input parameter can be a float scalar or a float vector. In case of a float scalar the normalize function is trivial and returns 1.0.

## Faceforward

``````float faceforward(float N, float I, float Nref)
float2 faceforward(float2 N, float2 I, float2 Nref)
float3 faceforward(float3 N, float3 I, float3 Nref)
float4 faceforward(float4 N, float4 I, float4 Nref)``````

The function returns a vector that points in the same direction as a reference vector. The function has three input parameters of the type float scalar or float vector: N, the vector to orient, I, the incident vector, and Nref, the reference vector. If the dot product of I and Nref is smaller than zero the return value is N. Otherwise -N is returned.

## Reflect

``````float reflect(float I, float N)
float2 reflect(float2 I, float2 N)
float3 reflect(float3 I, float3 N)
float4 reflect(float4 I, float4 N)``````

The function returns a vector that points in the direction of reflection. The function has two input parameters of the type float scalar or float vector: I, the incident vector, and N, the normal vector of the reflecting surface.

To obtain a reflection vector with the same length as the incident vector the vector N has to be normalized. It follows that the reflection vector is normalized if N and I are both normalized.

## Refract

``````float refract(float I, float N, float eta)
float2 refract(float2 I, float2 N, float eta)
float3 refract(float3 I, float3 N, float eta)
float4 refract(float4 I, float4 N, float eta)``````

The function returns a vector that points in the direction of refraction. The function has two input parameters of the type float scalar or float vector and one input parameter of the type float scalar: I, the incident vector, N, the normal vector of the refracting surface, and eta, the ratio of indices of refraction.

To obtain the desired result the vectors I and N have to be normalized.