私はMATLABを初めて使用しますが、JavaScriptやその他のプログラミング言語で作業しました。MATLAB - 中心点を中心に正三角形を回転します
私は、辺の長さ、x座標、y座標、および回転角度が与えられた正三角形を生成するMATLABプログラムを作成しています。それは回転を除いて意図したとおりに働いています。 回転行列を使って三角形を回転しています。これは動作しますが、点で回転するのではなく原点を中心に回転します。 (下記の例を参照)。
90度ローテーション例私はその(何とか)の周りを回転後、三角形の中心を計算する必要があると考える場でそれを回転させるために
。私はこれをどうやって行うのか、これを行うためのより簡単な方法があれば分かりません。私は回転関数があるのを見ましたが、私が見たものからは、デカルト平面ではなく球面の空間です。
コードは、以下の混乱のために残念である:
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
は、私はそれをクリーンアップだけでなく、回転の問題を支援するために、コード/ヘルプを改善のための任意の提案を開いています。助けてくれてありがとう。