Octave: Dotted Circle

I was in need for a tool that generates 2D cartesian coordinates (x/y) of points that are equally spaced on a circle (in Euclidean space). As this was a one-time endeavour I chose to quickly fire up Octave and created a simple function to get the job done:

# CIRCLE_POINTS Calculates equally spaced points on a circle.
#
# Inputs:
#   x0    : The X-component of the origin of the circle.
#   y0    : The Y-component of the origin of the circle.
#   radius: The radius of the circle.
#   count : The number of points on the circle.
#
# Outputs:
#   coords: A 2xcount matrice containing the X & Y coordinates of each point on the circle.
#
# Author: Joel Bodenmann
#
function coords = circle_points(x0, y0, radius, count)
    theta = (0:count-1)*2*pi/count;

    x = x0 + radius * cos(theta);
    y = y0 + radius * sin(theta);

    coords = [x; y];
endfunction

There’s nothing surprising going on here: First, we create a list of angles, then we simply calculate X and Y coordinates for each angle. That’s it.

The function can be used like this:

# PLOT_2D_COORDINATES    Plots 2D coordinates in a scatter plot.
#
# Inputs:
#   coords    : A cell array where each cell contains a 2xN matrix represending the X & Y components of each coordinate.
#
function plot_2d_coordinates(coords)
    # Styling options
    dotSize = 15;
    color = "b";
    style = "filled";

    # Plot
    hold on;
    axis equal;
    scatter(coords(1,:), coords(2,:), dotSize, color, style);
    set(gca, "xaxislocation", "origin");
    set(gca, "yaxislocation", "origin");
    hold off;
endfunction

# Calculate points on concentric circles
coords = circle_points(0, 0, 1, 32);

# Plot
figure(1);
clf;
plot_2d_coordinates(coords);

Resulting plot:

comments powered by Disqus