Hello,
I am trying to write a script that will become a function
for calculating the angular error between two lines, where
one line is the reference line. My script takes three
points that need not be located near the origin and then
translates them so that "start" is located at the origin,
shifting "actual" and "target" with it.
I then want to rotate all three lines around the orgin, with
"start" fixed at (0,0), so that the line defined by start
and actual lies along the positive x-axis, still preserving
the original triangle.
Now, the line from start-target should lie either above or
below the x-axis. If it lies above, I want to calculate the
angle between the (start-target) line and the (start-actual)
line and get a NEGATIVE angle. If the start-target line
lies below, I want to get a POSITIVE angle between the two
lines. Basically, I want to see if people walked to the
left (negative error) or right (positive error) of a target,
and how many degrees off they were.
I think there is something wrong with my rotation formula in
the (incomplete) if statement at the end of the script. It
doesn't quite rotate the start-actual line to lie along the
positive x-axis. It should give me a finalactualx = 3, and
a finalactualy = 0, but it's off by a bit.
Any ideas? Any suggestions on how to set up a good if
statement would be helpful too...
Thanks,
Jon
=======
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
jericson81 - 31 Jul 2008 18:52 GMT
I think I figured it out, for anyone who is interested...
Now I just have to make it into a function...
=======
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')