「java直线斜率」垂直的线 斜率

博主:adminadmin 2022-11-28 06:49:09 64

今天给各位分享java直线斜率的知识,其中也会对垂直的线 斜率进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

用JAVA求一条直线的斜率

k

=

tan

a

//斜率

tan

a

=

(y2-y1)/(x2-x1)//a为度数,(x1,y1),(x2,y2)分别为直线2个点的坐标

public

class

Test

{

public

Double

k;

//斜率

//计算斜率

public

Double

countK(Double

x1,Double

y1,Double

x2,Double

y2){

return

(y2-y1)/(x2-x1);

}

public

static

void

main(String[]

args)

{

System.out.println(new

Test().countK(3.0,

4.0,

5.0,

6.0));

}

}

java 判断直线状况

public class LineTest {

public static void main(String[] args) {

Point p1 = new Point(0, 3), p2 = new Point(4, 0);

Line l1 = new Line(0, 3, 4, 0),

l2 = new Line(p1, p2),

l3 = new Line(0,4,0,3),

l4 = new Line(4,4,0,4),

l5 = new Line(0,0,3,3);

System.out.println("l1:"+l1);

System.out.println("l2:"+l2);

System.out.println("l3:"+l3);

System.out.println("l4:"+l4);

System.out.println("l5:"+l5);

System.out.println("l1 length:"+l1.getLength());

System.out.println("l2 length:"+l2.getLength());

System.out.println("l3 length:"+l3.getLength());

System.out.println("l4 length:"+l4.getLength());

System.out.println("l1 isHorizontal:"+l1.isHorizontal());

System.out.println("l3 isHorizontal:"+l3.isHorizontal());

System.out.println("l1 isVertical:"+l1.isVertical());

System.out.println("l4 isVertical:"+l4.isVertical());

System.out.println("l1 slope:"+l1.getSlope());

System.out.println("l3 slope:"+l3.getSlope());

System.out.println("l4 slope:"+l4.getSlope());

System.out.println("l5 slope:"+l5.getSlope());

System.out.println("l1 midpoint:"+l1.getMidpoint().toString());

System.out.println("l3 midpoint:"+l3.getMidpoint().toString());

System.out.println("l1 equals l2:"+l1.equals(l2));

System.out.println("l1 equals l3:"+l1.equals(l3));

System.out.println("l2 equals l3:"+l2.equals(l3));

}

}

class Line {

private Point p1,p2;

public Line(Point p1, Point p2) {

super();

this.p1 = p1;

this.p2 = p2;

}

public Line(int x1,int y1,int x2,int y2){

super();

this.p1=new Point(x1, y1);

this.p2=new Point(x2, y2);

}

public int getLength(){

int a=(int) Math.abs(p1.getX()-p2.getX());

int b=(int) Math.abs(p1.getY()-p2.getY());

return (int) Math.sqrt(a*a+b*b);

}

public boolean isHorizontal(){

return Math.abs(p1.getX()-p2.getX())=0.00001;

}

public boolean isVertical(){

return Math.abs(p1.getY()-p2.getY())=0.00001;

}

public double getSlope(){

double a=p1.getX()-p2.getX();

double b=p1.getY()-p2.getY();

if(b==0.0)return Double.MAX_VALUE;

return a/b;

}

public Point getMidpoint(){

return new Point((int)((p1.getX()+p2.getX())/2)

,(int)((p1.getY()+p2.getY())/2));

}

public boolean equals(Line ol){

return (this.p1.equals(ol.getP1()))

(this.p2.equals(ol.getP2()));

}

public Point getP1() {

return p1;

}

public void setP1(Point p1) {

this.p1 = p1;

}

public Point getP2() {

return p2;

}

public void setP2(Point p2) {

this.p2 = p2;

}

@Override

public String toString(){

StringBuilder sb=new StringBuilder();

sb.append("[")

.append("(").append(p1.getX()).append(",")

.append(p1.getY()).append(")").append(",")

.append("(").append(p2.getX()).append(",")

.append(p2.getY()).append(")")

.append("]");

return sb.toString();

}

}

有个小问题就是你要求用Point做点,而Point点的数据类型是int,计算中点时会损失精度,可以改成Point2D实现。

java求高手改错

其实如一楼所说这种最好还是用2个点定义一条直线必要好点。但是根据题目的要求。我完善和更改了一些漏洞。都写了注释的。数学不是很好,可能还有一些地方没有想到。代码如下:

=====================================================

public class Line {

private Point point;

private double slope;

public Line(Point point, double slope) {

this.setPoint(point);

this.setSlope(slope);

}

public Line(Point p1, Point p2) {

this(p1, 0);

if(p1.getX() == p2.getX()){ // 判断是否斜率存在。不存在斜率设置为无穷大 否则设置为计算所得斜率

this.setSlope(Double.MAX_VALUE);

}

else{

this.setSlope((double)(p2.getY() - p1.getY())/(double)(p2.getX() - p1.getX()));

}

}

public Line(int a, int b) {

this(new Point(a, 0), new Point(0, b));

}

public void setSlope(double slope) {

this.slope = slope;

}

public void setPoint(Point point) {

this.point = point;

}

public double getSlope() {

return slope;

}

public Point getPoint() {

return point;

}

public boolean isParallel(Line line) {

if(this.point.isSamePoint(line.point)){ // 同一条直线重合 或者已经相交 故不平行

return false;

}

else{

if(this.isPointOnLine(line.point)){ // 如果两条直线的定义点都在各自的线上那么说明他们重合不平行

return false;

}

else{

return this.slope == line.slope; //否则才判断斜率是否相等

}

}

}

public boolean isPointOnLine(Point p){

double tmp = (double)(this.point.y - p.getY())/(double)(this.point.x - p.getX());

if(this.slope == tmp){

return true;

}

return false;

}

public static void main(String[] args) {

Line l1 = new Line(new Point(1, 0), new Point(1, 3));

Line l2 = new Line(new Point(2, 0), new Point(2, 3));

System.out.println(l1.isParallel(l2));

}

}

class Point{

int x,y;

public Point(int a,int b){

this.x = a;

this.y = b;

}

public int getX(){

return x;

}

public int getY(){

return y;

}

public boolean isSamePoint(Point p){

if(this.x == p.getX() this.y == p.getY()){

return true;

}

return false;

}

}

=============================

程序执行结果为 true

java编程判断这两条直线是否相交?,若相交,求出交点。

您好,下面是我写的代码,麻烦看一下是否符合要求。

Point类:

public class Point {

  private int x;

  private int y;

  public int getX() {

      return x;

  }

  public void setX(int x) {

      this.x = x;

  }

  public int getY() {

      return y;

  }

  public void setY(int y) {

      this.y = y;

  }

}

Line类:

public class Line {

  private Point a;

  private Point b;

  public Point getA() {

      return a;

  }

  public Point getB() {

      return b;

  }

  public Line(Point a, Point b) {

      this.a = a;

      this.b = b;

  }

}

LineIntersect类:

public class LineIntersect {

  private Line ab;

  private Line cd;

  public LineIntersect(Line ab, Line cd) {

      this.ab = ab;

      this.cd = cd;

  }

  public void JudgeLineIntersect() {

      Point A = this.ab.getA();

      Point B = this.ab.getB();

      Point C = this.cd.getA();

      Point D = this.cd.getB();

      int d1 = (B.getY() - A.getY()) * (D.getX() - C.getX());

      int d2 = (B.getX() - A.getX()) * (D.getY() - C.getY());

      int d3 = (C.getY() - A.getY()) * (B.getX() - A.getX());

      int d4 = (C.getX() - A.getX()) * (B.getY() - A.getY());

      if (d1 == d2) {

          System.out.println("两条直线平行。");

      } else if (d3 == d4) {

          System.out.println("两条直线重合。");

      } else {

          System.out.println("两条直线相交。");

          int d5 = C.getY() * D.getX() - C.getX() * D.getY();

          int d6 = A.getY() * B.getX() - A.getX() * B.getY();

          float intersect_x = ((C.getX() - D.getX()) * d6 - (A.getX() - B.getX()) * d5)

                  / ((B.getY() - A.getY()) * (D.getX() - C.getX()) - (B.getX() - A.getX()) * (D.getY() - C.getY()));

          float intersect_y = ((C.getY() - D.getY()) * d6 - (A.getY() - B.getY()) * d5)

                  / ((B.getY() - A.getY()) * (D.getX() - C.getX()) - (B.getX() - A.getX()) * (D.getY() - C.getY()));

          System.out.println("交点的X坐标为:" + intersect_x);

          System.out.println("交点的Y坐标为:" + intersect_y);

      }

  }

}

下面是主类(测试类):

import java.util.Scanner;

public class Main {

  public static void main(String[] args) {

      System.out.print("请输入A点的X坐标:");

      Scanner inAX = new Scanner(System.in);

      int ax = inAX.nextInt();

      System.out.print("请输入A点的Y坐标:");

      Scanner inAY = new Scanner(System.in);

      int ay = inAY.nextInt();

      System.out.print("请输入B点的X坐标:");

      Scanner inBX = new Scanner(System.in);

      int bx = inBX.nextInt();

      System.out.print("请输入B点的Y坐标:");

      Scanner inBY = new Scanner(System.in);

      int by = inBY.nextInt();

      System.out.print("请输入C点的X坐标:");

      Scanner inCX = new Scanner(System.in);

      int cx = inCX.nextInt();

      System.out.print("请输入C点的Y坐标:");

      Scanner inCY = new Scanner(System.in);

      int cy = inCY.nextInt();

      System.out.print("请输入D点的X坐标:");

      Scanner inDX = new Scanner(System.in);

      int dx = inDX.nextInt();

      System.out.print("请输入D点的Y坐标:");

      Scanner inDY = new Scanner(System.in);

      int dy = inDY.nextInt();

      Point A = new Point();

      A.setX(ax);

      A.setY(ay);

      Point B = new Point();

      B.setX(bx);

      B.setY(by);

      Point C = new Point();

      C.setX(cx);

      C.setY(cy);

      Point D = new Point();

      D.setX(dx);

      D.setY(dy);

      Line AB = new Line(A, B);

      Line CD = new Line(C, D);

      LineIntersect lineIntersect = new LineIntersect(AB, CD);

      lineIntersect.JudgeLineIntersect();

      inAX.close();

      inAY.close();

      inBX.close();

      inBY.close();

      inCX.close();

      inCY.close();

      inDX.close();

      inDY.close();

  }

}

下面是我刚才做的测试结果:

用JAVA语言证明两条直线平行?

两直线平行的条件:

如果两条直线的斜率都存在, 并且相等, 那么这两条直线平行. (y=2x + 1 and y=2x - 1)

如果两条直线的斜率都不存在, 那么这两条直线平行. (x = 1 and x= -1)

以上有2种特殊情况需要注意:

a. 斜率都为0, 例如 y= 1 and y= -1.

b. 斜率不存在, 例如 x=1 and x= -1.

这时, 两直线也都平行.

那么用java 实现, 我们就需要用户以y=kx + b, 中的k值, 即斜率.

import java.util.Scanner;

public class ParallelismDemo {

public static void main(String args[])

{

    new ParallelismDemo();

}

//Constructor method, will run during newing instance of this class. 

public ParallelismDemo()

{

    Scanner scan = new Scanner(System.in);

    System.out.println("Please enter first line's value 'k' which format is y = kx + b (If not exist, enter n)");

    String firstValueK = scan.nextLine();

    System.out.println("Please enter second line's value 'k' which format is y = kx + b (If not exist, enter n)");

    String secValueK = scan.nextLine();

    scan.close();

    //Check both k are not exist

    if(firstValueK.equals("n")  secValueK.equals("n"))

    {

        System.out.println("Your lines are parallel");

        return;

    }

    // If both k exist, then check whether they are equals

    else if(firstValueK.equals(secValueK))

    {

        System.out.println("Your lines are parallel");

        return;

    }

    // For the rest condition, they are not parallel

    else 

    {

        System.out.println("Your lines are not parallel");

        return;

    }

   }

}

JAVA直线求斜率的时候为什么要求公约数

因为例如up=4,down=2,和up=8,down=4,计算出来的斜率是一样的,这俩种情况的取值可能会出现在一条直线上。返回值则是a和b的最大公约数,所以JAVA直线求斜率的时候要求公约数。

java直线斜率的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于垂直的线 斜率、java直线斜率的信息别忘了在本站进行查找喔。

The End

发布于:2022-11-28,除非注明,否则均为首码项目网原创文章,转载请注明出处。