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
. 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.