Loading editor...

Vector field II: directly defined vectors

Loading commands, almost done…

In the previous example, we derived a vector field from the gradient of a scalar function. In this example, we'll directly define the vector at each point using a custom formula.

We'll create a vector field where the vector at each point is defined by a normalized combination of the coordinates, creating an interesting rotational pattern.

Let's start by creating a point p:

This part does not change anything on the canvas.

Extract the x and y coordinates from this point:

Define a parameter that will control the vector field:

Scale the x coordinate by :

To normalize our vector, we need the distance from to the origin :

Now we'll construct the vector components. We'll use for one component:

Normalize the components by dividing by the distance:

Define the output point and draw the vector field

Create the output vector from the normalized components:

Finally, visualize the vector field:

This creates a rotational vector field where vectors are tangent to circles centered at the origin. The parameter a controls the strength in the x-direction.

Try changing the value of to see how the vector field transforms:

Notice how the vector field changes. When , you get a purely vertical rotational field. As a increases, the field becomes more elliptical.