日期:2014-05-20 浏览次数:20662 次
public class Point { private int x; private int y; public Point(int x,int y){ this.x=x; this.y=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; } }
public class Line { private Point point; private double slope; public boolean isParallel(Line line) { return false; } public Line(Point point,double slope){ this.point=point; this.slope=slope; } public Line(Point p1,Point p2){//从这里开始实现 } }
public class Line { private Point point; private double slope; public double getSlope() { return slope; } public void setSlope(double slope) { this.slope = slope; } public Line(Point point, double slope) { this.point = point; this.slope = slope; } public Line(Point p1, Point p2) { this.slope = (p2.getY() - p1.getY()) / (p2.getX() - p1.getX()); } public Line(int a, int b) { this.slope = -b / a; } public boolean isParallel(Line line) { return this.getSlope() == line.getSlope(); } }
------解决方案--------------------
//楼主可以参考一下 public class Line { private Point point; private double slope; public boolean isParallel(Line line) { if(this.slope==line.slope)return true; return false; } public Line(Point point,double slope){ this.point=point; this.slope=slope; } public Line(Point p1,Point p2){ this(p1,p2.getY()-p1.getY()/(p2.getX()-p1.getX()));//调用上面的构造器,求得斜率 } public Line(int a,int b){ this(new Point(a,0),new Point(0,b)); } public static void main(String []args){ Line l1=new Line(new Point(0,1),2); Line l2=new Line(new Point(2,3),3); System.out.print(l1.isParallel(l2)); } }
------解决方案--------------------
斜率好求,注意分母是0的情况即可。
------解决方案--------------------