Drawing triangles with Matlab!

Whenever I want to test something out really quick, I always find that Matlab is the easiest way to get things done with ease and don’t worry if you don’t have it, most of the functionality can be found in FreeMat (http://goo.gl/1a6zA)!

So today, I’ve come up with the thought of adding some stuff to my library, namely a triangle drawing function! As of now, it only draws an equilateral triangle and is dependent on a line function. I’ve written the idea of my function to do this in Matlab and the code is as follows:

OriginX = 0;
OriginY = 0;
SideLen = 5;
Height = sqrt(3)*SideLen/2;

TCoX = OriginX;
TCoY = OriginY - Height/2;

LCoX = OriginX - SideLen/2;
LCoY = OriginY + Height/2;

RCoX = OriginX + SideLen/2;
RCoY = OriginY + Height/2;

Other than being completely simple, it doesn’t require any particularly hard math. The Sqrt(3)/2 can be a function and will probably be changed to an approximation for my actual library (Sqrt(3)/2 = 0.866 ~= 222/256 with an error of -0.13%. If I was to further increase accuracy past 8 bit to 10bit, I get the approximation of 887/1024 which has an error of -0.02. I could go on further but I don’t see to much point after then! One integer height pixel would be wrong in every 4668!). Obviously though, you want to see the utterly useless video I made in Matlab of a triangle rotating by 0.1 rad every 100ms so voila!

The rotation is achieved using floating points (soon to be eradicated…) and a standard transformation matrix (http://goo.gl/GGlM). Matrices are much easier than one thinks and a 4×4 matrix * 2×2 matrix will always result in a 2×2 matrix, remembering that matrix multiplication isn’t commutative! The multiplication stated above is really easy to implement in C and can be done on one line!

With regards to the rotation and video generation part of the Matlab code:

f = avifile('C:\Users\Haz\Documents\MATLAB\triangle.avi');
fig = figure;

for Angle = 0:0.1:2*pi
NewCoT = [cos(Angle), - sin(Angle); sin(Angle), cos(Angle)]*[TCoX; TCoY];
NewCoL = [cos(Angle), - sin(Angle); sin(Angle), cos(Angle)]*[LCoX; LCoY];
NewCoR = [cos(Angle), - sin(Angle); sin(Angle), cos(Angle)]*[RCoX; RCoY];

hold on
line([TCoX LCoX], [TCoY LCoY]);
line([TCoX RCoX], [TCoY RCoY]);
line([RCoX LCoX], [RCoY LCoY]);

line([NewCoT(1) NewCoL(1)], [NewCoT(2) NewCoL(2)]);
line([NewCoT(1) NewCoR(1)], [NewCoT(2) NewCoR(2)]);
line([NewCoR(1) NewCoL(1)], [NewCoR(2) NewCoL(2)]);
axis([-SideLen, SideLen, -SideLen, SideLen]);
f = addframe(f, getframe(fig));
pause(0.1);
end

f = close(f);

¬†Sorry for the crap variable names, I never really put much effort into Matlab’ing unless its for an assignment!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s