How to determine the centroid of this fan(triangle) where the coordinates of one of the vertices is only known and the distance is also known?
조회 수: 3 (최근 30일)
이전 댓글 표시
I am not getting the centroid correctly. Please check the code.
x=[3];
y=[3];
r=[2];
delta=pi/3;
theta=2*pi*0.4;
si=size(x);
for i=1:si(2)
theta2 = theta + delta;
t = linspace(theta,theta2);
A = x(i) + r(i)*cos(t);
B = y(i) + r(i)*sin(t);
x_cen = mean(A)
y_cen = mean(B)
plot([x(i),A,x(i)],[y(i),B,y(i)],'b-',x_cen,y_cen,'ro')
axis([0 10 0 10]);
end
답변 (1개)
Are Mjaavatten
2018년 6월 20일
편집: Are Mjaavatten
2018년 6월 20일
In my comment there was a typo in the expression for the centroid. A factor of 1/3 was missing. Sorry about that.
Here is a script that should give you what you want:
clear;
% Circle:
x_center= 3;
y_center= 3;
r = 2;
% Sector:
delta=pi/3; % Sector width
theta=2*pi*0.4; % Sector start angle
alpha = theta+ delta/2; % Sector center line angle
r_centroid = 4*r*sin(delta/2)/delta/3;
% Translate and rotate:
x_centroid = x_center + r_centroid*cos(alpha);
y_centroid = y_center + r_centroid*sin(alpha);
figure(1);
% Plot full circle:
t = linspace(0,2*pi);
x_circle = x_center + r*cos(t);
y_circle = y_center + r*sin(t);
plot(x_circle,y_circle,'--b',x_center,y_center,'*k');
hold on
% Plot sector:
t = linspace(theta,theta+delta);
A = x_center + r*cos(t);
B = y_center + r*sin(t);
plot([x_center,A,x_center],[y_center,B,y_center],'b-', ...
x_centroid,y_centroid,'ro')
axis([0 6 0 6]);
axis equal
hold off
댓글 수: 2
Image Analyst
2018년 6월 20일
Try again:
Undefined function or variable 'x'.
Error in test2 (line 18)
x_circle = x + r*cos(t);
Are Mjaavatten
2018년 6월 20일
편집: Are Mjaavatten
2018년 6월 21일
At some point i changed the variable name from x to x_center, but forgot to change all occurrences. I should have learned by now to start a script with clear, to avoid goofs like this.
I have also corrected an error in the expression for r_centroid, where I had mixed up the full and the half sector angle. The script should be correct now.
참고 항목
카테고리
Help Center 및 File Exchange에서 Detection에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!