樹心幽徑

« 三角形的外接圓 | Main | 三角形的旁切圓 »

三角形的內切圓
2017/11/29,16:08

alt

 


/*
   繪出三角形ABC的內接圓
   三角形ABC A(xa,ya), B(xb,yb), C(xc,yc)
   D為角A的角平分線在BC邊的交點,
   E為角B的角平分線在AC邊的交點,
   AD的直線參數式為(xa,ya)+t(xd-xa,yd-ya)
   BE的直線參數式為(xb,yb)+k(xe-xb,ye-yb)
   AD及BE的交點I,即為三角形的內心
   k=((xd-xa)*(yb-ya)-(xb-xa)*(yd-ya))/((ya-yd)*(xe-xb)-(ye-yb)*(xd-xa))
   xi=xb+k*(xe-xb)
   yi=yb+k*(ye-yb)
   s=(a+b+c)/2
   area=pow(s*(s-a)*(s-b)*(s-c),0.5);
   r=area/s;

   I在BC上的垂足J,即切點
   IJ的直線參數式為(xi,yi)+t(yc-yb,xb-xc)
   BC的直線參數式為(xb,yb)+k(xc-xb,yc-yb)
   當k=((xb-xc)*(xb-xi)-(yc-yb)*(yb-yi))/((yc-yb)*(yc-yb)+(xb-xc)*(xb-xc))
     xj=xb+k*(xc-xb)
     yj=yb+k*(yc-yb)
   I在AC上的垂足K,即切點
   IK的直線參數式為(xi,yi)+t(yc-ya,xa-xc)
   AC的直線參數式為(xa,ya)+k(xc-xa,yc-ya)
   當k=((xa-xc)*(xa-xi)-(yc-ya)*(ya-yi))/((yc-ya)*(yc-ya)+(xa-xc)*(xa-xc))
     xk=xa+k*(xc-xa)
     yk=ya+k*(yc-ya)
   I在AB上的垂足L,即切點
   IL的直線參數式為(xi,yi)+t(yb-ya,xa-xb)
   AC的直線參數式為(xa,ya)+k(xb-xa,yb-ya)
   當k=((xa-xb)*(xa-xi)-(yb-ya)*(ya-yi))/((yb-ya)*(yb-ya)+(xa-xb)*(xa-xb))
     xl=xa+k*(xb-xa)
     yl=ya+k*(yb-ya)
*/
void InnerCircle(double xa,double ya,double xb,double yb,double xc,double yc,Image *pim,char *cst){
double c=pow(pow(xa-xb,2)+pow(ya-yb,2),0.5);
double b=pow(pow(xa-xc,2)+pow(ya-yc,2),0.5);
double a=pow(pow(xb-xc,2)+pow(yb-yc,2),0.5);

double xd=xb+c*(xc-xb)/(b+c);
double yd=yb+c*(yc-yb)/(b+c);
double xe=xc+a*(xa-xc)/(a+c);
double ye=yc+a*(ya-yc)/(a+c);
double xf=xa+b*(xb-xa)/(a+b);
double yf=ya+b*(yb-ya)/(a+b);

double s=(a+b+c)/2;
double area=pow(s*(s-a)*(s-b)*(s-c),0.5);
double r=area/s;
printf("s=%.2f area=%.2f r=area/s=%.2f \n",s,area,r);
pim->strokeColor(cst);
pim->draw( DrawableLine(xc,yc,xa,ya));
pim->draw( DrawableLine(xb,yb,xa,ya));
pim->draw( DrawableLine(xb,yb,xc,yc));
pim->draw(DrawableText(xa, ya, "A"));
pim->draw(DrawableText(xb, yb, "B"));
pim->draw(DrawableText(xc, yc, "C"));

pim->draw( DrawableLine(xa,ya,xd,yd));
pim->draw(DrawableText(xd, yd, "D"));
pim->draw( DrawableLine(xb,yb,xe,ye));
pim->draw(DrawableText(xe, ye, "E"));
pim->draw( DrawableLine(xc,yc,xf,yf));
pim->draw(DrawableText(xf, yf, "F"));
double  k=((xd-xa)*(yb-ya)-(xb-xa)*(yd-ya))/((yd-ya)*(xe-xb)-(ye-yb)*(xd-xa));
double  xi=xb+k*(xe-xb);
double  yi=yb+k*(ye-yb);
pim->draw(DrawableText(xi, yi, "I"));
pim->draw(DrawableCircle(xi,yi,xi+r,yi));

k=((xb-xc)*(xb-xi)-(yc-yb)*(yb-yi))/((yc-yb)*(yc-yb)+(xb-xc)*(xb-xc));
double xj=xb+k*(xc-xb);
double yj=yb+k*(yc-yb);
pim->draw(DrawableText(xj, yj, "J"));
pim->draw( DrawableLine(xi,yi,xj,yj));

k=((xa-xc)*(xa-xi)-(yc-ya)*(ya-yi))/((yc-ya)*(yc-ya)+(xa-xc)*(xa-xc));
double  xk=xa+k*(xc-xa);
double  yk=ya+k*(yc-ya);
pim->draw(DrawableText(xk, yk, "K"));
pim->draw( DrawableLine(xi,yi,xk,yk));

k=((xa-xb)*(xa-xi)-(yb-ya)*(ya-yi))/((yb-ya)*(yb-ya)+(xa-xb)*(xa-xb));
double xl=xa+k*(xb-xa);
double yl=ya+k*(yb-ya);
pim->draw(DrawableText(xl, yl, "L"));
pim->draw( DrawableLine(xi,yi,xl,yl));
}

迴響

 
Accessible and Valid XHTML 1.0 Strict and CSS Powered by LifeType