判断空间上三个点是否在同一直线上【找bug篇】
作者:Vashon
时间:20150601 发布时间:20150718
一、拿到问题,首先分析并理清思路。
判断三点是否在同一条直线上需满足以下几点:
1、两点共点(p1与p2共点,p1与p3共点,p2与p3共点)
2、三点纵坐标相等,横坐标不相等
3、三点横坐标相等,且纵坐标不相等(横坐标不相等则不存在除数为0问题)
4、三点共点(可考虑可不考虑)
以上几点需要把斜率考虑在内(斜率相等(除数不能为0,且满足随机输入))
二、创建一个点的实体:
package com.ywx.entities; public class Point{
private float x;
private float y;
public Point(float x,float y){
this.x=x;
this.y=y;
}
public float getX() {
return x;
}
public void setX(float x) {
this.x = x;
}
public float getY() {
return y;
}
public void setY(float y) {
this.y = y;
}
}
三、判断三点是否共线(包含历史测试代码):
package com.ywx.isonlinetest; public class IsOnLine {
public static boolean IsLine(Point p1,Point p2,Point p3){
boolean flag=false;
float k1=0.0f;//斜率
float k2=0.0f;//斜率 //1、两点共点(p1与p2共点,p1与p3共点,p2与p3共点)
if((p1.getX()==p2.getX()&&p1.getY()==p2.getY())
||(p1.getX()==p3.getX()&&p1.getY()==p3.getY())
||(p2.getX()==p3.getX()&&p2.getY()==p3.getY())){
flag=true;
return flag;
} //2、三点纵坐标相等,横坐标不相等
if((p1.getY()==p2.getY())&&(p1.getY()==p3.getY())
&&(p1.getX()!=p2.getX())&&(p1.getX()!=p3.getX())){
flag=true;
return flag;
}
//3、三点横坐标相等,且纵坐标不相等
if((p1.getX()==p2.getX())&&(p1.getX()==p3.getX())
&&(p1.getY()!=p2.getY())&&(p1.getY()!=p3.getY())){
flag=true;
return flag;
}else{//横坐标不相等则不存在除数为0问题
k1=(p3.getY()-p2.getY())/(p3.getX()-p2.getX());
k2=(p1.getY()-p2.getY())/(p1.getX()-p2.getX());
if(k1==k2){
flag=true;
return flag;
}
} //4、三点共点
// if(p1.getX()==p2.getX()&&p1.getX()==p3.getX()
// &&p1.getY()==p2.getY()&&p1.getY()==p3.getY()){
// flag=true;
// }
//5、斜率相等(除数不能为0,且满足随机输入)
//>>>>>中间点p2作为除数,则可以随机输入
// float n=p3.getX()-p2.getX();
// float m=p1.getX()-p2.getX();
//
//
// if(n!=0&&m!=0){//除数不能为0
// k1=(p3.getY()-p2.getY())/n;
// k2=(p1.getY()-p2.getY())/m;
// if(k1==k2){
// flag=true;
// }
// }
return flag;
}
//主方法
@SuppressWarnings("static-access")
public static void main(String args[]){
//三点共点
boolean bool=new IsOnLine().IsLine(new Point(0.0f,0.0f), new Point(0.0f,0.0f), new Point(0.0f,0.0f));
// bool=new IsOnLine().IsLine(new Point(0.0f,0.0f), new Point(0.0f,0.0f), new Point(1.0f,1.0f));
// bool=new IsOnLine().IsLine(new Point(1.0f,2.0f), new Point(2.0f,4.0f), new Point(4.0f,8.0f));
// bool=new IsOnLine().IsLine(new Point(2.0f,3.0f), new Point(2.0f,5.0f), new Point(2.0f,8.0f));
bool=new IsOnLine().IsLine(new Point(1.0f,1.0f), new Point(1.0f,1.0f), new Point(1.0f,1.0f)); //三点随机输入测试
bool=new IsOnLine().IsLine(new Point(0.0f,2.0f), new Point(1.0f,1.0f), new Point(2.0f,0.0f));
bool=new IsOnLine().IsLine(new Point(1.0f,1.0f), new Point(2.0f,0.0f), new Point(0.0f,2.0f));
bool=new IsOnLine().IsLine(new Point(2.0f,0.0f), new Point(0.0f,2.0f), new Point(1.0f,1.0f)); System.out.println("三点是否共线:"+bool);
} }
四、用Junit测试(此部分主要是学习Junit测试):
package com.ywx.test; import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test; import com.ywx.count.IsOnLine;
import com.ywx.count.Point; @SuppressWarnings("static-access")
public class IsOnLineTest{ @Test
public void test1(){//测试三点在原点(或共点)
boolean bool=new IsOnLine().IsLine(new Point(0.0f,0.0f),new Point(0.0f,0.0f),
new Point(0.0f,0.0f));
bool=new IsOnLine().IsLine(new Point(3.0f,3.0f),new Point(3.0f,3.0f),
new Point(3.0f,3.0f));
System.out.println("三点是否共线:"+bool);
}
@Test
public void test2(){//三点随机输入
boolean bool=new IsOnLine().IsLine(new Point(0.0f,2.0f), new Point(1.0f,1.0f), new Point(2.0f,0.0f));
bool=new IsOnLine().IsLine(new Point(1.0f,1.0f), new Point(2.0f,0.0f), new Point(0.0f,2.0f));
bool=new IsOnLine().IsLine(new Point(2.0f,0.0f), new Point(0.0f,2.0f), new Point(1.0f,1.0f)); System.out.println("三点是否共线:"+bool);
}
@Test
public void test3(){//横坐标相等
boolean bool=new IsOnLine().IsLine(new Point(1.0f,2.0f), new Point(1.0f,2.0f), new Point(1.0f,5.0f)); System.out.println("三点是否共线:"+bool);
}
@Test
public void test4(){//纵坐标相等
boolean bool=new IsOnLine().IsLine(new Point(5.0f,2.0f), new Point(1.0f,2.0f), new Point(3.0f,2.0f)); System.out.println("三点是否共线:"+bool);
}
@Test
public void test5(){//斜率测试1
boolean bool=new IsOnLine().IsLine(new Point(1.0f,2.0f),new Point(2.0f,4.0f),
new Point(3.0f,6.0f));
bool=new IsOnLine().IsLine(new Point(0.0f,-1.0f),new Point(1.0f,0.0f),
new Point(2.0f,1.0f));
System.out.println("三点是否共线:"+bool);
}
@Test
public void test6(){//斜率测试2 boolean bool=new IsOnLine().IsLine(new Point(0.0f,-1.0f),new Point(1.0f,0.0f),
new Point(2.0f,1.0f));
bool=new IsOnLine().IsLine(new Point(1.0f,0.0f),new Point(2.0f,1.0f),
new Point(0.0f,-1.0f));
bool=new IsOnLine().IsLine(new Point(2.0f,1.0f),new Point(0.0f,-1.0f),
new Point(1.0f,0.0f)); System.out.println("三点是否共线:"+bool);
}
@Test
public void test7(){
boolean bool=false;
Assert.assertTrue("返回结果:", new IsOnLine().IsLine(new Point(0.0f,-1.0f),new Point(1.0f,0.0f),
new Point(2.0f,1.0f)));
Assert.assertTrue("返回结果:", new IsOnLine().IsLine(new Point(0.0f,-1.0f),new Point(1.0f,0.0f),
new Point(2.0f,1.0f)));
Assert.assertTrue("返回结果:", new IsOnLine().IsLine(new Point(0.0f,-1.0f),new Point(1.0f,0.0f),
new Point(2.0f,1.0f)));
} @Test
public void test0(){ Assert.assertTrue("返回结果:", new IsOnLine().IsLine(new Point(1.0f,0.0f),new Point(2.0f,0.0f),
new Point(3.0f,0.0f))); Assert.assertTrue("返回结果:", new IsOnLine().IsLine(new Point(0.0f,1.0f),new Point(0.0f,2.0f),
new Point(0.0f,3.0f))); }
@Before
public void test8(){
System.out.println("开始测试。。。");
}
@After
public void test9(){
System.out.println("测试结束。。。");
}
}
后记:最后得感谢当时带我的在一线打拼十余年的技术大牛,虽然这是一个小程序,但在他的苛刻要求及耐心指引下意识到很多,这将会在我以后的工作中能派上用场。
总结:小小事情,大感悟。
版权声明:本文为博主原创文章,未经博主允许不得转载。