老师让做一道题,输入矩形的四个坐标点,上下左右四个,但是矩形的坐标应该用什么表示呢?我现在还没有学到swing applet那些东西就是一般的doc下操作的!提示输入坐标点,最后如何判断输入的坐标满足矩形的条件!
高手们 我在线等哦!

解决方案 »

  1.   

    可以将点作为一个类来处理
    在二维平面上
    public class Point{
      double x;
      double y;
      Point(double x,double y){
      this.x=x;
      this.y=y;
     }
    }
     然后在按照不同的顺序比较坐标
    如 ABCD(逆时针) 可判断
     if(A.x==B.x&&A.y==D.y&&C.x==D.x&&C.y==B.y)
      则为矩形;
      

  2.   

    3楼不对吧?!·他说得矩形又不一定要是横放得,有可能是斜得,那么你得条件就不满足了啊!
    就按照数学中给定坐标判断是否矩形得算法判断,给定四个点,两个角是直角就是矩形,A(x1, y1)
    B(x2, y2) C(x3, y3),那么如何判断角ABC是直角呢?找本数学书看看,给定三个点确定是否直角,我记得是有公式得!太久没学数学了
      

  3.   

    import java.util.*;public class Rectangle{
    public static void main(String[] args){
    Scanner reader=new Scanner(System.in);

    System.out.print("Please enter the first coordinates x1,y1 in turn:");
    double x1=reader.nextDouble();
    double y1=reader.nextDouble();

    System.out.print("Please enter the second coordinates x2,y2 in turn:");
    double x2=reader.nextDouble();
    double y2=reader.nextDouble();

    System.out.print("Please enter the third coordinates x3,y3 in turn:");
    double x3=reader.nextDouble();
    double y3=reader.nextDouble();

    System.out.print("Please enter the fourth coordinates x4,y4 in turn:");
    double x4=reader.nextDouble();
    double y4=reader.nextDouble();

    double _x1=x2-x1;                 
    double _y1=y2-y1;

    double _x2=x3-x2;
    double _y2=y3-y2;

    double _x3=x4-x3;
    double _y3=y4-y3;

    double _x4=x1-x4;
    double _y4=y1-y4;
    if(_x1/_y1 == _y2/_x2)                   //证明第一个角是直角,以下同
    {
    if(_x2/_y2 == _y3/_x3)
    {
    if(_x3/_y3 == _y4/_x4)
    {
    System.out.println("It's rectangle.");
    }
    else System.out.println("It's not rectangle.");
    }
    else System.out.println("It's not rectangle.");
    }
    else System.out.println("It's not rectangle.");

    }

      

  4.   

    上一个是证明斜矩形的,以下为全部代码import java.util.*;public class q1{
    public static void main(String[] args){
    Scanner reader=new Scanner(System.in);

    System.out.print("Please enter the first coordinates x1,y1 in turn:");
    double x1=reader.nextDouble();
    double y1=reader.nextDouble();

    System.out.print("Please enter the second coordinates x2,y2 in turn:");
    double x2=reader.nextDouble();
    double y2=reader.nextDouble();

    System.out.print("Please enter the third coordinates x3,y3 in turn:");
    double x3=reader.nextDouble();
    double y3=reader.nextDouble();

    System.out.print("Please enter the fourth coordinates x4,y4 in turn:");
    double x4=reader.nextDouble();
    double y4=reader.nextDouble();

    double _x1=x2-x1;                 
    double _y1=y2-y1;

    double _x2=x3-x2;
    double _y2=y3-y2;

    double _x3=x4-x3;
    double _y3=y4-y3;

    double _x4=x1-x4;
    double _y4=y1-y4; if (_x1 == _x2){                                //正矩形时证明其为矩形
    if(_y2 == _y3){
    if(_x3 == _x4){
    System.out.println("It's rectangle.");
    }
    System.out.println("It's not rectangle.");
    }
    System.out.println("It's not rectangle.");
    }
    else if(_y1 == _y2){
    if(_x2 == _x3){
    if(_y3 == _y4){
    System.out.println("It's rectangle.");
    }
    System.out.println("It's not rectangle.");
    }
    System.out.println("It's not rectangle.");
    }
    else if(_x1/_y1 == _y2/_x2){                   //斜矩形时证明第一个角是直角,以下同
    if(_x2/_y2 == _y3/_x3){
    if(_x3/_y3 == _y4/_x4){
    System.out.println("It's rectangle.");
    }
    else System.out.println("It's not rectangle.");
    }
    else System.out.println("It's not rectangle.");
    }
    else System.out.println("It's not rectangle.");

    }
      

  5.   

    用rectangle类来表示吧,new rectangle();构造函数中带入左上角的横纵坐标,和长方形的长度和宽度。
      

  6.   


    有一个地方写错了
    if (_x1 == _x2){                                //正矩形时证明其为矩形
    if(_y2 == _y3){
    if(_x3 == _x4){
    System.out.println("It's rectangle.");
    }
    System.out.println("It's not rectangle.");
    }
    System.out.println("It's not rectangle.");
    }
    else if(_y1 == _y2){
    if(_x2 == _x3){
    if(_y3 == _y4){
    System.out.println("It's rectangle.");
    }
    System.out.println("It's not rectangle.");
    }
    System.out.println("It's not rectangle.");
    } 中的_x1,_y1等都要去掉_换成x1,y1....
      

  7.   

    Rectangle类只能用于新建矩形而不能判断是否为矩形吧?!