在 Matlab 中,只需指定圆心和半径即可画圆,如下所示:
R = 10;
Center = [5,8];
circle(Center,R,1000,'b-');
hold on
plot(Center(1),Center(2),'g.')
MatLab 的相同代码不适用于 GNU Octave。给定中心 x,y 坐标和半径,什么八度音阶代码可以绘制一个圆?
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);
使用八度额外包八度几何,您可以使用函数drawCircle作为
drawCircle(x_position, y_position, radius)
pkg install path_to_file.tar.gz
pkg load geometry
如果您想要可重用的功能,这里有一种可能性:
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
这是一个使用示例:
x = 0:.1:2*pi; y = sin(x); r = rand(size(x))*.3;
circles(x, y, r, '+')
如何在gnu Octave 3.8版本中画圆:
代码:
octave> x = -1:0.01:1;
octave> y = (1 - x .^ 2) .^ 0.5;
octave> plot(x,y, "linewidth", 4, x,-y, "linewidth", 4);
言语化:
创建一个介于 -1 和 1 之间的列表,增量为
.01
来表示 x 轴。 y 轴是圆的直径减去 x 平方的每个索引处的值,全部提升到 0.5
。
绘制 x 和 y(蓝色),给出圆的上半部分,然后绘制 x 到 -y,这反转顶部(绿色),创建圆的下半部分。
代码:
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);
言语化:
画一个圆圈。
在 GNU Octave 中绘制圆:
x = -3:0.01:3;
y = (4 - x .^ 2) .^ 0.5;
figure; plot(x,y); hold on; plot(x,-y);
应该画一个具有方程 x^2 + y^2 = 4 的圆;
“geometry”包中有一个内置函数,可以用来绘制不同的几何形状。这是圆的代码:
pkg load geometry
drawCircle (x0, y0, r)
Ocatve有多种画圆的方法,这是一种简单的方法。
pkg load geometry
x=0;
y=0; #center point ordered pair
r1=10; #radius of circule
h1=drawCircle(x,y,r1);
如果你想多画圈,
hold on;
r2=8;
h2=drawCircle(x,y,r2);
另一种方法是
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);
radius = 5;
x = cosd([0:360]) * radius;
y = sind([0:360]) * radius;
plot(x,y)