How to make two tangent to the ellipse?

edited September 2017 in Questions about Code

I have an ellipse and a point outside. I need to make 2 tangents to the ellipse from that point. Also I have a problem: too few points on ellipse to make a tangents. All I could done is:

class Point
{
  float x;
  float y;

  Point(float _x, float _y)
  {
    x = _x;
    y = _y;
  }
}

ArrayList<Point> listEllipsePoint;

float x = 400;
float y = 400;
float a = 250;
float b = 150;
float c = sqrt(pow(a/2,2)-pow(b/2,2));

Point A = new Point(385,163);

void setup() {
    size(800, 800);
    background(153, 153, 255);

    ellipse(x,y,a,b);
    findPoint(x, y);
for(int i=0; i<listEllipsePoint.size(); i++)
{

}
   doTangent(listEllipsePoint.get(1).x, listEllipsePoint.get(1).y,x,y);
  strokeWeight(5);
  point(A.x, A.y);
  strokeWeight(1);
}

void draw() {

}

boolean isTangent(float _x, float _y)
{
  for(int i=0; i<listEllipsePoint.size(); i++)
  {
    if((_x-listEllipsePoint.get(i).x)/(listEllipsePoint.get(i).x-A.x)==(_y-listEllipsePoint.get(i).y)/(listEllipsePoint.get(i).y-A.y))
    {
      println("ddd");
      return true;
    }
  }


  return false;
}

void doTangent(float _x1, float _x2, float x_center, float y_center)
{    
  ArrayList<Point> listPoint = new ArrayList<Point>();

    strokeWeight(1);
  for(float i=0; i<800; i++)
  {
    for(float j=0; j<800; j++)
    {
      if(((((i-x_center)*(_x1-x_center) )/pow(a/2,2))+(((j-y_center)*(_x2-y_center))/pow(b/2,2))==1) && isTangent(i,j))
      {
        println(i,j);
        point(i, j);

        listPoint.add(new Point(i, j));
      }
    }
  }

  for(int i=0; i<listPoint.size()-1; i++)
  {
   line( listPoint.get(i).x, listPoint.get(i).y, listPoint.get(i+1).x, listPoint.get(i+1).y);
  }
}

void findPoint(float x_center, float y_center)
{
  listEllipsePoint = new ArrayList<Point>();
  for(float i=0; i<800; i+=1)
    {
      for(float j=0; j<800; j+=1)
      {
        if(isInEllipse(i, j,x_center,y_center))
          {
            strokeWeight(5);
            point(i,j);
             strokeWeight(1);
          listEllipsePoint.add(new Point(i, j));
         // break;
          }
      }
    }    
}

boolean isInEllipse(float _x, float _y, float x_center, float y_center)
{
  if(((pow(_x-x_center,2))/(pow(a/2,2)))+((pow(_y-y_center,2))/(pow(b/2,2))) == 1)  
  {  
    return true;
  }    
  return false;
}
Tagged:

Answers

Sign In or Register to comment.