2016-04-09 12 views
0

Ich bin neu bei MATLAB, habe aber mit JavaScript und anderen Programmiersprachen gearbeitet.MATLAB - Drehen eines gleichseitigen Dreiecks um seinen Mittelpunkt

Ich schreibe ein MATLAB-Programm, das ein gleichseitiges Dreieck mit der Seitenlänge, einer x-Koordinate, einer y-Koordinate und einem Drehwinkel erzeugt. Es funktioniert wie vorgesehen außer den Rotationen. Ich verwende eine Rotationsmatrix, um das Dreieck zu drehen. Dies funktioniert, außer dass es sich um den Ursprung dreht, anstatt sich auf der Stelle zu drehen. (siehe untenstehendes Beispiel).

90-Grad-Drehungen Beispiel

enter image description here

Um es an Ort und Stelle zu drehen Ich glaube, ich brauche die Mitte des Dreiecks zu berechnen und zu drehen, dass dann um (irgendwie). Ich bin mir nicht sicher, wie ich das machen soll oder ob es einen einfacheren/besseren Weg gibt, dies zu tun. Ich habe gesehen, dass es eine Rotationsfunktion gibt, aber von dem, was ich gesehen habe, ist es für sphärischen Raum, nicht für kartesische Ebenen.

-Code ist unten, sorry für das Chaos:

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 

Ich bin offen für alle Vorschläge für eine Verbesserung des Kodex/help es sowie Hilfe bei den Dreh Probleme zu bereinigen. Danke für die Hilfe.

Antwort

0

Die rotation Matrix

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

dreht, um eine Menge von Punkten eines Winkels theta um den Ursprung der Achsen.

Um einen Punkt, um einen bestimmten Punkt zu drehen, müssen Sie:

-shift Ihre Punkte, so dass die rotatioin Punkt concides mit dem Ursprung der Achsen - drehen, um den Punkt der Rotationsmatrix mit - Stellen Sie Ihre Punkte zurück

Der folgende Code ist eine modifizierte Version Ihrer Funktion, in der der vorgeschlagene Ansatz implementiert wurde.

Im Code, den ich den Drehpunkt als Schwerpunkt dennoch

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

festgelegt haben, können Sie sie in einer anderen Art und Weise berechnen.

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 

ich gemacht habe auch ein paar zusätzliche Änderung:

  • ich deg-rad eine Umwandlung des Eingangswinkel hinzugefügt haben; Sie können es verwerfen, wenn Sie annehmen, dass die Eingabe bereits in rad
  • ist Ich habe die Art und Weise aktualisiert, das Dreieck zu plotten.

Als Anregung können Sie nargin verwenden, um die Anzahl der Eingabe in Ihrer Funktion thest und varargin sie statt Tests zu untersuchen, in dem sie existieren.

enter image description here

Hoffnung, das hilft.

Qapla '

Verwandte Themen