java中的设置一个矩形 老师让做一道题,输入矩形的四个坐标点,上下左右四个,但是矩形的坐标应该用什么表示呢?我现在还没有学到swing applet那些东西就是一般的doc下操作的!提示输入坐标点,最后如何判断输入的坐标满足矩形的条件!高手们 我在线等哦! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 可以将点作为一个类来处理在二维平面上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) 则为矩形; 3楼不对吧?!·他说得矩形又不一定要是横放得,有可能是斜得,那么你得条件就不满足了啊!就按照数学中给定坐标判断是否矩形得算法判断,给定四个点,两个角是直角就是矩形,A(x1, y1)B(x2, y2) C(x3, y3),那么如何判断角ABC是直角呢?找本数学书看看,给定三个点确定是否直角,我记得是有公式得!太久没学数学了 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."); }} 上一个是证明斜矩形的,以下为全部代码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."); }} 用rectangle类来表示吧,new rectangle();构造函数中带入左上角的横纵坐标,和长方形的长度和宽度。 有一个地方写错了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.... Rectangle类只能用于新建矩形而不能判断是否为矩形吧?! 请推荐Java GUI 编写工具 帮帮忙!! jxl生成excel的行数问题 急:找不到 msvcr71.dll 关于导入jar包的问题 是否在构造函数中初始化有什么区别? [新手系列] 清华研究生因找不到工作跳楼自杀 能不能具体的解释一下mark()与reset()的用法 怎樣在java 中開ie 瀏覽器? 各位老大,谁知道如何更改JTable的表格头的高度。 面试题 请各位大虾帮忙下 一个很奇怪的编码习惯
在二维平面上
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)
则为矩形;
就按照数学中给定坐标判断是否矩形得算法判断,给定四个点,两个角是直角就是矩形,A(x1, y1)
B(x2, y2) C(x3, y3),那么如何判断角ABC是直角呢?找本数学书看看,给定三个点确定是否直角,我记得是有公式得!太久没学数学了
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.");
}
}
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.");
}
}
有一个地方写错了
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....