simpatico
simpatico

Reputation: 11087

How to draw a circle in GNU Octave

In Matlab you can draw a circle by just specifying the center and the radius like this:

R = 10;
Center = [5,8];
circle(Center,R,1000,'b-');
hold on
plot(Center(1),Center(2),'g.')

The same code for MatLab won't work for GNU Octave. What octave code would draw a circle given a center x,y coordinates and a radius?

Upvotes: 13

Views: 31820

Answers (8)

BenH
BenH

Reputation: 2120

r = 1;
center = [0, 0];
t = linspace(0,2*pi,100)'; 
x = r.*cos(t) + center(1); 
y = r.*sin(t) + center(2); 
plot(x, y); 

Upvotes: 19

TRAME Estruturas
TRAME Estruturas

Reputation: 11

radius = 5;
x = cosd([0:360]) * radius;
y = sind([0:360]) * radius;
plot(x,y)

Upvotes: 1

Mikael Fremling
Mikael Fremling

Reputation: 762

Using the octave extra package octave-geometry you can use the function drawCircle as

drawCircle(x_position, y_position, radius)
  1. Download the package from https://octave.sourceforge.io/geometry/
  2. In Octave:
    • pkg install path_to_file.tar.gz
    • pkg load geometry

Upvotes: 10

Manjitha Teshara
Manjitha Teshara

Reputation: 592

Ocatve have several way to draw circle,that is one of easy way.

pkg load geometry
x=0;
y=0; #center point ordered pair
r1=10; #radius of circule
h1=drawCircle(x,y,r1);

if you want drow more circle over,

hold on;
r2=8;
h2=drawCircle(x,y,r2);

enter image description here

another way is

angle=linspace(0,2*pi,360);

r=5; #radius

x=2;

y=5; #x and y is hear center point 

horizantalValue=r*cos(angle)+x;

verticalValue=r*sin(angle)+y;

plot(verticalValue,horizantalValue);

Upvotes: 0

user9896170
user9896170

Reputation: 11

There is a built in function in the package "geometry" that can be used to draw different geometric shapes. Here is the code for a circle:

pkg load geometry
drawCircle (x0, y0, r)

Upvotes: 1

Eric Leschinski
Eric Leschinski

Reputation: 153872

How to draw a circle in gnu octave version 3.8:

enter image description here

Code:

octave> x = -1:0.01:1;
octave> y = (1 - x .^ 2) .^ 0.5;
octave> plot(x,y, "linewidth", 4, x,-y, "linewidth", 4);

Verbalization:

Create a list between -1 and 1 in increments of .01 to represent the x axis. The y axis is the diameter of the circle minus the value at each index of x squared, all raised to the 0.5.

Plot x and y (blue), which gives the upper half of the circle, then plot x to -y, which inverts the top (green), creating the bottom half of the circle.

Alternatively, use linspace:

enter image description here

Code:

octave> r = 1;
octave> t = linspace(0,2.*pi,1);
octave> circsx = r.*cos(t) + x;
octave> circsy = r.*sin(t) + y;
octave> plot(circsx,circsy, "linewidth", 4, circsx, -circsy, "linewidth", 4); 

Verbalization:

Draws a circle.

Upvotes: 3

user3060513
user3060513

Reputation: 11

Plot a circle in GNU Octave:

x = -3:0.01:3;
y = (4 - x .^ 2) .^ 0.5;
figure; plot(x,y); hold on; plot(x,-y);

That should draw a circle having an equation x^2 + y^2 = 4;

Upvotes: 1

PauloT
PauloT

Reputation: 81

If you would like a reusable function, here's one possibility:

function [h, hc] = circles(x,y,r,cmrk)
% CIRCLES   plot 2-D circles, given a set of center coordinates and radii.
%
% Description:
%
%   Plot 2-D circles, given a set of center coordinates and radii. Values
%   can be vectors or matrices, as long as dimensions are consistent. If
%   a marker type (e.g. '+') is also given, circle centers will be marked
%   with it. The function returns a vector of handles for each circle and
%   a handle for all the center markers, if plotted.

assert(size(x)==size(y), 'Mismatching sizes')
assert(size(y)==size(r), 'Mismatching sizes')

if (nargin==4)
    hc = scatter(x,y,[],[],cmrk);
end
axis([min(x-r) max(x+r) min(y-r) max(y+r)], 'equal');

a = linspace(0, 2*pi, 12);
dx = sin(a); dy = cos(a);
hold on
for i=1:numel(x);
    h(i) = line(x(i)+dx*r(i), y(i)+dy*r(i));
end
hold off

Here's an example of usage:

x = 0:.1:2*pi; y = sin(x); r = rand(size(x))*.3;
circles(x, y, r, '+')

Upvotes: 4

Related Questions