How can I construct delauany's disc in matlab??
1 回表示 (過去 30 日間)
古いコメントを表示
This is the code which I did for displaying delanauys disc in matlab, however I am not getting the accurate output. Please find below my code and output for that. Kindly help me out with this.
% Script for delaunay's disc
clc
close all
figure
axis on;
hold on
n=5; % Declaring the value of n
% Calculation of concentric circles
x = linspace(0,n,1000);
y1 = sqrt(n^2-x.^2); % Calculation of circle equation for the top circle
% y2 = -y1; % Constructing the Bottom circle
fill([x 0],[y1 0],rand(1,3)) % Filling the top circle with red color
% fill([x 0],[y2 0],rand(1,3)) % Filling the bottom circle with white color
n1 = -5;
x1 = linspace(0,n1,1000);
y2 = sqrt(n1^2-x1.^2);
fill([x1 0],[y2 0],rand(1,3))
x2 = linspace(n1,n1,10);
y3 = sqrt(n1^2-x2.^2);
fill([x2 0],[y3 0],rand(1,3))
x3 = linspace(n1,0,10);
y4 = sqrt(n1^2-x3.^2);
fill([x3 0],[y4 0],rand(1,3))
Output:
Kindly help me with the correct code for this problem. Let me know if there is any concerns or questions.
Thanks in advance.
2 件のコメント
KALYAN ACHARJYA
2019 年 10 月 7 日
however I am not getting the accurate output.
Expected accurate result?
採用された回答
John D'Errico
2019 年 10 月 7 日
Hmm, if that was what you were expecting, you did not come very close. :)
Let me suggest a trick though.
Write a little helper function that will plot a quarter arc of a circular reqion, with an inner and outer radius specified, and you would tell the code which quadrant to create this in. You will be using fill for the plot. Then just call it repeatedly. I might write it like this:
function quarterarc(R1,R2,quadrant,arccolor)
% R1 and R2 are radii for the arcs. R1 can be zero, in which case
% the arc will fill all the way to the origin.
% quadrant is expected to be a number from the set {1,2,3,4}.
% It will denote the quadrant the arc will live in.
% arccolor must be a row vector of length 3, indicating a color in RGB.
if ~ismember(quadrant,[1,2,3,4])
error('The sky is falling!. quadrant was an unexpected value')
end
% create the arc, in the form of a polygonal region with n points along
% each the inner and outer edges
t1 = (quadrant - 1)*pi/2;
t2 = quadrant*pi/2;
n = 100;
theta = [linspace(t1,t2,n),linspace(t2,t1,n),t1];
R = [repmat(R1,1,n),repmat(R2,1,n),R1];
% Convert to carteisna coordinates
[X,Y] = pol2cart(R,theta)
The nice thing is, that little helper function can be tested first, BEFORE you write the rest of your code. Make sure that it does what you expected. This act of encompassing important pieces of code in a function is an incredibly valuable one, that you will frequently find of use in your programming efforts. It allows you to break a problem up into small pieces. Then test and debug each piece. Verify that it works, and works correctly, BEFORE putting it all together.
But then, it seems trivial to write the plot code.
figure
hold on
axis([-5,5,-5,5])
axis equal
for R1 = 0:4
R2 = R1 + 1;
for quadrant = 1:4
quarterarc(R1,R2,quadrant,100,rand(1,3));
end
end
Sadly, I have a funny feeling if you turn in my solution as your homework that your teacher will know you copied it. But it may give you a hint as to how to write your own code.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Surface and Mesh Plots についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!