第1个回答 2012-03-23
如果要考虑4个象限,考虑各种x3y3x4y4, 比较麻烦。
大致思路,计算b点到线L1的距离,计算c点到线L1的距离,
用ang3,ang4 与 ang 的相对位置判断 l1 和 l2 应当相加还是相减得 总距离。
程序如下(没考虑4个象限):
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
void main()
{
double r1,r2;
double R=5.0, ang=1.05;
double Bx,By,P1x,P1y,P2x,P2y,l1,l2,L;
double x3=1,y3=3,x4=8,y4=10;
double ang3,ang4;
ang3 = atan(y3/x3);
ang4 = atan(y4/x4);
printf("input angle in deg: \n");
scanf("%lf",&ang);
ang = ang / 180.0 * 3.14159265;
Bx = R * cos(ang);
By = R * sin(ang);
r1 =( (x3*Bx)+(y3*By)) / (R*R);
P1x = r1 * Bx;
P1y = r1 * By;
l1 = sqrt( (x3-P1x)*(x3-P1x) + (y3-P1y)*(y3-P1y) );
r2 =( (x4*Bx)+(y4*By)) / (R*R);
P2x = r2 * Bx;
P2y = r2 * By;
l2 = sqrt( (x4-P2x)*(x4-P2x) + (y4-P2y)*(y4-P2y) );
if ( (ang3 >= ang) && (ang4 <= ang) ) L= fabs(l2 + l1);
else L = fabs(l2-l1);
printf("distance: %.2lf ",L);
}本回答被提问者和网友采纳
第2个回答 2012-03-23
#include <stdio.h>
#include <math.h>
#define PI acos(-1.0);
int main()
{
double Lab,L2,a,cosab_L2,len=0,k2;//所求长度==Vab*VL2/|VL2|;向量算VL2=(1,k2),Vab(7,7);
scanf("%lf",&a);
a=a*180/PI;//如果是角度数
k2=-1/tan(a));
Lab=sqrt(2)*7;
L2=sqrt(1+k2*k2);
cosab_L2=(7+7*k2)/L2/Lab;
len=L2*Lab*cosab_L2/L2;
cout<<abs(len)<<endl;
return 0;
}
也许需要特判a==0||a>90等 但是应该也不难 我写了主要思路 剩下的你应该没问题了