Projected vector

I am writing a shader to draw a frame around a shape geometry, I am passing the corners -2D points- to the shaderprojecting the position on to the edge to check the distance. Somehow the trigonometry is off, i cant see wher i got it wrong ( the points coordinates and uniforms check out).


  vertexShader = `

  //varying vec2 vUv;
  out float isBorder;

uniform int shapeLen;
 uniform  vec2 shapeArr[shapeArrayLength];
 uniform float uWidth;


  void main(){
   
   vec2 pos = position.xy ;
 
    isBorder = 0.0;

//for each side

  for (int i = 0; i < shapeLen ; i++){

    //egde vector (AB)
   highp vec2 AB;
   highp vec2 B;
   
   //edge vector

    if( i < (shapeLen-1 )){ int next = i+1;
    AB = shapeArr[next] - shapeArr[i] ;  }
    else{ AB  = vec2( shapeArr[0] - shapeArr[i]  ) ; } //close the shape

     vec2 ABnorm = normalize(AB);  //unit vector of the edge
     float ABlen = length(AB);//distance( shapeArr[i], B );



    //vector corner to position (AX)
    vec2 AX =  pos - shapeArr[i] ;
    float  AXlen  = length(AX);  //distance( shapeArr[i],  pos );
     vec2 AXnorm = normalize(AX);      //unit vector


     //test corners
    float dist = distance(shapeArr[i], pos );
     if(dist < .150){isBorder = 1.0;}



    //dot product to project point to edge

    float dotSide = dot(AB, AX);
   if(  dotSide >= 0.0 &&  dotSide <= 1.0){
    //isBorder =0.15;



 //angle of both vectors
     float angle = acos(dot(ABnorm, AXnorm));

     //length of projected vector ??
  float APlen = cos(angle) * ABlen;

  // distance to edge 
    float XPlen = sqrt( pow(AXlen, 2.0) - pow(APlen, 2.0));

    // check distance to edge
  if(XPlen < 2.0){isBorder = 1.0;}
   }

  }

   vec4 modelPosition = modelMatrix * vec4(position, 1.0);
    vec4 viewPosition = viewMatrix * modelPosition;
    vec4 clipPosition = projectionMatrix * viewPosition;
    gl_Position = clipPosition;

  }
  `;

without the dot filter

right, I swapped the 2 vectors to get the angle, her ein case anyone needs it:


  vertexShader = `

  //varying vec2 vUv;
  out float isBorder;

uniform int shapeLen;
 uniform  vec2 shapeArr[shapeArrayLength];
 uniform float uWidth;

   varying vec3 vNormal;
  varying vec3 vViewDir;
  uniform vec3 vDir;

  void main(){
   
  //  vUv = uv; 
  // vec2 pos = position.xz ;
   vec2 pos = position.xy ;
 
       isBorder = 0.0;

//for each side / corner

  for (int i = 0; i < shapeLen ; i++){

    //egde vector (AB)
    highp vec2 AB;
    highp vec2 B;
   
     //edge vector
    if( i < (shapeLen-1 )){ int next = i+1;
    AB = shapeArr[next] - shapeArr[i] ;  }
    else{ AB  = vec2( shapeArr[0] - shapeArr[i]  ) ; } //close the shape
    vec2 ABnorm = normalize(AB);  //unit vector of the edge
    float ABlen = length(AB);//distance( shapeArr[i], B );

    //vector corner to position (AX)
    vec2 AX =  pos - shapeArr[i] ;
    float  AXlen  = length(AX);  //distance( shapeArr[i],  pos );
     vec2 AXnorm = normalize(AX);      //unit vector


      /* //test corners
  float dist = distance(shapeArr[i], pos );
     if(dist < .150){isBorder = 1.0;}*/


    //dot product to project point to edge

    float dotSide = dot(AX, AB);

 //angle of both vectors
     float angle = acos(dot(ABnorm, AXnorm));

     //length of projected vector 
  float APlen = cos(angle) * AXlen;

  // distance to edge 
  float XPlen = sqrt( pow(AXlen, 2.0) - pow(APlen, 2.0));

    // check distance to edge
  if(XPlen < uWidth ){
   // straight borders
    isBorder = 1.0;
   }

  }

   vec4 modelPosition = modelMatrix * vec4(position, 1.0);
    vec4 viewPosition = viewMatrix * modelPosition;
    vec4 clipPosition = projectionMatrix * viewPosition;
    gl_Position = clipPosition;

  }
  `;```
![Screenshot 2024-08-13 at 1.42.02 PM|690x478](upload://rVa6LwufcEbfT4KvhwCHiPzYkNV.png)