http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=184334

解决方案 »

  1.   

    好像还有点问题,找不出什么原因,就是当3顶点如下时判断错误
    -----------
    |       /
    |     /
    |   /
    | /
    |/
    import javax.swing.JOptionPane;class MyPoint{

    private double x;
    private double y;

    public MyPoint(){
    x=0;
    y=0;
    }

    public MyPoint(double x , double y){
    this.x=x;
    this.y=y;
    }

    public MyPoint(MyPoint p){
    this.x=p.getX();
    this.y=p.getY();
    }

    public double getX(){
    return x;
    }

    public double getY(){
    return y;
    }

    public boolean isSameLocation(MyPoint p){
    if ( ( Math.abs( x-p.getX() ) < 0.0001 ) &&
     ( Math.abs( y-p.getY() ) < 0.0001 )   )

    return true;
    else 
    return false;
    }

    public double calculateSlope(MyPoint p){

    if ( isSameLocation(p) ) return 0;

    if ( Math.abs( x-p.getX() ) < 0.0001 ) {
    if (  y-p.getY() < 0 ) 
    return  Double.MAX_VALUE;
    else 
    return Double.MIN_VALUE;
    }

    return ( p.getY()-y )/(p.getX()-x);
    }


    public String toString(){
    return "This Point: X="+x+" ;Y="+y;
    }

    }class Triangle{
    private MyPoint v1;
    private MyPoint v2;
    private MyPoint v3;

    public Triangle(MyPoint vv1, MyPoint vv2, MyPoint vv3){

    v1 = vv1;
    v2 = vv2;
    v3 = vv3;
    }

    public boolean isTriangle(){

    if  (  v1.isSameLocation(v2) || 
    v1.isSameLocation(v3) ||
    v3.isSameLocation(v2)   )  
    return false;

    if ( Math.abs( v1.calculateSlope(v2) - v1.calculateSlope(v3) )<0.001 )
    return false;

    return true;
    }

    public double getArea( MyPoint v1, MyPoint v2, MyPoint v3){
    //area = ( (x1-x3)*(y2-y3)  -  (y1-y3)(x2-x3) )/2  
    return Math.abs( ( v1.getX() - v3.getX() ) * ( v2.getY() -v3.getY() )  
      - (v1.getY() - v3.getY()) * ( v2.getX() - v3.getX()) )/2 ; }



    public boolean inThisTriangle(MyPoint p){

           double  s  =  0;    //存放多边形的面积的2倍  
           double  s0  =  0;    //存放该点与该凸多边形各边构成的三角形的面积之和的2倍  
          
            
           s =  getArea(v1,  v2,  v3);
           s0  =  getArea(p,  v1,  v2) 
                  + getArea(p,  v2,  v3) 
                  + getArea(p,  v1,  v3);  
                  
           if ( Math.abs( s-s0 )<0.001 ) 
            return true;
           else 
            return false; }

    }public class TestTriangle{


    public static void main(String args[]){

    String value;
    double[] d= new double[8];


    for (int i=0;i<4;i++){
    value=JOptionPane.showInputDialog("Enter the 'Vertex "+ (i+1) +"' X" );
    d[2*i] = Double.parseDouble(value);
    value=JOptionPane.showInputDialog("Enter the 'Vertex "+ (i+1) +"' Y" );
    d[2*i+1] = Double.parseDouble(value);
    }

    MyPoint p1 = new MyPoint(d[0],d[1]);
    MyPoint p2 = new MyPoint(d[2],d[3]);
    MyPoint p3 = new MyPoint(d[4],d[5]);
    MyPoint p4 = new MyPoint(d[6],d[7]);

    Triangle t= new Triangle(p1,p2,p3);


    System.out.println("Point 1:" +p1 +"\nPoint 2:" +p2 +"\nPoint 3:" +p3 +"\nPoint 4:" +p4 );
    System.out.println(t.getArea(p1,p2,p3));

    if ( t.isTriangle() ){
    if ( t.inThisTriangle(p4) ) 
    System.out.println("The last Point is in the Triangel!!");
    else System.out.println("The last Point is out of the Triangel!!");
    }else
    System.out.println("The Points you input :\n1:  "+p1+"\n2:  "+p2+"\n3:  "+p3+"\nCan not construct a Triangle");

    System.exit(0);
    }

    }