Align apex of one triangle with base of another

29 Views Asked by At

I am having difficulty translating the logic of what I want to do into programming math (geometry). I created this function to generate coordinates for triangles using tutorials but I am having one small problem and that is I cannot seem to figure out how to lock/confine the area from which the triangles should be drawn so that the base of one is always aligned to the apex. Main sources of difficulty: 1) The coordinates (sizes) vary through iterations in a loop, which is what I am having trouble with, and 2) whether the apex is pointed down or up varies through the loop as well so that if one triangle apex is up, the other one is down. I am quite lost.

function [Lx, Ly, Rx, Ry] = trifunc(anglesRad, r1,r2,Lrect_xcenter, Rrect_xcenter,rect_ycenter, s)% r1 = radius:gain, r2 = radius:loss
        theta = pi/6; 
       
        if s == 1 % 1 = left: gain, right: loss; 2 = left: loss, right: gain
            yVec1 = -sin(anglesRad) * r2 + (rect_ycenter/ 3);
            xVec1 = cos(anglesRad) * r2 + Lrect_xcenter;

            yVec2 = -sin(anglesRad) * r1 + (rect_ycenter/ 3);
            xVec2 = cos(anglesRad) * r1 + Rrect_xcenter;
        else
            yVec1 = -sin(anglesRad) * r1 + (rect_ycenter/ 3);
            xVec1 = cos(anglesRad) * r1 + Lrect_xcenter;

            yVec2 = -sin(anglesRad) * r2 + (rect_ycenter/ 3);
            xVec2 = cos(anglesRad) * r2 + Rrect_xcenter;
        end

    % Assign the results to output variables
    tx = round(xVec1); % tx
    ty = round(yVec1); % ty

    tx2 = round(xVec2);
    ty2 = round(yVec2);

    vL = [tx; ty]; % store the coordinates together for rotation

    % Shift left triangles for upward rotation
    Lxcenter = tx(2); % identify the point of rotation from the x vectors
    Lycenter = ty(2); % identify the point of rotation from the y vectors

    l_center = repmat([Lxcenter;Lycenter],1, length(tx)); % create a matrix with the center points
    lshift = vL - l_center; % shift the vectors by their respective center points
  
        if s == 1 % left facing down
            R1 = [cos(theta) sin(theta); -sin(theta) cos(theta)]; % rotation matrix for triangle
       else
           R1 = [cos(theta) -sin(theta); sin(theta) cos(theta)];
        end
        
     shift_l = R1 * lshift; % multiply the rotation matrix by the shifted vectors
     vl = shift_l + l_center; % project back to point of origin
     Lx = round(vl(1,:)); % new x/y vectors for triangles
     Ly = round(vl(2,:));
         
   % Shift right triangles for upward rotation  
     vR = [tx2; ty2];
          Rxcenter = tx2(1);
          Rycenter = ty2(1);
    
      r_center = repmat([Rxcenter;Rycenter],1, length(tx2)); 
      rshift = vR - r_center;
            
            if s == 2 % left facing up
                R2 = [cos(theta) sin(theta); -sin(theta) cos(theta)];
            else
               R2 = [cos(theta) -sin(theta); sin(theta) cos(theta)];% rotation matrix for triangle
            end
                    
       shift_r = R2 * rshift;
       vr = shift_r + r_center;
       Rx = round(vr(1,:));
       Ry = round(vr(2,:));
end

What I have tried:

1] changing the coordinates for the triangles that have upward-facing apices so that they are shifted down... recty_center for tri_1 = yrect_center/3 and for tri_2 (facing down) rect_ycenter /1.5 . This kind of works but they are not exactly aligned.I could just keep incrementally changing it but it would need to be modified across other screens

2] Attempted to created a functional translation matrix for one iteration but it didn't work; got an error trl_m = [1 0 0; 0 1 yVec(1); 0 0 1]

I have added a visualization to better depict the intended outcome Example triangles

0

There are 0 best solutions below