function scaling = addvec(resultant,varargin) % graphical interface for adding vectors. % RATHER THAN CHANGING Vone and Vtwo, change the scale factor. % switch to a matrix formulation so you can index the various vectors % ad add as many as you want. % Add a solve key % Add more vectors % add a key to add a small random component to each vector mat = []; redraw=1; try for k=1:length(varargin) foo = varargin{k}; mat = [mat, foo(:)]; end catch error('Vectors must all be the same length'); end [dimension,nvecs] = size(mat); scaling = ones(nvecs,1); activewidth=8; normalwidth=3; activevec=1; scalefactor = 1.5; plotrangex = max( resultant(1), max(cumsum(abs(mat(1,:))))); plotrangey = max( resultant(2), max(cumsum(abs(mat(2,:))))); if dimension == 3 plotrangez = max( resultant(3), max(cumsum(abs(mat(3,:))))); end ff = figure(1); clf; set(ff,'WindowButtonDownFcn', 'rotate3d(''down'')'); set(ff,'WindowButtonUpFcn', 'rotate3d(''up'')'); if dimension == 3 plot3([0 1], [0,1],[0,1]); grid on; end set(gca,'xlim', scalefactor*plotrangex*[-1,1], 'ylim', scalefactor*plotrangey*[-1,1]); if dimension == 3 set(gca, 'zlim', scalefactor*plotrangey*[-1,1]); end if dimension == 3 plotrangez = max( resultant(1), max(cumsum(abs(mat(3,:))))); set(gca, 'zlim', scalefactor*plotrangez*[-1,1]); end coords = 1:dimension; noextension = 0; vectorlines = makelines(resultant,mat,scaling,... coords, activevec, normalwidth, activewidth,noextension); drawlines(vectorlines, resultant,mat,scaling,... coords, activevec, normalwidth, activewidth,noextension); while 1 w = waitforbuttonpress; if w == 1 k = get(gcf,'CurrentChar'); switch k case 'c' % change camera position set(gca,'CameraPosition', mat(:,activevec)); case 'f' % flip the sign of the active vector scaling(activevec) = -scaling(activevec); redraw = 1; case {'b','s'} % enlarge or shrink the axes if k=='b' scalefactor = 1.5*scalefactor; else scalefactor = .66*scalefactor; end set(gca, 'xlim', scalefactor*plotrangex*[-1,1], ... 'ylim', scalefactor*plotrangey*[-1,1]); if dimension == 3 set(gca, 'zlim', scalefactor*plotrangez*[-1,1]); end redraw = 1; case 'e' noextension = ~ noextension; redraw = 1; case {29,'='} % grow the vector scaling(activevec) = 1.05*scaling(activevec); redraw = 1; case {28, '-'} % shrink the vector scaling(activevec) = 0.95*scaling(activevec); redraw = 1; case 'x' % print out the solution so far disp(scaling); case 'n' % move to the next vector activevec = activevec+1; if activevec > nvecs activevec = 1; end redraw = 1; case 'm' % move to the previous vector activevec = activevec - 1; if activevec <= 0 activevec = nvecs; end redraw=1; case 'r' % prepare for rotation rotate3d on; case {'q',27} break; end else %mouse click end if redraw redraw = 0; drawlines(vectorlines, resultant,mat,scaling,... coords, activevec, normalwidth, activewidth,noextension); end end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function res = makelines(resultant, mat, scaling, coords,activevec,normalwidth,activewidth,noextension) shadowgray = [.8,.8,.8]; [dimension,nvecs] = size(mat); mymat = mat(coords,:).*kron(scaling(:)',ones(dimension,1)); colors = 'bgmckbgmckbgmckbgmckbgmckbgmck'; colors = [colors, colors, colors, colors]; if length(coords) == 2 res.resultant = line( [resultant(coords(1))],[resultant(coords(2))]); else % 3 -dimensional res.resultant = line(resultant(coords(1)), resultant(coords(2)), ... resultant(coords(3))); res.resultantxy = line(resultant(coords(1)), resultant(coords(2)), ... resultant(coords(3)), 'Marker', 'diamond', ... 'markersize', 10, 'MarkerFaceColor', shadowgray, 'EraseMode','xor'); res.resultantyz = line(resultant(coords(1)), resultant(coords(2)), ... resultant(coords(3)),'Marker', 'diamond', ... 'markersize', 10, 'MarkerFaceColor', shadowgray, 'EraseMode','xor'); res.resultantxz = line(resultant(coords(1)), resultant(coords(2)), ... resultant(coords(3)), 'Marker', 'diamond', ... 'markersize', 10, 'MarkerFaceColor', shadowgray, 'EraseMode','xor'); end set(res.resultant, 'Color', 'r', 'Marker', 'diamond', ... 'markersize', 10, 'MarkerFaceColor', 'r', 'EraseMode','xor'); heads = cumsum(mymat,2); tails = cumsum( [zeros(dimension,1), mat(:,1:end-1)],2); big = 100; for k=1:nvecs xpts = [heads(coords(1),k), tails(coords(1),k)]; ypts = [heads(coords(2),k), tails(coords(2),k)]; if length(coords) == 2 res(k).vec = line(xpts, ypts, 'color',colors(k), ... 'linewidth', normalwidth, 'EraseMode', 'Xor'); else zpts = [heads(coords(3),k), tails(coords(3),k)]; res(k).vec = line(xpts, ypts, zpts, 'color', colors(k), ... 'linewidth', normalwidth, 'EraseMode', 'Xor'); res(k).shadowxy = line(xpts,ypts,[0 0], 'color', shadowgray, ... 'linewidth', normalwidth, 'EraseMode', 'Xor'); res(k).shadowxz = line(xpts, [0,0], zpts, 'color', shadowgray, ... 'linewidth', normalwidth, 'EraseMode', 'Xor'); res(k).shadowyz = line([0,0], ypts, zpts, 'color', shadowgray, ... 'linewidth', normalwidth, 'EraseMode', 'Xor'); end extxpts = [heads(coords(1),k) + big*mat(coords(1),k)*[-1,1]]; extypts = [heads(coords(2),k) + big*mat(coords(2),k)*[-1,1]]; if length(coords == 2) res(k).ext = line(extxpts,extypts,'color',colors(k), ... 'LineStyle', ':', 'EraseMode', 'Xor'); else extzpts = [heads(coords(3),k) + big*mat(coords(3),k)*[-1,1]]; res(k).ext = line(extxpts,extypts,extzpts, 'color',colors(k), ... 'LineStyle', ':', 'EraseMode', 'Xor'); end end set(res(activevec).vec, 'LineWidth', activewidth); return %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function drawlines(res, resultant, mat, scaling, coords,activevec,normalwidth,activewidth,noextension) [dimension,nvecs] = size(mat); dimension = length(coords); mymat = mat(coords,:).*kron(scaling(:)',ones(dimension,1)); colors = 'bgmckbgmckbgmckbgmckbgmckbgmck'; colors = [colors, colors, colors, colors]; %set(res(1).resultant, 'Xdata', [resultant(coords(1))], ... % 'Ydata', [resultant(coords(2))], ... % 'Color', 'r', 'linewidth', normalwidth); heads = cumsum(mymat,2); tails = cumsum( [zeros(dimension,1), mymat(:,1:end-1)],2); big = 100; for k=1:nvecs xpts = [heads(coords(1),k), tails(coords(1),k)]; ypts = [heads(coords(2),k), tails(coords(2),k)]; set(res(k).vec, 'Xdata', xpts, 'Ydata', ypts, 'LineWidth', normalwidth) if dimension == 3 zpts = [heads(coords(3),k), tails(coords(3),k)]; set(res(k).vec, 'Zdata', zpts); zplane = min(get(gca, 'Zlim')); yplane = max(get(gca, 'Ylim')); xplane = max(get(gca,'Xlim')); set(res(k).shadowxy, 'Xdata', xpts, 'Ydata', ypts,... 'Zdata', zplane.*[1,1], ... 'LineWidth', normalwidth); set(res(k).shadowxz, 'Xdata', xpts, ... 'Ydata', yplane.*[1,1], ... 'Zdata', zpts, 'LineWidth', normalwidth); set(res(k).shadowyz, 'Xdata', xplane.*[1,1], ... 'Ydata', ypts, ... 'Zdata', zpts, 'LineWidth', normalwidth); if k==1 set(res(1).resultantxy, 'Zdata', zplane); set(res(1).resultantxz, 'Ydata', yplane); set(res(1).resultantyz, 'Xdata', xplane); end end extxpts = [tails(coords(1),k) + big*mat(coords(1),k)*[-1,1]]; extypts = [tails(coords(2),k) + big*mat(coords(2),k)*[-1,1]]; set(res(k).ext, 'Xdata', extxpts, 'Ydata', extypts); if noextension set(res(k).ext, 'Color', [1 1 1]); else set(res(k).ext, 'Color', colors(k)); end if dimension == 3 extzpts = [tails(coords(3),k) + big*mat(coords(3),k)*[-1,1]]; set(res(k).ext, 'Zdata', extzpts); end end set(res(activevec).vec, 'LineWidth', activewidth); return