How to draw a circle in GNU Octave

跟風遠走 提交于 2019-12-08 15:40:41

问题


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?


回答1:


t = linspace(0,2*pi,100)'; 
circsx = r.*cos(t) + x; 
circsy = r.*sin(t) + y; 
plot(circsx,circsy); 



回答2:


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



回答3:


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, '+')



回答4:


How to draw a circle in gnu octave version 3.8:

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:

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.




回答5:


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;




回答6:


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)



回答7:


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);

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);


来源:https://stackoverflow.com/questions/7971467/how-to-draw-a-circle-in-gnu-octave

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!