2016-04-09 10 views
0

MATLAB için yeni ama javascript ve diğer programlama dilleriyle çalıştım.MATLAB - Bir eşkenar üçgeni merkez noktasının etrafında döndürme

Yan uzunluk, bir x koordinatı, bir y koordinatı ve bir dönüş açısı verilen bir eşkenar üçgen oluşturacak bir MATLAB programı yazıyorum. Rotasyonlar dışında amaçlandığı gibi çalışıyor. Üçgeni döndürmek için bir döndürme matrisi kullanıyorum. Bu çalışır, ancak o noktada dönme yerine orijin etrafında döner. (aşağıdaki örneğe bakınız).

90 derece Rotasyonlar Örnek Ben bu (her nasılsa) etrafında döndürmek sonra üçgenin merkezini hesaplamak gerekir ve düşünmek yerinde döndürmek amacıyla

enter image description here

. Bunu nasıl yapacağımı veya bunu yapmanın daha kolay/daha iyi bir yolu varsa emin değilim. Döndürme fonksiyonunun olduğunu gördüm, fakat gördüğüm kadarıyla, küresel uzay için değil, Kartezyen düzlemleri değil.

Kod aşağıda karışıklık için özür:

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 

Ben rotasyon sorunları ile bunu temizlemek yanı sıra yardım için kod/yardımına iyileştirmeler için herhangi önerileri açık. Yardım için teşekkürler.

cevap

0

rotation matris

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

eksen menşei hakkında bir açı theta noktalarına bir dizi döner. Verilen bir nokta etrafında bir noktaya döndürmek için

, sahip olmak:

-shift senin noktaları böylece eksen kökeni ile rotatioin noktası concides - rotasyon matrisi kullanarak noktasını döndürmek - puanlarınızı geri kaydırın

Aşağıdaki kod, önerilen yaklaşımın uygulandığı işlevin değiştirilmiş bir sürümüdür. Ben

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

yine ağırlık merkezi olarak döndürme noktasını kurdum kodunda

, farklı bir şekilde bunları hesaplayabilir.

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 

Ayrıca ek değişiklik bir çift yaptık:

  • I rad için deg gelen giriş açısının bir dönüşüm ekledik; Giriş zaten rad
  • içinde olduğunu varsayalım, bu üçgeni çizmek için yolu güncelleştirdim.

bir öneri olarak, kendi işlevine giriş sayısını thest için nargin kullanabilir ve onlar var içinde varargin test yerine bunları incelemek.Bu yardımcı olur

enter image description here

Umut.

Qapla '