直线的5种方程:
①点斜式:已知直线过点(x0,y0),斜率为k,则直线方程为y-y0=k(x-x0),它不包括垂直于x轴的直线;②斜截式:已知直线在y轴上的截距为b,斜率为k,则直线方程为y=kx+b,它不包括垂直于x轴的直线;③两点式:已知直线经过P1(x1,y1),P2(x2,y2)两点,则直线方程为x-x1/x2-x1=y-y1/y2-y1,它不包括垂直于坐标轴的直线;④截距式:已知直线在x轴和y轴上的截距为a,b,则直线方程为x/a+y/b=1,它不包括垂直于坐标轴的直线和过原点的直线;⑤一般式:任何直线均可写成Ax+By+C=0(A,B不同时为0)的形式。
五种直线方程,解交点。每种直线类的属性由另一个类的对象构成,该类包含对应直线方程的所需变量。
import java.util.Scanner;
public class haihai
{
public static void main(String[] args)
{
Scanner cin=new Scanner(System.in);
SLpoint t1=new SLpoint(1,2,3,4);
SLpoint t2=new SLpoint(5,4,7,2);
SL T=new SL(t1,t2);
double p[]=T.JD(t1, t2);
System.out.println("1.在两点式中,两直线的交点:("+p[0]+","+p[1]+")\n");
System.out.println("2.请输入第一条直线一般式中的三个系数:");
double a1,b1,c1;
a1=cin.nextDouble();
b1=cin.nextDouble();
c1=cin.nextDouble();
YBSxs l1=new YBSxs(a1,b1,c1);
System.out.println("请输入第2条直线一般式中的三个系数:");
double a2,b2,c2;
a2=cin.nextDouble();
b2=cin.nextDouble();
c2=cin.nextDouble();
YBSxs l2=new YBSxs(a2,b2,c2);
Yibanshi y=new Yibanshi(l1,l2);
double p2[]=y.JD();
System.out.println("在一般式中,两直线的交点为:("+p2[0]+","+p2[1]+")\n");
System.out.println("3.请输入点斜式中的第一条直线k,b:");
double k1,b11;
k1=cin.nextDouble();
b11=cin.nextDouble();
System.out.println("请输入点斜式中的第2条直线k,b:");
double k2,b22;
k2=cin.nextDouble();
b22=cin.nextDouble();
XJSxs x1=new XJSxs(k1,b11),x2=new XJSxs(k2,b22);
Xiejieshi x=new Xiejieshi(x1,x2);
double []p3=x.JD();
System.out.println("在点斜式中,两直线的交点为:("+p3[0]+","+p3[1]+")");
System.out.println("4.请输入点斜式中的第一个直线的定点,斜率:");
double X1,Y1,K1;
X1=cin.nextDouble();
Y1=cin.nextDouble();
K1=cin.nextDouble();
DXSxs d1=new DXSxs(X1,Y1,K1),d2;
System.out.println("请输入点斜式中的第2个直线的定点,斜率:");
double X2,Y2,K2;
X2=cin.nextDouble();
Y2=cin.nextDouble();
K2=cin.nextDouble();
d2=new DXSxs(X2,Y2,K2);
Dianxieshi dd=new Dianxieshi(d1,d2);
double []p4=dd.JD();
System.out.println("在点斜式中,两直线的交点为:("+p4[0]+","+p4[1]+")");
System.out.println("5.请输入截距式的第一条直线的a,b:");
double A1,B1;
A1=cin.nextDouble();
B1=cin.nextDouble();
JJSxs T1=new JJSxs(A1,B1),T2;
System.out.println("请输入截距式的第二条直线的a,b:");
A1=cin.nextDouble();
B1=cin.nextDouble();
T2=new JJSxs(A1,B1);
Jiejushi J=new Jiejushi(T1,T2);
double[] p5=J.JD();
System.out.println("在截距式中,两条直线的交点("+p5[0]+","+p5[1]+")");
}
}
class SLpoint
{
private double x1,y1,x2,y2;
public double x1() {return x1;}
public double y1() {return y1;}
public double x2() {return x2;}
public double y2() {return y2;}
public SLpoint(){}
public SLpoint(double p1,double p2,double p3,double p4)
{
x1=p1;y1=p2;x2=p3;y2=p4;
}
}
class YBSxs
{
private double a,b,c;
public YBSxs(){}
public YBSxs(double a1,double b1,double c1){a=a1;b=b1;c=c1;}
public double a() {return a;}
public double b() {return b;}
public double c() {return c;}
}
class XJSxs
{
private double k,b;
public XJSxs() {}
public XJSxs(double k1,double b1) {k=k1;b=b1;}
public double k() {return k;}
public double b() {return b;}
}
class DXSxs
{
private double x,y,k;
public double x() {return x;}
public double y() {return y;}
public double k() {return k;}
public DXSxs() {}
public DXSxs(double x1,double y1,double k1) {x=x1;y=y1;k=k1;}
}
class JJSxs
{
private double a,b;
public double a() {return a;}
public double b() {return b;}
public JJSxs() {}
public JJSxs(double a1,double b1) {a=a1;b=b1;}
}
class Jiejushi
{
private JJSxs l1,l2;
public Jiejushi() {}
public Jiejushi(JJSxs t1,JJSxs t2) {l1=t1;l2=t2;}
public double[] JD()
{
double[] point= {0,0};
if(l1.a()/l1.b()==l2.a()/l2.b()) return point;
point[1]=(l2.a()-l1.a())*l1.b()*l2.b()/(l2.a()*l1.b()-l1.a()*l2.b());
point[0]=l1.a()-point[1]/l1.b();
return point;
}
}
class Dianxieshi
{
private DXSxs l1,l2;
public Dianxieshi(DXSxs t1,DXSxs t2) {l1=t1;l2=t2;}
public double[] JD() {
double[]point= {0,0};
point[0]=(l2.y()-l1.y()+l1.k()*l1.x()+l2.k()*l2.x())/(l1.k()-l2.k());
point[1]=l1.y()+l1.k()*(point[0]);
return point;
}
}
class Xiejieshi
{
private XJSxs l1,l2;
public Xiejieshi() {}
public Xiejieshi(XJSxs t1,XJSxs t2) {l1=t1;l2=t2;}
public double[] JD()
{double[] point= {0,0};
if(l1.k()==l2.k()) return point;
point[0]=(l2.b()-l1.b())/(l1.k()-l2.k());
point[1]=(l1.k()*l2.b()-l2.k()*l1.b())/(l1.k()-l2.k());
return point;
}
}
class Yibanshi
{
private YBSxs l1,l2;
public Yibanshi() {}
public Yibanshi(YBSxs t1,YBSxs t2) {l1=t1;l2=t2;}
public double[] JD()
{
double[] point =new double[2] ;
point[0]=0;point[1]=0;
if((l2.a()*l1.b()-l1.a()*l2.b())==0) return point;
point[0]=(l2.b()*l1.c()-l1.b()*l2.c())/(l2.a()*l1.b()-l1.a()*l2.b());
point[1]=(l1.a()*l2.c()-l2.a()*l1.c())/(l2.a()*l1.b()-l1.a()*l2.b());
return point;
}
}
class SL
{
private SLpoint l1,l2;
public SL(){}
public SL(SLpoint t1,SLpoint t2){l1=t1;l2=t2;}
public double[] JD(SLpoint t1,SLpoint t2)
{
double point[]= {0,0};
if((t1.x1()-t1.x2()==0)&&(t2.x1()-t2.x2()==0)) {return point;}
else if((t1.x1()-t1.x2()==0))
{
double k2=(t2.y1()-t2.y2())/(t2.x1()-t2.x2());
double b2=t2.y1()-k2*t2.x1();
point[0]=t1.x1();
point[1]=k2*point[0]+b2;
return point;
}
else if((t2.x1()-t2.x2()==0))
{
double k1=(t1.y1()-t1.y2())/(t1.x1()-t1.x2());
double b1=t1.y1()-k1*t1.x1();
point[0]=t2.x1();
point[1]=k1*point[0]+b1;
return point;
}
else if((t2.x1()-t2.x2()!=0)&&(t1.x1()-t1.x2()!=0))
{
double k1=(t1.y1()-t1.y2())/(t1.x1()-t1.x2());
double k2=(t2.y1()-t2.y2())/(t2.x1()-t2.x2());
double b1=t1.y1()-k1*t1.x1();
double b2=t2.y1()-k2*t2.x1();
point[0]=(b1-b2)/(k2-k1);
point[1]=k1*point[0]+b1;
return point;
}
else //if((t1.y1()-t1.y2())/(t1.x1()-t1.x2())==(t2.y1()-t2.y2())/(t2.x1()-t2.x2())) {return point;}
return point;
}
}
(有一部分的公式为自己推导出来的,如有错误还请大家指出来,谢谢!)