2016-04-09 20 views
0

私はMATLABを初めて使用しますが、JavaScriptやその他のプログラミング言語で作業しました。MATLAB - 中心点を中心に正三角形を回転します

私は、辺の長さ、x座標、y座標、および回転角度が与えられた正三角形を生成するMATLABプログラムを作成しています。それは回転を除いて意図したとおりに働いています。 回転行列を使って三角形を回転しています。これは動作しますが、点で回転するのではなく原点を中心に回転します。 (下記の例を参照)。

90度ローテーション例私はその(何とか)の周りを回転後、三角形の中心を計算する必要があると考える場でそれを回転させるために

enter image description here

。私はこれをどうやって行うのか、これを行うためのより簡単な方法があれば分かりません。私は回転関数があるのを見ましたが、私が見たものからは、デカルト平面ではなく球面の空間です。

コードは、以下の混乱のために残念である:

function [ side, coord1,coord2 ] = equilateral(side, x,y, rotation) 
%EQUILATERAL- given a side length and x,y, coordinates as inputs, the 
%function plots an equilateral triangle an angle of rotation can be 
%given as an input as well. This will rotate the trianlge around the x 
%and y coordinates given. 

%rotation argument is not required. If not given, angle is 0 
if(exist('rotation','var')) 
    angle = rotation; 
else 
    angle = 0; 
end 

%rotation matrix 
R = [cos(angle), -sin(angle); sin(angle), cos(angle)]; 
%Make the axis equal so the triangles look equilateral 
axis equal; 
%max horizontal x coordinate 
x2 = x + side; 
%max horiontal y coordinate (equal to original y coordinate) 
y2 = y; 
%height of the triangle at midpoint (perpendicular height) 
h = side*sin(pi/3) + y; 
%coordinates of midpoint/top vertice 
mid = [x2-(0.5*side), h]; 
%min coordinates 
coord1 = [x,y]; 
%max coordinates 
coord2 = [x2,y2]; 

if (angle > 0) 
    coord1 = coord1*R; 
    coord2 = coord2*R; 
    mid = mid*R; 

end 

%plot the base of the triangle 
plot(linspace(coord1(1),coord2(1)), linspace(coord1(2),coord2(2))); 
hold on 
%plot the first side from inital coords to midpoint 
plot(linspace(coord1(1),mid(1)), linspace(coord1(2),mid(2))); 
%plot second side from mid point to max coords 
plot(linspace(mid(1),coord2(1)), linspace(mid(2),coord2(2))); 
end 

は、私はそれをクリーンアップだけでなく、回転の問題を支援するために、コード/ヘルプを改善のための任意の提案を開いています。助けてくれてありがとう。

答えて

0

rotationマトリックス

[cos(theta), -sin(theta) 
sin(theta), cos(theta)] 

は軸の原点周りの角度thetaの点のセットを回転させます。与えられた点の周りの点を回転させるためには、あなたがする必要がある

からshiftあなたのポイント軸 の原点とrotatioinポイントconcidesように - 回転行列 を使用してポイントを回転させます - あなたのポイントを戻す

次のコードは、提案されたアプローチが実装されている関数の修正版です。私は重力

XR=x+side/2 
YR=y+h*.3 

の中心として回転ポイントを設定したコードで

それにもかかわらず、あなたは別の方法でそれらを計算することができます。

function [ side, coord1,coord2 ] = equilateral(side, x,y, rotation) 
%EQUILATERAL- given a side length and x,y, coordinates as inputs, the 
%function plots an equilateral triangle an angle of rotation can be 
%given as an input as well. This will rotate the trianlge around the x 
%and y coordinates given. 

%rotation argument is not required. If not given, angle is 0 
if(exist('rotation','var')) 
% angle = rotation; 
% Convert the angle from deg to rad 
    angle = rotation*pi/180; 
else 
    angle = 0; 
end 

%rotation matrix 
R = [cos(angle), -sin(angle); sin(angle), cos(angle)]; 
%Make the axis equal so the triangles look equilateral 
axis equal; 
%max horizontal x coordinate 
x2 = x + side; 
%max horiontal y coordinate (equal to original y coordinate) 
y2 = y; 
%height of the triangle at midpoint (perpendicular height) 
h = side*sin(pi/3) + y; 
%coordinates of midpoint/top vertice 
mid = [x2-(0.5*side), h]; 
%min coordinates 
coord1 = [x,y]; 
%max coordinates 
coord2 = [x2,y2]; 
plot([coord1(1) coord2(1) mid(1) coord1(1)],[coord1(2) coord2(2) mid(2) coord1(2)],'r') 
hold on 
% 
% Define the coord of the point aroud with to turn 
% 
XR=x+side/2 
YR=y+h*.3 
plot(XR,YR,'o','markerfacecolor','k','markeredgecolor','k') 

if (angle > 0) 
%  coord1 = coord1*R; 
%  coord2 = coord2*R; 
%  mid = mid*R; 
% Shift the triangle so that the rotation point coincides with the origin 
% of the axes 
    r_coord1 = (coord1-[XR YR])*R+[XR YR]; 
    r_coord2 = (coord2-[XR YR])*R+[XR YR]; 
    r_mid = (mid-[XR YR])*R+[XR YR]; 
% 
% Plot the rotated triangle 
plot([r_coord1(1) r_coord2(1) r_mid(1) r_coord1(1)],[r_coord1(2) r_coord2(2) r_mid(2) r_coord1(2)],'r') 
end 

% % % 
% % % %plot the base of the triangle 
% % % plot(linspace(coord1(1),coord2(1)), linspace(coord1(2),coord2(2))); 
% % % hold on 
% % % %plot the first side from inital coords to midpoint 
% % % plot(linspace(coord1(1),mid(1)), linspace(coord1(2),mid(2))); 
% % % %plot second side from mid point to max coords 
% % % plot(linspace(mid(1),coord2(1)), linspace(mid(2),coord2(2))); 
end 

は、私はまた、追加修正のカップル作りました:

  • を私はdegからradへの入力角度の変換を追加しました。入力がすでに入っていると仮定すると破棄できますrad
  • 私は三角形をプロットする方法を更新しました。

の提案として、あなたはあなたの関数への入力の数をthestするnarginを使用することができますし、それらが存在してvararginはテストするのではなく、それらを検査します。

enter image description here

は、この情報がお役に立てば幸いです。

カプロラ '

関連する問題