Hello,
I'm working on a script that I will eventually turn into a
function to use for data analysis. The idea is to take a
triangle and translate it so that the "start" point is at
the origin. I then want to rotate the triangle so that the
line defined by "start" and "actual" lies on the positive
x-axis, and calculate the angle between the start-actual
line and the start-target line.
I need the start-actual line to be the reference line
because I need signed angular error (basically, the error
will be positive(up to 180 deg) if the subject went to the
right of the target, and negative if they went to the left
of the target (up to 180deg).
I'm having trouble rotating the triangle... the formula I'm
using almost gives me the right points, but not quite. I
should get finalactualx = 3, and finactualy = 0, but I'm off
by a bit. What am I doing wrong here? I'm also trying to
set up an if statment that accounts for the fact that
equations I'm using are doing a counterclockwise rotation,
so if "actual" is above the x-axis, I actually want to
rotate the point 360-"newactualdegs".
Any help would be greatly appreciated!!
==========
clear all
close all
start = [-1 -1]
actual = [-1 -4]
target = [-3 -2]
% create a translator matrix based on the start location
translator = [start(1)*-1 start(2)*-1];
% translate all the points so that start is at the origin
newstart = start + translator;
newactual = actual + translator;
newtarget = target + translator;
% plot the original points
figure
scatter(start(1),start(2), 'go');
axis ([-15 15 -15 15]);
hold on
scatter (actual(1),actual(2), 'bo');
scatter (target(1),target(2), 'ro');
% plot the translated points
figure
scatter (newstart(1),newstart(2), 'go');
axis ([-15 15 -15 15]);
hold on
scatter (newactual(1), newactual(2),'bo');
scatter (newtarget(1), newtarget(2),'ro');
% rotate the translated points so that the actual point is
on +x-axis
newactualrads = cart2pol(actual(1),actual(2)) % get polar
coord theta (radians) for actual location
newactualdegs = rad2deg(newactualrads) % convert the
polar coord to a degree value
if newactualdegs < 0
newactualdegs = abs(newactualdegs)
finalactualx = newactual(1) * cosd(newactualdegs) -
newactual(2) * sind(newactualdegs);
finalactualy = newactual(2) * cosd(newactualdegs) +
newactual(1) * sind(newactualdegs);
elseif newactualdegs > 0
...
else
continue
end
Thanks,
Jon
Jon Ericson - 31 Jul 2008 17:07 GMT
Sorry -- I posted this thing twice by accident because there
was a long delay before the post went up, so I thought it
didn't work...
Jon Ericson - 31 Jul 2008 18:52 GMT
I think I figured it out, for anyone who is interested...
=========
clear all
close all
start = [0 0]
actual = [10 0]
target = [11 15]
% create a translator matrix based on the start location
translator = [start(1)*-1 start(2)*-1];
% translate all the points so that start is at the origin
newstart = start + translator;
newactual = actual + translator;
newtarget = target + translator;
% plot the original points
figure
scatter(start(1),start(2), 'go');
axis ([-15 15 -15 15]);
hold on
scatter (actual(1),actual(2), 'bo');
scatter (target(1),target(2), 'ro');
% plot the translated points
figure
scatter (newstart(1),newstart(2), 'go');
axis ([-15 15 -15 15]);
hold on
scatter (newactual(1), newactual(2),'bo');
scatter (newtarget(1), newtarget(2),'ro');
% rotate the translated points so that the actual point is
on +x-axis
newactualrads = cart2pol(newactual(1),newactual(2)) % get
polar coord theta (radians) for actual location
newactualdegs = rad2deg(newactualrads) %
convert the polar coord to a degree value
% now that the actual point is on the x-axis, the target
point is either
% above, below, or on the x-axis (not likely). If the
target point is
% above the x-axis, the subject went to the right of the
target, and we
% will get an error in +degrees. If the target point is
below the x-axis,
% the subject went to the left of the target andwe will get
an error in -degrees.
% no matter what, we've already translated the start to
(0,0), so let's
% just be sure that it actually is (0,0)
finalstartx = 0
finalstarty = 0
if newactualdegs < 0 % if the target
is below the x-axis
newactualdegs = abs(newactualdegs) % use the
absolute value of the degree difference because the formulas
below do a CCW rotation
elseif newactualdegs > 0 % if the target
is above the x-axis
newactualdegs = 360-newactualdegs % rotate by 360
the angle for the same reason
elseif newactualdegs == 0
end
finalactualx = newactual(1) * cosd(newactualdegs) -
newactual(2) * sind(newactualdegs);
finalactualy = newactual(2) * cosd(newactualdegs) +
newactual(1) * sind(newactualdegs);
finaltargetx = newtarget(1) * cosd(newactualdegs) -
newtarget(2) * sind(newactualdegs);
finaltargety = newtarget(2) * cosd(newactualdegs) +
newtarget(1) * sind(newactualdegs);
% now plot the
figure
scatter(finalstartx, finalstarty, 'go')
axis ([-15 15 -15 15]);
hold on
scatter(finalactualx, finalactualy, 'bo')
scatter(finaltargetx, finaltargety, 'ro')
=========
If you play around with start, actual, and target
parameters, it seems to work no matter what...