Jump to content

Circle tagents


Recommended Posts

Goal:
Q3sz7.gif.99b923582b9b76338753aa1dcbcdbbca.gif

Looking into tangent lines between two circles and trying to convert this function into vex.
 

The code is originally in Java but looks like it should be easily converted to vex but I'm getting errors with my attempt below. 
 

function float[] common_tangent_line(x1, y1, r1, x2, y2, r2) {
  // Compute the common tangent line of two circles: (x1, y1) - r1 and (x2, y2) - r2
  // Return in the form of line equation: ax + by + c == 0
  delta1 = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) - (r1 + r2) * (r1 + r2);
  delta2 = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) - (r1 - r2) * (r1 - r2);
  p1 = r1 * (x1 * x2 + y1 * y2 - x2 * x2 - y2 * y2);
  p2 = r2 * (x1 * x1 + y1 * y1 - x1 * x2 - y1 * y2);
  q = x1 * y2 - x2 * y1;
  results = [];
  if(delta1 >= 0) {
    l11 = {
      a: (x2 - x1) * (r1 + r2) + (y1 - y2) * Math.sqrt(delta1),
      b: (y2 - y1) * (r1 + r2) + (x2 - x1) * Math.sqrt(delta1),
      c: p1 + p2 + q * Math.sqrt(delta1)
    };
    l12 = {
      a: (x2 - x1) * (r1 + r2) - (y1 - y2) * Math.sqrt(delta1),
      b: (y2 - y1) * (r1 + r2) - (x2 - x1) * Math.sqrt(delta1),
      c: p1 + p2 - q * Math.sqrt(delta1)
    };
    results.push(l11);
    results.push(l12);
  }
  if(delta2 >= 0) {
    l21 = {
      a: (x2 - x1) * (r1 - r2) + (y1 - y2) * Math.sqrt(delta2),
      b: (y2 - y1) * (r1 - r2) + (x2 - x1) * Math.sqrt(delta2),
      c: p1 - p2 + q * Math.sqrt(delta2)
    };
    l22 = {
      a: (x2 - x1) * (r1 - r2) - (y1 - y2) * Math.sqrt(delta2),
      b: (y2 - y1) * (r1 - r2) - (x2 - x1) * Math.sqrt(delta2),
      c: p1 - p2 - q * Math.sqrt(delta2)
    };
    results.push(l21);
    results.push(l22);
  }
  return results;
}

 

Edited by sessionbeer
Link to comment
Share on other sites

11 hours ago, sessionbeer said:

Great, thank you. Last one:
How do I correct the error for a: ?


if(delta1 >= 0) {
    l11 = {
      a: = (x2 - x1) * (r1 + r2) + (y1 - y2) * sqrt(delta1),
      b: (y2 - y1) * (r1 + r2) + (x2 - x1) * sqrt(delta1),
      c: p1 + p2 + q * sqrt(delta1)
    };

 

Looks like homework wasn't done first ;)

 

Link to comment
Share on other sites

Getting no more errors but seems like the results are incorrect. I've made a @common attribute to hold the results which I'm expecting to be (a,b,c) but instead, I'm getting a lot more float values. Attached a hip file too to show all the errors in my ways.
 

function float[] common_tangent_line(float x1, y1, r1, x2, y2, r2) {
  // Compute the common tangent line of two circles: (x1, y1) - r1 and (x2, y2) - r2
  // Return in the form of line equation: ax + by + c == 0
  float delta1 = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) - (r1 + r2) * (r1 + r2);
  float delta2 = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) - (r1 - r2) * (r1 - r2);
  float p1 = r1 * (x1 * x2 + y1 * y2 - x2 * x2 - y2 * y2);
  float p2 = r2 * (x1 * x1 + y1 * y1 - x1 * x2 - y1 * y2);
  float q = x1 * y2 - x2 * y1;
  float results[];
  
  if(delta1 >= 0) {
    float a = (x2 - x1) * (r1 + r2) + (y1 - y2) * sqrt(delta1);
    float b = (y2 - y1) * (r1 + r2) + (x2 - x1) * sqrt(delta1);
    float c = p1 + p2 + q * sqrt(delta1);
    float   l11[];
    l11 = array(a,b,c);

    float a2 = (x2 - x1) * (r1 + r2) - (y1 - y2) * sqrt(delta1);
    float b2 = (y2 - y1) * (r1 + r2) - (x2 - x1) * sqrt(delta1);
    float c2 = p1 + p2 - q * sqrt(delta1);
    float   l12[];
    l12 = array(a,b,c);
    push(results, l11);
    push(results, l12);
  };
  if(delta2 >= 0) {
    float a = (x2 - x1) * (r1 - r2) + (y1 - y2) * sqrt(delta2);
    float b = (y2 - y1) * (r1 - r2) + (x2 - x1) * sqrt(delta2);
    float c = p1 - p2 + q * sqrt(delta2);
    float   l21[];
    l21 = array(a,b,c);

    float a2 = (x2 - x1) * (r1 - r2) - (y1 - y2) * sqrt(delta2);
    float b2 = (y2 - y1) * (r1 - r2) - (x2 - x1) * sqrt(delta2);
    float c2 = p1 - p2 - q * sqrt(delta2);
    float   l22[];
    l22 = array(a,b,c);
    push(results, l21);
    push(results, l22);
  }
  return results;
}

vector point1 = @P;;
vector point2 = (0,"P",1);

f[]@common =  common_tangent_line(point1.x, point1.y, 1.5, point2.x, point2.y, 1);

 

Circle_Tangents_03.hip

Edited by sessionbeer
Link to comment
Share on other sites

10 hours ago, sessionbeer said:

Getting no more errors but seems like the results are incorrect. I've made a @common attribute to hold the results which I'm expecting to be (a,b,c) but instead, I'm getting a lot more float values. Attached a hip file too to show all the errors in my ways.
 


function float[] common_tangent_line(float x1, y1, r1, x2, y2, r2) {
  // Compute the common tangent line of two circles: (x1, y1) - r1 and (x2, y2) - r2
  // Return in the form of line equation: ax + by + c == 0
  float delta1 = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) - (r1 + r2) * (r1 + r2);
  float delta2 = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) - (r1 - r2) * (r1 - r2);
  float p1 = r1 * (x1 * x2 + y1 * y2 - x2 * x2 - y2 * y2);
  float p2 = r2 * (x1 * x1 + y1 * y1 - x1 * x2 - y1 * y2);
  float q = x1 * y2 - x2 * y1;
  float results[];
  
  if(delta1 >= 0) {
    float a = (x2 - x1) * (r1 + r2) + (y1 - y2) * sqrt(delta1);
    float b = (y2 - y1) * (r1 + r2) + (x2 - x1) * sqrt(delta1);
    float c = p1 + p2 + q * sqrt(delta1);
    float   l11[];
    l11 = array(a,b,c);

    float a2 = (x2 - x1) * (r1 + r2) - (y1 - y2) * sqrt(delta1);
    float b2 = (y2 - y1) * (r1 + r2) - (x2 - x1) * sqrt(delta1);
    float c2 = p1 + p2 - q * sqrt(delta1);
    float   l12[];
    l12 = array(a,b,c);
    push(results, l11);
    push(results, l12);
  };
  if(delta2 >= 0) {
    float a = (x2 - x1) * (r1 - r2) + (y1 - y2) * sqrt(delta2);
    float b = (y2 - y1) * (r1 - r2) + (x2 - x1) * sqrt(delta2);
    float c = p1 - p2 + q * sqrt(delta2);
    float   l21[];
    l21 = array(a,b,c);

    float a2 = (x2 - x1) * (r1 - r2) - (y1 - y2) * sqrt(delta2);
    float b2 = (y2 - y1) * (r1 - r2) - (x2 - x1) * sqrt(delta2);
    float c2 = p1 - p2 - q * sqrt(delta2);
    float   l22[];
    l22 = array(a,b,c);
    push(results, l21);
    push(results, l22);
  }
  return results;
}

vector point1 = @P;;
vector point2 = (0,"P",1);

f[]@common =  common_tangent_line(point1.x, point1.y, 1.5, point2.x, point2.y, 1);

 

Circle_Tangents_03.hip

Well done. Check hip, I have added the rest code from the article above. Hope it will help to finish the setup.

cg_v01.hip

cg.gif.ed9901d3156b417c1e133f18bab62688.gif

Edited by fencer
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...